private void notifyListenerOfAddedMetric( final MetricRegistryListener listener, final Metric metric, final String name ) { if(metric instanceof Gauge) { listener.onGaugeAdded(name, (Gauge<?>) metric); } else if(metric instanceof Counter) { listener.onCounterAdded(name, (Counter) metric); } else if(metric instanceof Histogram) { listener.onHistogramAdded(name, (Histogram) metric); } else if(metric instanceof Meter) { listener.onMeterAdded(name, (Meter) metric); } else if(metric instanceof Timer) { listener.onTimerAdded(name, (Timer) metric); } else { throw new IllegalArgumentException("Unsupported metric type: " + metric.getClass()); } }
private void notifyListenerOfRemovedMetric( final String name, final Metric metric, final MetricRegistryListener listener ) { if(metric instanceof Gauge) { listener.onGaugeRemoved(name); } else if(metric instanceof Counter) { listener.onCounterRemoved(name); } else if(metric instanceof Histogram) { listener.onHistogramRemoved(name); } else if(metric instanceof Meter) { listener.onMeterRemoved(name); } else if(metric instanceof Timer) { listener.onTimerRemoved(name); } else { throw new IllegalArgumentException("Unsupported metric type: " + metric.getClass()); } }
@Test public void testWrappingDropwizardMetrics() { Metric2Registry registry = new Metric2Registry(); MetricRegistryListener listener = Mockito.mock(MetricRegistryListener.class); registry.getMetricRegistry().addListener(listener); registry.register(MetricName.name("test").build(), new Gauge<Integer>() { @Override public Integer getValue() { return 1; } }); // Verify the underlying Dropwizard listener was called Mockito.verify(listener).onGaugeAdded(Mockito.eq("test"), Mockito.<Gauge>any()); // Should be able to read the gauge from either registry Map.Entry<MetricName, Gauge> stagemonitorEntry = registry.getGauges().entrySet().iterator().next(); Map.Entry<String, Gauge> dropwizardEntry = registry.getMetricRegistry().getGauges().entrySet().iterator().next(); assertEquals("test", stagemonitorEntry.getKey().getName()); assertEquals(1, stagemonitorEntry.getValue().getValue()); assertEquals("test", dropwizardEntry.getKey()); assertEquals(1, dropwizardEntry.getValue().getValue()); // Unregister should notify Dropwizard listeners registry.remove(MetricName.name("test").build()); Mockito.verify(listener).onGaugeRemoved(Mockito.eq("test")); assertEquals(0, registry.getGauges().size()); assertEquals(0, registry.getMetricRegistry().getGauges().size()); }
/** * Creates a new {@link CustomMetricRegistry}. */ public CustomMetricRegistry() { this.metrics = buildMap(); this.listeners = new CopyOnWriteArrayList<MetricRegistryListener>(); }
private void onMetricAdded(final String name, final Metric metric) { for(final MetricRegistryListener listener : listeners) { notifyListenerOfAddedMetric(listener, metric, name); } }
private void onMetricRemoved(final String name, final Metric metric) { for(final MetricRegistryListener listener : listeners) { notifyListenerOfRemovedMetric(name, metric, listener); } }
@Override public void removeListener(MetricRegistryListener listener) { throw new UnsupportedOperationException(); }
@Override public void addListener(MetricRegistryListener listener) { throw new UnsupportedOperationException(); }
/** * Adds a {@link MetricRegistryListener} to a collection of listeners that will be notified on * metric creation. Listeners will be notified in the order in which they are added. * <p> * <b>N.B.:</b> The listener will be notified of all existing metrics when it first registers. * * @param listener the listener that will be notified */ public final void addListener(final MetricRegistryListener listener) { listeners.add(listener); for(Map.Entry<String, Metric> entry : metrics.entrySet()) { notifyListenerOfAddedMetric(listener, entry.getValue(), entry.getKey()); } }
/** * Removes a {@link MetricRegistryListener} from this registry's collection of listeners. * * @param listener the listener that will be removed */ public final void removeListener(final MetricRegistryListener listener) { listeners.remove(listener); }