Integrating dropwizard metrics with prometheus metrics

Advertisements

Dropwizard is a framework in Java to create web application and focus on micro-services providing a lot nice features. One of those are metrics, Dropwizard metrics helps to instrument exposing metrics with JVM data (memory, cpu, threads), API metrics (requests, execution time, errors, etc) and even custom metrics.

However, Dropwizard metrics lacks on features like labels and use to make calculations on the client side, for example: a metric use to have a 15 minutes lifecycle, so it metrics like Gauges, or Timer provide an average value on the last minute, five minutes and 15 minutes. Which is useful in some case, but also not accurate in other scenarios.

Prometheus metrics is an extended format to handle metrics, several tools are able to process them (Prometheus Server, Graphana, Kibana, etc). The prometheus client library allow the code to create 4 types of metrics: Counter, Gauges, Histogram and Summary. So, we can create our custom metrics, depending on the needs.

Advertisements

It could be the case, it is needed to use Prometheus metrics, because of the format and the custom metrics and its features (for example: labels), but at the same time it’s required to expose all those metrics already provided by Dropwizard. The good news is that integrating the two instrumentation metrics libraries requires only a few of lines of code.

Adding dependency

<dependency>
  <groupId>io.prometheus</groupId>
  <artifactId>simpleclient_dropwizard</artifactId>
  <version>0.16.0</version>
</dependency>
implementation 'io.prometheus:simpleclient_dropwizard:0.16.0'

Integrating dropwizard metrics with prometheus metrics

import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.dropwizard.DropwizardExports;

@Override
public void run(Configuration configuration, Environment environment){
    ... 
	CollectorRegistry.defaultRegistry.register(new DropwizardExports(environment.metrics()));
    ...
}

Conclusion

With that few changes, all the metrics exposed by Dropwizard Metrics (using annotations in endpoints, or JVM metrics, or custom metrics) are also collected by Prometheus, and they will be exposed in prometheus format together with your prometheus metrics.

Reference

Advertisements

Leave a Reply

Your email address will not be published. Required fields are marked *