MetricAdapterFactoryImpl implements MetricAdapterFactory { @Override @SuppressWarnings("unchecked") public MetricAdapter createMetricAdapterFor(String originalMetricName, Metric metric) { Preconditions.checkArgument(!(metric instanceof MetricSet), "Metric Sets cannot be adapted!!"); String translatedName = translate(originalMetricName); MetricDescriptor descriptor = metricDescriptorLookup.getDescriptorFor(translatedName); if (metric instanceof Timer) { return new TimerAdapter((Timer) metric, translatedName, descriptor.getDescription()); } if (metric instanceof Histogram) { return new HistogramAdapter((Histogram) metric, translatedName, descriptor.getDescription(), descriptor.getUnit()); } if (metric instanceof Counter) { return new CountingAdapter((Counter) metric, translatedName, descriptor.getDescription(), descriptor.getSemantics()); } if (metric instanceof Gauge) { return new GaugeAdapter<>((Gauge) metric, translatedName, descriptor.getDescription(), descriptor.getUnit(), descriptor.getSemantics()); } if (metric instanceof Metered) { return new MeteredAdapter((Metered) metric, translatedName, descriptor.getDescription()); } throw new UnsupportedOperationException(String.format("Unable to produce a monitorable adapter for metrics of class %s (%s)", metric.getClass().getName(), originalMetricName)); } MetricAdapterFactoryImpl(MetricDescriptorLookup metricDescriptorLookup, MetricNameTranslator metricNameTranslator);  MetricAdapterFactoryImpl(MetricDescriptorLookup metricDescriptorLookup); @Override @SuppressWarnings("unchecked") MetricAdapter createMetricAdapterFor(String originalMetricName, Metric metric);  }
@Test(expected = IllegalArgumentException.class) public void shouldThrowIllegalArgumentExceptionWhenMetricIsAMetricSet() { metricAdapterFactory.createMetricAdapterFor(NAME, mock(MetricSet.class)); }
@Test(expected = UnsupportedOperationException.class) public void shouldThrowUnsupportedOperationExceptionWhenMetricIsUnrecognized() { metricAdapterFactory.createMetricAdapterFor(NAME, mock(Metric.class)); }
@Test public void shouldNotConvertMetricNamesWhenNoConverterIsProvided() { metricAdapterFactory = new MetricAdapterFactoryImpl(metricDescriptorLookup); assertThat(getFirstMonitorable(metricAdapterFactory.createMetricAdapterFor(NAME, counter)).getName(), is(NAME)); }
@Test public void shouldUseTranslatedMetricNameToLookUpDescriptor() { metricAdapterFactory = new MetricAdapterFactoryImpl(metricDescriptorLookup, metricNameTranslator); metricAdapterFactory.createMetricAdapterFor(NAME, counter); verify(metricDescriptorLookup).getDescriptorFor(TRANSLATED_NAME); }
@Test public void shouldProduceACountingAdapterWhenTheMetricIsACounter() { assertThat(metricAdapterFactory.createMetricAdapterFor(NAME, counter), is(instanceOf(CountingAdapter.class))); }
@Test public void shouldUseTranslatedNameForNameOfReturnedCounterAdapter() { assertThat(getFirstMonitorable(metricAdapterFactory.createMetricAdapterFor(NAME, counter)).getName(), is(TRANSLATED_NAME)); }
@Test public void shouldUseRetrievedDescriptionForCounterAdapter() { assertThat(getFirstMonitorable(metricAdapterFactory.createMetricAdapterFor(NAME, counter)).getDescription(), startsWith(DESCRIPTION)); }
@Test public void shouldSpecifyOneAsUnitOfMeasurementInReturnedCounterAdapter() { assertThat(getFirstMonitorable(metricAdapterFactory.createMetricAdapterFor(NAME, counter)).getUnit(), Matchers.<Unit>is(ONE)); }
@Test public void shouldSpecifyFreeRunningAsValueSemanticsInReturnedCounterAdapter() { assertThat(getFirstMonitorable(metricAdapterFactory.createMetricAdapterFor(NAME, counter)).getSemantics(), is(ValueSemantics.FREE_RUNNING)); }
@Test public void shouldProduceAGaugeAdapterWhenTheMetricIsAGauge() { assertThat(metricAdapterFactory.createMetricAdapterFor(NAME, gauge), is(instanceOf(GaugeAdapter.class))); }
@Test public void shouldUseTranslatedNameForNameOfReturnedGaugeAdapter() { assertThat(getFirstMonitorable(metricAdapterFactory.createMetricAdapterFor(NAME, gauge)).getName(), is(TRANSLATED_NAME)); }
@Test public void shouldUseRetrievedDescriptionForGaugeAdapter() { assertThat(getFirstMonitorable(metricAdapterFactory.createMetricAdapterFor(NAME, gauge)).getDescription(), startsWith(DESCRIPTION)); }
@Test public void shouldSpecifyFreeRunningAsValueSemanticsInReturnedGaugeAdapter() { assertThat(getFirstMonitorable(metricAdapterFactory.createMetricAdapterFor(NAME, gauge)).getSemantics(), is(ValueSemantics.FREE_RUNNING)); }
@Test public void shouldUseUnitFromMetricDescriptorInGaugeAdapter() { assertThat(getFirstMonitorable(metricAdapterFactory.createMetricAdapterFor(NAME, gauge)).getUnit(), Matchers.<Unit>is(GRAY)); }
@Test public void shouldProduceAHistogramAdapterWhenTheMetricIsAHistogram() { assertThat(metricAdapterFactory.createMetricAdapterFor(NAME, histogram), is(instanceOf(HistogramAdapter.class))); }
@Test public void shouldUseTranslatedNameForNameOfReturnedHistogramAdapter() { assertThat(getFirstMonitorable(metricAdapterFactory.createMetricAdapterFor(NAME, histogram)).getName(), startsWith(TRANSLATED_NAME)); }
@Test public void shouldUseRetrievedDescriptionForHistogramAdapter() { assertThat(getFirstMonitorable(metricAdapterFactory.createMetricAdapterFor(NAME, histogram)).getDescription(), startsWith(DESCRIPTION)); }
@Test public void shouldUseUnitFromMetricDescriptorInHistogramAdapter() { assertThat(getMonitorableByName(TRANSLATED_NAME + ".mean", metricAdapterFactory.createMetricAdapterFor(NAME, histogram)).getUnit(), Matchers.<Unit>is(GRAY)); assertThat(getMonitorableByName(TRANSLATED_NAME + ".median", metricAdapterFactory.createMetricAdapterFor(NAME, histogram)).getUnit(), Matchers.<Unit>is(GRAY)); assertThat(getMonitorableByName(TRANSLATED_NAME + ".max", metricAdapterFactory.createMetricAdapterFor(NAME, histogram)).getUnit(), Matchers.<Unit>is(GRAY)); assertThat(getMonitorableByName(TRANSLATED_NAME + ".min", metricAdapterFactory.createMetricAdapterFor(NAME, histogram)).getUnit(), Matchers.<Unit>is(GRAY)); assertThat(getMonitorableByName(TRANSLATED_NAME + ".stddev", metricAdapterFactory.createMetricAdapterFor(NAME, histogram)).getUnit(), Matchers.<Unit>is(GRAY)); }
@Test public void shouldProduceATimerAdapterWhenTheMetricIsATimer() { assertThat(metricAdapterFactory.createMetricAdapterFor(NAME, timer), is(instanceOf(TimerAdapter.class))); }
@Test public void shouldUseTranslatedNameForNameOfReturnedTimerAdapter() { assertThat(getFirstMonitorable(metricAdapterFactory.createMetricAdapterFor(NAME, timer)).getName(), startsWith(TRANSLATED_NAME)); }
@Test public void shouldUseRetrievedDescriptionForTimerAdapter() { assertThat(getFirstMonitorable(metricAdapterFactory.createMetricAdapterFor(NAME, timer)).getDescription(), startsWith(DESCRIPTION)); }
@Test public void shouldProduceAMeteredAdapter_WhenTheMetricIsAMeter() { assertThat(metricAdapterFactory.createMetricAdapterFor(NAME, meter), is(instanceOf(MeteredAdapter.class))); }
@Test public void shouldUseTranslatedNameForNameOfReturnedMeteredAdapter() { assertThat(getFirstMonitorable(metricAdapterFactory.createMetricAdapterFor(NAME, meter)).getName(), startsWith(TRANSLATED_NAME)); }
@Test public void shouldUseRetrievedDescriptionForMeteredAdapter() { assertThat(getFirstMonitorable(metricAdapterFactory.createMetricAdapterFor(NAME, meter)).getDescription(), startsWith(DESCRIPTION)); }