PrometheusReporter implements MetricReporter { @Override public void notifyOfAddedMetric(final Metric metric, final String metricName, final MetricGroup group) { List<String> dimensionKeys = new LinkedList<>(); List<String> dimensionValues = new LinkedList<>(); for (final Map.Entry<String, String> dimension : group.getAllVariables().entrySet()) { final String key = dimension.getKey(); dimensionKeys.add(CHARACTER_FILTER.filterCharacters(key.substring(1, key.length() - 1))); dimensionValues.add(CHARACTER_FILTER.filterCharacters(dimension.getValue())); } final String scopedMetricName = getScopedName(metricName, group); final String helpString = metricName + " (scope: " + getLogicalScope(group) + ")"; final Collector collector; Integer count = 0; synchronized (this) { if (collectorsWithCountByMetricName.containsKey(scopedMetricName)) { final AbstractMap.SimpleImmutableEntry<Collector, Integer> collectorWithCount = collectorsWithCountByMetricName.get(scopedMetricName); collector = collectorWithCount.getKey(); count = collectorWithCount.getValue(); } else { collector = createCollector(metric, dimensionKeys, dimensionValues, scopedMetricName, helpString); try { collector.register(); } catch (Exception e) { LOG.warn("There was a problem registering metric {}.", metricName, e); } } addMetric(metric, dimensionValues, collector); collectorsWithCountByMetricName.put(scopedMetricName, new AbstractMap.SimpleImmutableEntry<>(collector, count + 1)); } }  @Override void open(MetricConfig config); @Override void close(); @Override void notifyOfAddedMetric(final Metric metric, final String metricName, final MetricGroup group); @Override void notifyOfRemovedMetric(final Metric metric, final String metricName, final MetricGroup group);  }
@Test public void registeringSameMetricTwiceDoesNotThrowException() { Counter counter = new SimpleCounter(); counter.inc(); String counterName = "testCounter"; reporter.notifyOfAddedMetric(counter, counterName, metricGroup); reporter.notifyOfAddedMetric(counter, counterName, metricGroup); }
@Test public void addingUnknownMetricTypeDoesNotThrowException(){ class SomeMetricType implements Metric{} reporter.notifyOfAddedMetric(new SomeMetricType(), "name", metricGroup); }