public Map<Point, UntypedMetric> getMapForMetric(@NonNull String metricName) { Map<Point, UntypedMetric> result = new HashMap<>(); for (Map.Entry<Identifier, UntypedMetric> entry : values.entrySet()) { if (metricName.equals(entry.getKey().getName())) { result.put(entry.getKey().getLocation(), entry.getValue()); } } return result; }
/** * Record a sample at the given position. * * @param x * sample value * @param p * position/dimension values for the sample */ public void sample(double x, Point p) { receiver.update(new Sample(new Measurement(Double.valueOf(x)), new Identifier(name, p), AssumedType.GAUGE)); }
public Collection<String> getAllMetricNames() { Set<String> names = new HashSet<>(); for (Identifier id : values.keySet()) { names.add(id.getName()); } return names; }
private SimpleImmutableEntry<Point, UntypedMetric> locationValuePair(Map.Entry<Identifier, UntypedMetric> entry) { return new SimpleImmutableEntry<>(entry.getKey().getLocation(), entry.getValue()); }
/** * Add to this metric at the given point. * * @param n * the amount by which to increase this counter * @param p * the point in the metric space at which to add to the metric */ public void add(long n, Point p) { metricReceiver.update(new Sample(new Measurement(Long.valueOf(n)), new Identifier(name, p), AssumedType.COUNTER)); }
public Map<String, List<Map.Entry<Point, UntypedMetric>>> getValuesByMetricName() { Map<String, List<Map.Entry<Point, UntypedMetric>>> result = new HashMap<>(); for (Map.Entry<Identifier, UntypedMetric> entry : values.entrySet()) { List<Map.Entry<Point, UntypedMetric>> singleMetric; if (result.containsKey(entry.getKey().getName())) { singleMetric = result.get(entry.getKey().getName()); } else { singleMetric = new ArrayList<>(); result.put(entry.getKey().getName(), singleMetric); } singleMetric.add(locationValuePair(entry)); } return result; }
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(); }
private void padMetric(String metric, Bucket toPresent, int currentDataPoints) { final LinkedHashMap<Point, TimeStampedMetric> cachedPoints = getCachedMetric(metric); int toAdd = pointsToKeep - currentDataPoints; @SuppressWarnings({"unchecked","rawtypes"}) Entry<Point, TimeStampedMetric>[] cachedEntries = cachedPoints.entrySet().toArray(new Entry[0]); long nowMillis = System.currentTimeMillis(); for (int i = cachedEntries.length - 1; i >= 0 && toAdd > 0; --i) { Entry<Point, TimeStampedMetric> leastOld = cachedEntries[i]; if (leastOld.getValue().millis + MAX_AGE_MILLIS < nowMillis) { continue; } final Identifier id = new Identifier(metric, leastOld.getKey()); if (!toPresent.hasIdentifier(id)) { toPresent.put(id, leastOld.getValue().metric.pruneData()); --toAdd; } } }
public Collection<Map.Entry<Point, UntypedMetric>> getValuesForMetric(@NonNull String metricName) { List<Map.Entry<Point, UntypedMetric>> singleMetric = new ArrayList<>(); for (Map.Entry<Identifier, UntypedMetric> entry : values.entrySet()) { if (metricName.equals(entry.getKey().getName())) { singleMetric.add(locationValuePair(entry)); } } return singleMetric; }
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)); }
private UntypedMetric get(Sample sample) { Identifier dim = sample.getIdentifier(); UntypedMetric v = values.get(dim); if (v == null) { // please keep inside guard, as sample.getHistogramDefinition(String) touches a volatile v = new UntypedMetric(sample.getHistogramDefinition(dim.getName())); values.put(dim, v); } return v; }
@Override public void set(String key, Number val, Context ctx) { receiver.update(new Sample(new Measurement(val), new Identifier(key, getSimpleCoordinate(ctx)), AssumedType.GAUGE)); }
void merge(Bucket other, boolean otherIsNewer) { LazySet<String> malformedMetrics = LazySet.newHashSet(); for (Map.Entry<Identifier, UntypedMetric> entry : other.values.entrySet()) { String metricName = entry.getKey().getName(); try { if (!malformedMetrics.contains(metricName)) { get(entry.getKey(), entry.getValue()).merge(entry.getValue(), otherIsNewer); } } catch (IllegalArgumentException e) { log.log(LogLevel.WARNING, "Problems merging metric " + metricName + ", possibly ignoring data."); // avoid spamming the log if there are a lot of mismatching // threads malformedMetrics.add(metricName); } } }