@BeforeClass
  public static void setUp() throws Exception {
    if (!tmpDir.toFile().exists()) {
      System.out.println("Creating directory " + tmpDir);
      Files.createDirectories(tmpDir,
              PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rwxr-xr-x")));
    }

    Configuration conf = new Configuration();

    jsonReportFile = File.createTempFile("TestCodahaleMetrics", ".json");
    System.out.println("Json metrics saved in " + jsonReportFile.getAbsolutePath());

    conf.set(MetastoreConf.ConfVars.METRICS_CLASS.getHiveName(), CodahaleMetrics.class.getCanonicalName());
    conf.set(MetastoreConf.ConfVars.HIVE_CODAHALE_METRICS_REPORTER_CLASSES.getHiveName(),
        "org.apache.hadoop.hive.common.metrics.metrics2.JsonFileMetricsReporter, "
            + "org.apache.hadoop.hive.common.metrics.metrics2.JmxMetricsReporter");
    conf.set(MetastoreConf.ConfVars.METRICS_JSON_FILE_LOCATION.getHiveName(), jsonReportFile.getAbsolutePath());
    conf.setTimeDuration(MetastoreConf.ConfVars.METRICS_JSON_FILE_INTERVAL.getHiveName(), REPORT_INTERVAL_MS,
        TimeUnit.MILLISECONDS);

    MetricsFactory.init(conf);
    metricRegistry = ((CodahaleMetrics) MetricsFactory.getInstance()).getMetricRegistry();
  }
Unit test for new Metrics subsystem.
standalone-metastore/metastore-common/src/test/java/org/apache/hadoop/hive/common/metrics/metrics2/TestCodahaleMetrics.java