private void update(InMemoryMetricRepository result, String key, Metric<?> metric) { String name = this.prefix + key; Metric<?> aggregate = result.findOne(name); if (aggregate == null) { aggregate = new Metric<Number>(name, metric.getValue(), metric.getTimestamp()); } else if (key.contains("counter.")) { // accumulate all values aggregate = new Metric<Number>(name, metric.increment(aggregate.getValue().intValue()).getValue(), metric.getTimestamp()); } else if (aggregate.getTimestamp().before(metric.getTimestamp())) { // sort by timestamp and only take the latest aggregate = new Metric<Number>(name, metric.getValue(), metric.getTimestamp()); } result.set(aggregate); }
@Override public Metric<?> findOne(String metricName) { if (!metricName.startsWith(this.prefix)) { return null; } InMemoryMetricRepository result = new InMemoryMetricRepository(); String baseName = metricName.substring(this.prefix.length()); for (Metric<?> metric : this.source.findAll()) { String name = getSourceKey(metric.getName()); if (baseName.equals(name)) { update(result, name, metric); } } return result.findOne(metricName); }
@Override public Iterable<Metric<?>> findAll() { InMemoryMetricRepository result = new InMemoryMetricRepository(); for (Metric<?> metric : this.source.findAll()) { String key = getSourceKey(metric.getName()); if (key != null) { update(result, key, metric); } } return result.findAll(); }
@Bean @ExportMetricReader @ActuatorMetricWriter public InMemoryMetricRepository actuatorMetricRepository() { return new InMemoryMetricRepository(); }
@Bean public MetricRepository metricRepository() { return new InMemoryMetricRepository(); }