Search code examples
javadropwizardprometheus

How to expose Dropwizard Metrics to Prometheus


I have implemented a Java web service using Dropwizard. Now I want it to also expose Prometheus metrics.

I have followed this pretty straight-forward example. However, the endpoint at http://localhost:9090/metrics is still not exposed.

Here's the relevant code:

Dependencies in the pom.xml:

    <dependency>
        <groupId>io.prometheus</groupId>
        <artifactId>simpleclient_dropwizard</artifactId>
        <version>0.5.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/io.prometheus/simpleclient_servlet -->
    <dependency>
        <groupId>io.prometheus</groupId>
        <artifactId>simpleclient_servlet</artifactId>
        <version>0.5.0</version>
    </dependency>

The Java code:

import io.dropwizard.Application;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.dropwizard.DropwizardExports;
import io.prometheus.client.exporter.MetricsServlet;
[...]

public class MyApplication extends Application<MyServiceConfiguration> {

@Override
public void run(final MyServiceConfiguration configuration,
        final Environment environment) {
    final MyServiceResource resource = createResource(configuration);
    environment.jersey().register(resource);

    registerHealthChecks(environment, resource);

    registerMetrics(environment);
}

private void registerMetrics(Environment environment) {
    CollectorRegistry collectorRegistry = new CollectorRegistry();
    collectorRegistry.register(new DropwizardExports(environment.metrics()));
    environment.admin().addServlet("metrics", new MetricsServlet(collectorRegistry))
            .addMapping("/metrics");
}

Any pointers to what I'm doing wrong?


Solution

  • Remember default dropwizard configuration has the admin app on a different port. That's where you'd find the metrics servlet.