DynamicMetricsManager { public void createOrUpdateCounter(String classSimpleName, String key, String metricName, long value) { validateArguments(classSimpleName, metricName); Counter counter = (Counter) checkCache(classSimpleName, key, metricName).orElseGet(() -> { Counter newCounter = _metricRegistry.counter(MetricRegistry.name(classSimpleName, key, metricName)); updateCache(classSimpleName, key, metricName, newCounter); return newCounter; }); counter.inc(value); } private  DynamicMetricsManager(MetricRegistry metricRegistry); static DynamicMetricsManager createInstance(MetricRegistry metricRegistry); static DynamicMetricsManager createInstance(MetricRegistry metricRegistry, @Nullable String testName); static DynamicMetricsManager getInstance(); @SuppressWarnings("unchecked") T registerMetric(String simpleName, String key, String metricName, Class<T> metricClass); T registerMetric(String classSimpleName, String metricName, Class<T> metricClass); @SuppressWarnings("unchecked") Gauge<T> registerGauge(String simpleName, String key, String metricName, Supplier<T> supplier); @SuppressWarnings("unchecked") Gauge<T> registerGauge(String simpleName, String metricName, Supplier<T> supplier); void unregisterMetric(String simpleName, String key, String metricName); void unregisterMetric(String simpleName, String metricName); void createOrUpdateCounter(String classSimpleName, String key, String metricName, long value); void createOrUpdateCounter(String classSimpleName, String metricName, long value); void createOrUpdateMeter(String classSimpleName, String key, String metricName, long value); void createOrUpdateMeter(String classSimpleName, String metricName, long value); void createOrUpdateSlidingWindowHistogram(String classSimpleName, String key, String metricName,
      long windowTimeMs, long value); void createOrUpdateHistogram(String classSimpleName, String key, String metricName, long value); void createOrUpdateHistogram(String classSimpleName, String metricName, long value); @SuppressWarnings("unchecked") T getMetric(String name); MetricRegistry getMetricRegistry();  }
@Test public void testCreateOrUpdateCounter() { String numEvents = "numEvents"; String numErrors = "numErrors"; String fullKeylessMetricName = MetricRegistry.name(CLASS_NAME, numEvents); _metricsManager.createOrUpdateCounter(CLASS_NAME, numEvents, 1); Counter keylessCounter = _metricsManager.getMetric(fullKeylessMetricName); Assert.assertEquals(keylessCounter.getCount(), 1); _metricsManager.createOrUpdateCounter(CLASS_NAME, numEvents, 1); Assert.assertEquals(keylessCounter.getCount(), 2); _metricsManager.createOrUpdateCounter(CLASS_NAME, numEvents, 5); String someKey = "someKey"; String fullMetricName = MetricRegistry.name(CLASS_NAME, someKey, numEvents); _metricsManager.createOrUpdateCounter(CLASS_NAME, someKey, numEvents, 1); Counter keyedCounter = _metricsManager.getMetric(fullMetricName); Assert.assertEquals(keyedCounter.getCount(), 1); _metricsManager.createOrUpdateCounter(CLASS_NAME, someKey, numEvents, 1); Assert.assertEquals(keyedCounter.getCount(), 2); _metricsManager.createOrUpdateCounter(CLASS_NAME, someKey, numErrors, 19); Counter counter = _metricsManager.getMetric(MetricRegistry.name(CLASS_NAME, someKey, numErrors)); Assert.assertEquals(counter.getCount(), 19); Assert.assertNotEquals(keyedCounter, keylessCounter); Assert.assertNotEquals(keyedCounter, counter); String anotherFullKeylessMetricName = MetricRegistry.name(CLASS_NAME, numErrors); _metricsManager.createOrUpdateCounter(CLASS_NAME, numErrors, 1); Counter anotherKeylessCounter = _metricsManager.getMetric(anotherFullKeylessMetricName); Assert.assertEquals(anotherKeylessCounter.getCount(), 1); _metricsManager.createOrUpdateCounter(CLASS_NAME, numErrors, 1); Assert.assertEquals(anotherKeylessCounter.getCount(), 2); _metricsManager.createOrUpdateCounter(CLASS_NAME, numErrors, 5); Assert.assertNotEquals(anotherKeylessCounter, keylessCounter); String anotherKey = "anotherKey"; String anotherFullMetricName = MetricRegistry.name(CLASS_NAME, anotherKey, numErrors); _metricsManager.createOrUpdateCounter(CLASS_NAME, anotherKey, numErrors, 1); Counter anotherKeyedCounter = _metricsManager.getMetric(anotherFullMetricName); Assert.assertEquals(anotherKeyedCounter.getCount(), 1); _metricsManager.createOrUpdateCounter(CLASS_NAME, anotherKey, numErrors, 1); Assert.assertEquals(anotherKeyedCounter.getCount(), 2); Assert.assertNotEquals(keyedCounter, anotherKeyedCounter); Assert.assertTrue(_metricsManager.checkCache(CLASS_NAME, null, numErrors).isPresent()); _metricsManager.unregisterMetric(CLASS_NAME, numErrors); Assert.assertFalse(_metricsManager.checkCache(CLASS_NAME, null, numErrors).isPresent()); Assert.assertTrue(_metricsManager.checkCache(CLASS_NAME, anotherKey, numErrors).isPresent()); _metricsManager.unregisterMetric(CLASS_NAME, anotherKey, numErrors); Assert.assertFalse(_metricsManager.checkCache(CLASS_NAME, anotherKey, numErrors).isPresent()); Assert.assertTrue(_metricsManager.checkCache(CLASS_NAME, null, numEvents).isPresent()); Assert.assertTrue(_metricsManager.checkCache(CLASS_NAME, someKey, numEvents).isPresent()); }