public JdiscMetricsFactory(MetricReceiver receiver) { this.metricReceiver = receiver; this.metricInstance = new SimpleMetricConsumer(receiver); }
@Override public MetricSnapshot latestSnapshot() { Bucket curr = metricReceiver.getSnapshot(); if (curr == null) { log.warning("no snapshot from instance of " + metricReceiver.getClass()); return null; } else { SnapshotConverter converter = new SnapshotConverter(curr); return converter.convert(); } }
@Override public void histogram(PrintStream output) { Bucket curr = metricReceiver.getSnapshot(); if (curr == null) { log.warning("no snapshot from instance of " + metricReceiver.getClass()); } else { SnapshotConverter converter = new SnapshotConverter(curr); converter.outputHistograms(output); } }
MetricSnapshot convert() { for (Map.Entry<Identifier, UntypedMetric> entry : snapshot.entrySet()) { Identifier ident = entry.getKey(); getMap(ident.getLocation()).put(ident.getName(), convert(entry.getValue())); } Map<MetricDimensions, MetricSet> data = new HashMap<>(); for (Map.Entry<Point, Map<String, MetricValue>> entry : perPointData.entrySet()) { MetricDimensions key = convert(entry.getKey()); MetricSet newval = new MetricSet(entry.getValue()); MetricSet old = data.get(key); if (old != null) { // should not happen, this is bad // TODO: consider merging the two MetricSet instances log.warning("losing MetricSet when converting for: "+entry.getKey()); } else { data.put(key, newval); } } return new MetricSnapshot(snapshot.getFromMillis(), snapshot.getToMillis(), TimeUnit.MILLISECONDS, data); }
@Override public void add(String key, Number val, Context ctx) { receiver.update(new Sample(new Measurement(val), new Identifier(key, getSimpleCoordinate(ctx)), AssumedType.COUNTER)); }
default: if (c < 32) { target.append("\\u").append(fourDigitHexString(c)); } else { target.append(c);
private void serializeSingleDimension(StringBuilder buffer, final String dimensionName, Value dimensionValue) { buffer.append('"'); escape(dimensionName, buffer); buffer.append("\": "); switch (dimensionValue.getType()) { case LONG: buffer.append(Long.toString(dimensionValue.longValue())); break; case DOUBLE: buffer.append(Double.toString(dimensionValue.doubleValue())); break; case STRING: buffer.append('"'); escape(dimensionValue.stringValue(), buffer); buffer.append('"'); break; default: buffer.append("\"Unknown type for this dimension, this is a bug.\""); break; } }
static MetricDimensions convert(Point p) { if (p == null) { return StateMetricContext.newInstance(null); } List<String> dimensions = p.dimensions(); List<Value> location = p.location(); Map<String, Object> pointWrapper = new HashMap<>(dimensions.size()); for (int i = 0; i < dimensions.size(); ++i) { pointWrapper.put(dimensions.get(i), valueAsString(location.get(i))); } return StateMetricContext.newInstance(pointWrapper); }
private String getIdentifierString(Identifier id) { StringBuilder buffer = new StringBuilder(); Point location = id.getLocation(); buffer.append(id.getName()); if (location != null) { buffer.append(", dimensions: { "); Iterator<String> dimensions = location.dimensions().iterator(); Iterator<Value> values = location.location().iterator(); boolean firstDimension = true; while (dimensions.hasNext() && values.hasNext()) { if (firstDimension) { firstDimension = false; } else { buffer.append(", "); } serializeSingleDimension(buffer, dimensions.next(), values.next()); } buffer.append(" }"); } return buffer.toString(); }
void outputHistograms(PrintStream output) { boolean gotHistogram = false; for (Map.Entry<Identifier, UntypedMetric> entry : snapshot.entrySet()) { if (entry.getValue().getHistogram() == null) { continue; } gotHistogram = true; DoubleHistogram histogram = entry.getValue().getHistogram(); Identifier id = entry.getKey(); String metricIdentifier = getIdentifierString(id); output.println("# start of metric " + metricIdentifier); histogram.outputPercentileDistribution(output, 4, 1.0d, true); output.println("# end of metric " + metricIdentifier); } if (!gotHistogram) { output.println("# No histograms currently available."); } }
static MetricValue convert(UntypedMetric val) { if (val.isCounter()) { return CountMetric.newInstance(val.getCount()); } else { if (val.getHistogram() == null) { return GaugeMetric.newInstance(val.getLast(), val.getMax(), val.getMin(), val.getSum(), val.getCount()); } else { return GaugeMetric.newInstance(val.getLast(), val.getMax(), val.getMin(), val.getSum(), val.getCount(), Optional.of(buildPercentileList(val.getHistogram()))); } } }
@Override public void set(String key, Number val, Context ctx) { receiver.update(new Sample(new Measurement(val), new Identifier(key, getSimpleCoordinate(ctx)), AssumedType.GAUGE)); }