@SuppressWarnings("unchecked") protected synchronized <T extends ContextAwareMetric> T getOrCreate(String name, ContextAwareMetricFactory<T> factory) { InnerMetric metric = this.contextAwareMetrics.get(name); if (metric != null) { if (factory.isInstance(metric)) { return (T) metric.getContextAwareMetric(); } throw new IllegalArgumentException(name + " is already used for a different type of metric"); } T newMetric = factory.newMetric(this.metricContext.get(), name); this.register(name, newMetric); return newMetric; }
@Test public void testContextAwareTimerFactory() { Assert.assertTrue(ContextAwareMetricFactory.DEFAULT_CONTEXT_AWARE_TIMER_FACTORY.isInstance(this.timer)); Assert.assertFalse(ContextAwareMetricFactory.DEFAULT_CONTEXT_AWARE_TIMER_FACTORY.isInstance(this.counter)); Assert.assertEquals(this.timer.getContext(), this.context); Assert.assertEquals(this.timer.getName(), TOTAL_DURATION); }
@BeforeClass public void setUp() { this.context = MetricContext.builder(CONTEXT_NAME).build(); this.counter = ContextAwareMetricFactory.DEFAULT_CONTEXT_AWARE_COUNTER_FACTORY.newMetric( this.context, RECORDS_PROCESSED); this.meter = ContextAwareMetricFactory.DEFAULT_CONTEXT_AWARE_METER_FACTORY.newMetric( this.context, RECORD_PROCESS_RATE); this.histogram = ContextAwareMetricFactory.DEFAULT_CONTEXT_AWARE_HISTOGRAM_FACTORY.newMetric( this.context, RECORD_SIZE_DISTRIBUTION); this.timer = ContextAwareMetricFactory.DEFAULT_CONTEXT_AWARE_TIMER_FACTORY.newMetric( this.context, TOTAL_DURATION); }
@Test public void testContextAwareCounterFactory() { Assert.assertTrue(ContextAwareMetricFactory.DEFAULT_CONTEXT_AWARE_COUNTER_FACTORY.isInstance(this.counter)); Assert.assertFalse(ContextAwareMetricFactory.DEFAULT_CONTEXT_AWARE_COUNTER_FACTORY.isInstance(this.meter)); Assert.assertEquals(this.counter.getContext(), this.context); Assert.assertEquals(this.counter.getName(), RECORDS_PROCESSED); }
@SuppressWarnings("unchecked") protected synchronized <T extends ContextAwareMetric> T getOrCreate( ContextAwareMetricFactory<T> factory, ContextAwareMetricFactoryArgs args) { String name = args.getName(); InnerMetric metric = this.contextAwareMetrics.get(name); if (metric != null) { if (factory.isInstance(metric)) { return (T) metric.getContextAwareMetric(); } throw new IllegalArgumentException(name + " is already used for a different type of metric"); } T newMetric = factory.newMetric(args); this.register(name, newMetric); return newMetric; }
@Test public void testContextAwareHistogramFactory() { Assert.assertTrue(ContextAwareMetricFactory.DEFAULT_CONTEXT_AWARE_HISTOGRAM_FACTORY.isInstance(this.histogram)); Assert.assertFalse(ContextAwareMetricFactory.DEFAULT_CONTEXT_AWARE_HISTOGRAM_FACTORY.isInstance(this.timer)); Assert.assertEquals(this.histogram.getContext(), this.context); Assert.assertEquals(this.histogram.getName(), RECORD_SIZE_DISTRIBUTION); }
@SuppressWarnings("unchecked") protected synchronized <T extends ContextAwareMetric> T getOrCreate(String name, ContextAwareMetricFactory<T> factory) { InnerMetric metric = this.contextAwareMetrics.get(name); if (metric != null) { if (factory.isInstance(metric)) { return (T) metric.getContextAwareMetric(); } throw new IllegalArgumentException(name + " is already used for a different type of metric"); } T newMetric = factory.newMetric(this.metricContext.get(), name); this.register(name, newMetric); return newMetric; }
@Test public void testContextAwareMeterFactory() { Assert.assertTrue(ContextAwareMetricFactory.DEFAULT_CONTEXT_AWARE_METER_FACTORY.isInstance(this.meter)); Assert.assertFalse(ContextAwareMetricFactory.DEFAULT_CONTEXT_AWARE_METER_FACTORY.isInstance(this.histogram)); Assert.assertEquals(this.meter.getContext(), this.context); Assert.assertEquals(this.meter.getName(), RECORD_PROCESS_RATE); }
@SuppressWarnings("unchecked") protected synchronized <T extends ContextAwareMetric> T getOrCreate( ContextAwareMetricFactory<T> factory, ContextAwareMetricFactoryArgs args) { String name = args.getName(); InnerMetric metric = this.contextAwareMetrics.get(name); if (metric != null) { if (factory.isInstance(metric)) { return (T) metric.getContextAwareMetric(); } throw new IllegalArgumentException(name + " is already used for a different type of metric"); } T newMetric = factory.newMetric(args); this.register(name, newMetric); return newMetric; }