Metrics is a minimal library for various server monitoring metrics, and easy reporting. Supported metrics include rate meters; instant gauges; counters; histograms; timers. Furthermore, various reporting formatting is included. See their Getting started page for more details.

It’s available on Maven Central, so the following Gradle config will include the latest 3.1.0 version.

repositories {
  mavenCentral()
}

dependencies {
  compile 'io.dropwizard.metrics:metrics-core:3.1.0'
}

The simplified tests in the code below will output this report.

2/9/17 8:10:50 PM =============================================================

-- Histograms ------------------------------------------------------------------
histogram
             count = 4
               min = 1
               max = 3
              mean = 2.50
            stddev = 0.87
            median = 3.00
              75% <= 3.00
              95% <= 3.00
              98% <= 3.00
              99% <= 3.00
            99.9% <= 3.00


2/9/17 8:10:50 PM =============================================================

-- Gauges ----------------------------------------------------------------------
gauge
             value = 123


2/9/17 8:10:50 PM =============================================================

-- Meters ----------------------------------------------------------------------
meter
             count = 2
         mean rate = 19.68 events/second
     1-minute rate = 0.00 events/second
     5-minute rate = 0.00 events/second
    15-minute rate = 0.00 events/second


2/9/17 8:10:50 PM =============================================================

-- Timers ----------------------------------------------------------------------
timer
             count = 1
         mean rate = 4.95 calls/second
     1-minute rate = 0.00 calls/second
     5-minute rate = 0.00 calls/second
    15-minute rate = 0.00 calls/second
               min = 200.13 milliseconds
               max = 200.13 milliseconds
              mean = 200.13 milliseconds
            stddev = 0.00 milliseconds
            median = 200.13 milliseconds
              75% <= 200.13 milliseconds
              95% <= 200.13 milliseconds
              98% <= 200.13 milliseconds
              99% <= 200.13 milliseconds
            99.9% <= 200.13 milliseconds


2/9/17 8:10:50 PM =============================================================

-- Counters --------------------------------------------------------------------
counter
             count = 6

Some metrics examples:

MetricsApiTest.java
GitHub Raw
/* Copyright rememberjava.com. Licensed under GPL 3. See http://rememberjava.com/license */
package com.rememberjava.performance;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.Counter;
import com.codahale.metrics.Gauge;
import com.codahale.metrics.Histogram;
import com.codahale.metrics.Meter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
import com.codahale.metrics.Timer.Context;

public class MetricsApiTest {

  private MetricRegistry metrics;

  @Before
  public void setup() {
    metrics = new MetricRegistry();
  }

  @After
  public void report() {
    ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics).build();
    reporter.report();
  }

  @Test
  public void testMeter() throws InterruptedException {
    Meter meter = metrics.meter("meter");
    meter.mark();
    Thread.sleep(100);
    meter.mark();
  }

  @Test
  public void testGauge() {
    Gauge<Integer> g = (() -> 123);
    metrics.register("gauge", g);
  }

  @Test
  public void testCounter() {
    Counter counter = metrics.counter("counter");
    counter.inc();
    counter.inc(5);
  }

  @Test
  public void testHistogram() {
    Histogram histogram = metrics.histogram("histogram");
    histogram.update(1);
    histogram.update(3);
    histogram.update(3);
    histogram.update(3);
  }

  @Test
  public void testTimer() throws InterruptedException {
    Timer timer = metrics.timer("timer");
    Context time = timer.time();
    Thread.sleep(200);
    time.stop();
  }
}