DynamicMetricsManager { public void createOrUpdateMeter(String classSimpleName, String key, String metricName, long value) { validateArguments(classSimpleName, metricName); Meter meter = (Meter) checkCache(classSimpleName, key, metricName).orElseGet(() -> { Meter newMeter = _metricRegistry.meter(MetricRegistry.name(classSimpleName, key, metricName)); updateCache(classSimpleName, key, metricName, newMeter); return newMeter; }); meter.mark(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 testCreateOrUpdateMeter() { String eventRate = "eventRate"; String errorRate = "errorRate"; String fullKeylessMetricName = MetricRegistry.name(CLASS_NAME, eventRate); _metricsManager.createOrUpdateMeter(CLASS_NAME, eventRate, 1); Meter keylessMeter = _metricsManager.getMetric(fullKeylessMetricName); Assert.assertEquals(keylessMeter.getCount(), 1); _metricsManager.createOrUpdateMeter(CLASS_NAME, eventRate, 1); Assert.assertEquals(keylessMeter.getCount(), 2); _metricsManager.createOrUpdateMeter(CLASS_NAME, eventRate, 5); String someKey = "someKey"; String fullMetricName = MetricRegistry.name(CLASS_NAME, someKey, eventRate); _metricsManager.createOrUpdateMeter(CLASS_NAME, someKey, eventRate, 1); Meter keyedMeter = _metricsManager.getMetric(fullMetricName); Assert.assertEquals(keyedMeter.getCount(), 1); _metricsManager.createOrUpdateMeter(CLASS_NAME, someKey, eventRate, 1); Assert.assertEquals(keyedMeter.getCount(), 2); _metricsManager.createOrUpdateMeter(CLASS_NAME, someKey, errorRate, 19); Meter meter = _metricsManager.getMetric(MetricRegistry.name(CLASS_NAME, someKey, errorRate)); Assert.assertEquals(meter.getCount(), 19); Assert.assertNotEquals(keyedMeter, keylessMeter); Assert.assertNotEquals(keyedMeter, meter); String anotherFullKeylessMetricName = MetricRegistry.name(CLASS_NAME, errorRate); _metricsManager.createOrUpdateMeter(CLASS_NAME, errorRate, 11); Meter anotherKeylessMeter = _metricsManager.getMetric(anotherFullKeylessMetricName); Assert.assertEquals(anotherKeylessMeter.getCount(), 11); Assert.assertNotEquals(anotherKeylessMeter, keylessMeter); String anotherKey = "anotherKey"; String anotherFullKeyedMetricName = MetricRegistry.name(CLASS_NAME, anotherKey, errorRate); _metricsManager.createOrUpdateMeter(CLASS_NAME, anotherKey, errorRate, 6); Meter anotherKeyedMeter = _metricsManager.getMetric(anotherFullKeyedMetricName); Assert.assertEquals(anotherKeyedMeter.getCount(), 6); Assert.assertNotEquals(anotherKeyedMeter, keyedMeter); Assert.assertTrue(_metricsManager.checkCache(CLASS_NAME, null, errorRate).isPresent()); _metricsManager.unregisterMetric(CLASS_NAME, errorRate); Assert.assertFalse(_metricsManager.checkCache(CLASS_NAME, null, errorRate).isPresent()); Assert.assertTrue(_metricsManager.checkCache(CLASS_NAME, anotherKey, errorRate).isPresent()); _metricsManager.unregisterMetric(CLASS_NAME, anotherKey, errorRate); Assert.assertFalse(_metricsManager.checkCache(CLASS_NAME, anotherKey, errorRate).isPresent()); Assert.assertTrue(_metricsManager.checkCache(CLASS_NAME, null, eventRate).isPresent()); Assert.assertTrue(_metricsManager.checkCache(CLASS_NAME, someKey, eventRate).isPresent()); }