CounterToRateMetricTransform implements MetricObserver { public void update(List<Metric> metrics) { Preconditions.checkNotNull(metrics, "metrics"); LOGGER.debug("received {} metrics", metrics.size()); final List<Metric> newMetrics = new ArrayList<>(metrics.size()); for (Metric m : metrics) { if (isCounter(m)) { final MonitorConfig rateConfig = toRateConfig(m.getConfig()); final CounterValue prev = cache.get(rateConfig); if (prev != null) { final double rate = prev.computeRate(m); newMetrics.add(new Metric(rateConfig, m.getTimestamp(), rate)); } else { CounterValue current = new CounterValue(m); cache.put(rateConfig, current); if (intervalMillis > 0L) { final double delta = m.getNumberValue().doubleValue(); final double rate = current.computeRate(intervalMillis, delta); newMetrics.add(new Metric(rateConfig, m.getTimestamp(), rate)); } } } else { newMetrics.add(m); } } LOGGER.debug("writing {} metrics to downstream observer", newMetrics.size()); observer.update(newMetrics); } CounterToRateMetricTransform(MetricObserver observer, long heartbeat, TimeUnit unit);   CounterToRateMetricTransform(
      MetricObserver observer, long heartbeat, long estPollingInterval, TimeUnit unit,
      final Clock clock);  CounterToRateMetricTransform(
      MetricObserver observer, long heartbeat, long estPollingInterval, TimeUnit unit); String getName(); void update(List<Metric> metrics); void reset();  }
@Test public void testResultingType() throws Exception { MemoryMetricObserver mmo = new MemoryMetricObserver("m", 1); MetricObserver transform = new CounterToRateMetricTransform(mmo, 120, TimeUnit.SECONDS); Map<String, String> metrics; long baseTime = System.currentTimeMillis() + 100000L; transform.update(mkList(baseTime + 0, 0)); metrics = mkTypeMap(mmo.getObservations()); assertEquals(metrics.size(), 2); assertEquals(metrics.get("m3"), null); assertEquals(metrics.get("m2"), "GAUGE"); assertEquals(metrics.get("m1"), null); transform.update(mkList(baseTime + 5000, 5)); metrics = mkTypeMap(mmo.getObservations()); assertEquals(metrics.size(), 3); assertEquals(metrics.get("m3"), "RATE"); assertEquals(metrics.get("m2"), "GAUGE"); assertEquals(metrics.get("m1"), null); }
@Test public void testSimpleRate() throws Exception { MemoryMetricObserver mmo = new MemoryMetricObserver("m", 1); MetricObserver transform = new CounterToRateMetricTransform(mmo, 120, TimeUnit.SECONDS); Map<String, Double> metrics; long baseTime = System.currentTimeMillis() + 100000L; transform.update(mkList(baseTime + 0, 0)); metrics = mkMap(mmo.getObservations()); assertEquals(metrics.size(), 2); assertTrue(metrics.get("m3") == null); transform.update(mkList(baseTime + 5000, 5)); metrics = mkMap(mmo.getObservations()); assertEquals(metrics.size(), 3); assertEquals(metrics.get("m3"), 1.0, 0.00001); transform.update(mkList(baseTime + 10000, 20)); metrics = mkMap(mmo.getObservations()); assertEquals(metrics.size(), 3); assertEquals(metrics.get("m3"), 3.0, 0.00001); transform.update(mkList(baseTime + 15000, 20)); metrics = mkMap(mmo.getObservations()); assertEquals(metrics.size(), 3); assertEquals(metrics.get("m3"), 0.0, 0.00001); transform.update(mkList(baseTime + 20000, 19)); metrics = mkMap(mmo.getObservations()); assertEquals(metrics.size(), 3); assertEquals(metrics.get("m3"), 0.0, 0.00001); }
@Test public void testFirstSample() throws Exception { MemoryMetricObserver mmo = new MemoryMetricObserver("m", 1); MetricObserver transform = new CounterToRateMetricTransform(mmo, 120, 5, TimeUnit.SECONDS); Map<String, Double> metrics; long baseTime = System.currentTimeMillis() + 100000L; transform.update(mkList(baseTime + 0, 10)); metrics = mkMap(mmo.getObservations()); assertEquals(metrics.size(), 3); assertEquals(metrics.get("m3"), 2.0, 0.00001); transform.update(mkList(baseTime + 5000, 15)); metrics = mkMap(mmo.getObservations()); assertEquals(metrics.size(), 3); assertEquals(metrics.get("m3"), 1.0, 0.00001); }