Dropwizard metrics migration

The MicroProfile metrics API is similar to Dropwizard metrics 3.2.3. In most cases, code that is instrumented with Dropwizard metrics can run with MicroProfile metrics with a few small changes.

Key differences in MicroProfile Metrics

  • Only a subset of core classes from metrics-core and metrics-annotation are available.
  • Classes are packaged under org.eclipse.microprofile.metrics and org.eclipse.microprofile.metrics.annotation.
  • Counter, Histogram, Timer, Meter, and MetricRegistry are interfaces.
  • CDI can be used to get the MetricRegistry and metric instances.
  • A single shared application MetricRegistry.
  • Metadata can be supplied and registered with the metric.
  • Custom clock and reservoirs cannot be supplied.

Package names

A subset of classes from the Dropwizard metrics-core and metrics-annotation packages can be found in the MicroProfile metrics package org.eclipse.microprofile.metrics. The following example illustrates how you can update the package name of your Dropwizard metrics classes to work with MicroProfile metrics.
  • com.codahale.metrics becomes org.eclipse.microprofile.metrics
  • com.codahale.metrics.annotation becomes org.eclipse.microprofile.metrics.annotation

Obtaining the MetricRegistry

The MetricRegistry is an interface. Instead of creating an instance of the registry, you can inject the Application MetricRegistry by using CDI.

Instead of using
// Dropwizard snippet
MetricRegistry metrics = new MetricRegistry();
// or
MetricRegistry sharedMetrics = SharedMetricRegistries.getOrCreate(...);
You can use
@Inject
MetricRegistry metrics;

Counter, Histogram, Timer, Meter interfaces

Counter, Histogram, Timer, and Meter are interfaces. They cannot be directly instantiated and must be created by using the registry or by using CDI. For example:

Counter jobCount = metrics.counter("jobs");

//Example using annotation
@Inject
@Metric(name="jobs", absolute=true)
Counter jobCount;