Source:
public void jsonReporter() throws Exception { String jsonFile = System.getProperty("java.io.tmpdir") + System.getProperty("file.separator") + "TestMetricsOutput.json"; Configuration conf = MetastoreConf.newMetastoreConf(); MetastoreConf.setVar(conf, MetastoreConf.ConfVars.METRICS_REPORTERS, "json"); MetastoreConf.setVar(conf, MetastoreConf.ConfVars.METRICS_JSON_FILE_LOCATION, jsonFile); MetastoreConf.setTimeVar(conf, MetastoreConf.ConfVars.METRICS_JSON_FILE_INTERVAL, 1, TimeUnit.SECONDS); Metrics.initialize(conf); final List<String> words = Arrays.asList("mary", "had", "a", "little", "lamb"); MetricRegistry registry = Metrics.getRegistry(); registry.register("my-gauge", new Gauge<Integer>() { @Override public Integer getValue() { return words.size(); } }); Counter counter = Metrics.getOrCreateCounter("my-counter"); counter.inc(); counter.inc(); Histogram hist = registry.histogram("my-histogram"); hist.update(5); hist.update(17); Meter meter = registry.meter("my-meter"); meter.mark(); Thread.sleep(10); meter.mark(); Timer timer = Metrics.getOrCreateTimer("my-timer"); timer.time(new Callable<Long>() { @Override public Long call() throws Exception { Thread.sleep(100); return 1L; } }); Thread.sleep(2000); FileSystem fs = FileSystem.get(conf); Path path = new Path(jsonFile); Assert.assertTrue(fs.exists(path)); String json = new String(MetricsTestUtils.getFileData(jsonFile, 200, 10)); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.COUNTER, "my-counter", 2); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.METER, "my-meter", 2); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.TIMER, "my-timer", 1); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, "my-gauge", 5); }

Target:
public void jsonReporter() throws Exception { String jsonFile = System.getProperty("java.io.tmpdir") + System.getProperty("file.separator") + "TestMetricsOutput.json"; Configuration conf = MetastoreConf.newMetastoreConf(); MetastoreConf.setVar(conf, MetastoreConf.ConfVars.METRICS_REPORTERS, "json"); MetastoreConf.setVar(conf, MetastoreConf.ConfVars.METRICS_JSON_FILE_LOCATION, jsonFile); MetastoreConf.setTimeVar(conf, MetastoreConf.ConfVars.METRICS_JSON_FILE_INTERVAL, 1, TimeUnit.SECONDS); Metrics.initialize(conf); final List<String> words = Arrays.asList("mary", "had", "a", "little", "lamb"); MetricRegistry registry = Metrics.getRegistry(); registry.register("my-gauge", new Gauge<Integer>() { @Override public Integer getValue() { return words.size(); } }); Counter counter = Metrics.getOrCreateCounter("my-counter"); counter.inc(); counter.inc(); Meter meter = registry.meter("my-meter"); meter.mark(); Thread.sleep(10); meter.mark(); Timer timer = Metrics.getOrCreateTimer("my-timer"); timer.time(new Callable<Long>() { @Override public Long call() throws Exception { Thread.sleep(100); return 1L; } }); Thread.sleep(2000); FileSystem fs = FileSystem.get(conf); Path path = new Path(jsonFile); Assert.assertTrue(fs.exists(path)); String json = new String(MetricsTestUtils.getFileData(jsonFile, 200, 10)); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.COUNTER, "my-counter", 2); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.METER, "my-meter", 2); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.TIMER, "my-timer", 1); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, "my-gauge", 5); }