Sling Metrics

Sling Metrics bundle provides integration with Dropwizard Metrics library which provides a toolkit to capture runtime performance statistics in your application.


  • Registers a MetricsService which can be used to create various types of Metric instances
  • WebConsole Plugin which provides a HTML Reporter for the various Metric instances
  • Inventory Plugin which dumps the Metric state in plain text format

Basic Usage


private MetricsService metricsService;

private Counter counter;

private void activate(){
    counter = metricsService.counter("sessionCounter");

public void onSessionCreation(){

To make use of MetricsService

  1. Get a reference to
  2. Initialize the metric e.g. Counter in above case. This avoids any potential lookup cost in critical code paths
  3. Make use of metric instance to capture require stats

Refer to Metric Getting Started guide to see how various types of Metric instances can be used. Note that when using Sling Commons Metrics bundle class names belong to package

Best Practices

  1. Use descriptive names - Qualify the name with class/package name where the metric is being used
  2. Do not use the metrics for operation which take less than 1E-7s i.e. 1000 nano seconds otherwise timer overhead (Metrics makes use of System.nanoTime) would start affecting the performance.


Sling Metrics bundle provides its own Metric classes which are modelled on Dropwizard Metrics library. The metric interfaces defined by Sling bundle only provides methods related to data collection.

Further it provides a MetricsService which enables creation of different type of Metrics like Meter, Timer, Counter and Histogram.

Requirement of wrapper interfaces

  • Abstraction - Provides an abstraction around how metrics are collected and how they are reported and consumed. Most of the code would only be concerned with collecting interesting data. How it gets consumed or reported is implementation detail.
  • Ability to turnoff stats collection - We can easily turn off data collection by switching to NOOP variant of MetricsService in case it starts adding appreciable overhead. Turning on and off can also be done on individual metric basis.

It also allows us to later extend the type of data collected. For e.g. we can also collect TimerSeries type of data for each metric without modifying the caller logic.

Access to Dropwizard Metrics API

Sling Metrics bundle also registers the MetricRegistry instance with OSGi service registry. The instance registered has a service property name set to sling (so as allow distinguishing from any other registered MetricRegistry instance). It can be used to get direct access to Dropwizard Metric API if required.

@Reference(target = "(name=sling)")
private MetricRegistry registry;

Also the wrapper Metric instance can be converted to actual instance via adaptTo calls.


Counter counter = metricService.counter("login");
com.codahale.metrics.Counter = counter.adaptTo(com.codahale.metrics.Counter.class)

WebConsole Plugin

A Web Console plugin is also provided which is accessible at http://localhost:8080/system/console/slingmetrics. It lists down all registered Metric instances and their state.

Metric Plugin

The plugin lists all Metric instances from any MetricRegistry instance found in the OSGi service registry. If the MetricRegistry service has a name property defined then that would be prefixed to the Metric names from that registry. This allows use of same name in different registry instances.


Add following Maven dependency to your pom.xml:


Or download from here

- ( Sling Metrics )