### Question:
SpectatorReporter extends ScheduledReporter { @SuppressWarnings("PMD.NPathComplexity") @Override public void report( SortedMap<String, Gauge> gauges, SortedMap<String, Counter> counters, SortedMap<String, Histogram> histograms, SortedMap<String, Meter> meters, SortedMap<String, Timer> timers) { LOGGER.debug("gauges {}, counters {}, histograms {}, meters {}, timers {}", gauges.size(), counters.size(), histograms.size(), meters.size(), timers.size()); for (Map.Entry<String, Gauge> entry : gauges.entrySet()) { final Object obj = entry.getValue().getValue(); if (obj instanceof Number) { if (gaugeCounters.matcher(entry.getKey()).matches()) { final long v = ((Number) obj).longValue(); updateCounter(entry.getKey(), v); } else { final double v = ((Number) obj).doubleValue(); setGaugeValue(entry.getKey(), v); } } } for (Map.Entry<String, Counter> entry : counters.entrySet()) { if (gaugeCounters.matcher(entry.getKey()).matches()) { updateCounter(entry.getKey(), entry.getValue().getCount()); } else { setGaugeValue(entry.getKey(), entry.getValue().getCount()); } } for (Map.Entry<String, Histogram> entry : histograms.entrySet()) { final Id id = nameFunction.apply(entry.getKey()); if (id != null) { final DistributionSummary sHisto = spectatorRegistry.distributionSummary(id); final Histogram mHisto = entry.getValue(); final long[] vs = mHisto.getSnapshot().getValues(); for (long v : vs) { sHisto.record(v); } } } for (Map.Entry<String, Meter> entry : meters.entrySet()) { updateCounter(entry.getKey(), entry.getValue().getCount()); } for (Map.Entry<String, Timer> entry : timers.entrySet()) { final Id id = nameFunction.apply(entry.getKey()); if (id != null) { final com.netflix.spectator.api.Timer sTimer = spectatorRegistry.timer(id); final Timer mTimer = entry.getValue(); final long[] vs = mTimer.getSnapshot().getValues(); for (long v : vs) { sTimer.record(v, TimeUnit.NANOSECONDS); } } } } SpectatorReporter(
      MetricRegistry metricRegistry,
      Registry spectatorRegistry,
      NameFunction nameFunction,
      ValueFunction valueFunction,
      Pattern gaugeCounters); static Builder forRegistry(MetricRegistry registry); @SuppressWarnings("PMD.NPathComplexity") @Override void report(
      SortedMap<String, Gauge> gauges,
      SortedMap<String, Counter> counters,
      SortedMap<String, Histogram> histograms,
      SortedMap<String, Meter> meters,
      SortedMap<String, Timer> timers);  }### Answer:
@Test public void incrementGaugeCounter() { long before = registry.counter("gaugeCounter").count(); gaugeCounter.inc(); reporter.report(); long after = registry.counter("gaugeCounter").count(); Assertions.assertEquals(before + 1, after); metricsCounter.dec(); reporter.report(); before = after; after = getValue("gaugeCounter"); Assertions.assertEquals(before, after); }
@Test public void incrementMetricsCounter() { reporter.report(); long before = getValue("metricsCounter"); metricsCounter.inc(); reporter.report(); long after = getValue("metricsCounter"); Assertions.assertEquals(before + 1, after); metricsCounter.dec(); reporter.report(); before = after; after = getValue("metricsCounter"); Assertions.assertEquals(before - 1, after); }