Skip to content

MaksymDolgykh/dropwizard-micrometer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

dropwizard-micrometer

Dropwizard bundle that enables your dropwizard application for exposition of micrometer-like metrics (system, jvm and http requests) as a prometheus endpoint. In addition, if you use JDBI to manage mapping of objects to database tables, you can utilize dropwizad-micrometer-jdbi package to meter SQL request's latencies.

Packages

  • dropwizad-micrometer-core Dropwizard bundle that implements prometheus endpoint and exposes core system metrics and JVM metrics utilizing micrometer instrumentation. Optionally, it provides servlet filter to record HTTP requests latencies and statuses within dimensional prometheus histogram.

  • dropwizad-micrometer-jdbi An additional module to record latencies of JDBI queries within dimensional prometheus histogram.

Usage

You can find an example of usage in the ExampleApplication.

Below are the steps explained in more detail specifically for each package.

dropwizad-micrometer-core

This package provides a minimal setup, i.e. it instantiates /prometheus endpoint, adds system and JVM metrics utilizing micrometer instrumentation, and optionally you can set up servlet filter to record HTTP requests latencies/statuses.

Add dependency into your pom.xml

If you use maven, you can simply reference it in the <dependenccies> block as below. The latest version can be found in Releases or in the maven repository

    <dependencies>
        ...
        ...
        <dependency>
            <groupId>io.github.maksymdolgykh.dropwizard</groupId>
            <artifactId>dropwizard-micrometer-core</artifactId>
            <version>2.0.5</version>
        </dependency>
        ...
        ...
    </dependencies>

Import DropwizardMicrometer classes in your Application class

import io.github.maksymdolgykh.dropwizard.micrometer.MicrometerBundle;
import io.github.maksymdolgykh.dropwizard.micrometer.MicrometerHttpFilter;
import javax.servlet.FilterRegistration;
import javax.servlet.DispatcherType;
import java.util.EnumSet;

Add the bundle to your application

Add MicrometerBundle class to the bootstrapping phase of your Application class

public class ExampleApplication extends Application<ExampleConfiguration> {
    //...
    //...

    @Override
    public void initialize(Bootstrap<ExampleConfiguration> bootstrap) {
        //...
        //...
        bootstrap.addBundle(new MicrometerBundle());
        //...
        //...
    }
    //...
    //...
}

You will also need to make your ExampleConfiguration class implement MicrometerBundleConfiguration in order to provide the bundle with the configuration:

public class ExampleConfiguration extends Configuration implements MicrometerBundleConfiguration {
    @JsonProperty("prometheus")
    private PrometheusConfiguration prometheusConfiguration = new PrometheusConfiguration();

    @Override
    public PrometheusConfiguration getPrometheusConfiguration() {
        return prometheusConfiguration;
    }
    //...
    //...
}

Assuming, the above example, configuration element is prometheus, so you can use configuration block like this in a config file:

prometheus:
  endpoint: "/prometheus"

This will expose /prometheus endpoint in admin connector, by default admin connector is exposed at port 8081 in dropwizard apps.

Assign servlet filter to the environment

To leverage latency metrics per http endpoint you need to assign servlet filter to the environment in your Application class within run method

public class ExampleApplication extends Application<ExampleConfiguration> {
    //...
    //...
    @Override
    public void run(ExampleConfiguration configuration, Environment environment) {
        //...
        //...
        FilterRegistration.Dynamic micrometerFilter = environment.servlets().addFilter("MicrometerHttpFilter", new MicrometerHttpFilter());
        micrometerFilter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");
    }
}

This way all http requests will be metered and metrics will be recorded within http_server_requests_seconds histogram with the following labels:

  • method, http method, i.e. GET, POST etc
  • status, response status, i.e. 200, 404, 500 etc
  • uri, request path

dropwizad-micrometer-jdbi

If you use JDBI to manage mapping of objects to database tables, you can utilize dropwizad-micrometer-jdbi package to meter SQL request's latencies. It depends on dropwizad-micrometer-core, so that to use dropwizad-micrometer-jdbi package the core package should be already installed and bundle should be added to your dropwizard application (see how to install it in dropwizad-micrometer-core section). Once dropwizad-micrometer-core is installed, the steps to install dropwizad-micrometer-jdbi package are:

Add dependency into your pom.xml

If you use maven, you can simply reference it in the <dependenccies> block as below. The latest version can be found on in the maven repository

 <dependencies>
     ...
     ...
     <dependency>
         <groupId>io.github.maksymdolgykh.dropwizard</groupId>
         <artifactId>dropwizard-micrometer-jdbi</artifactId>
         <version>2.0.5</version>
     </dependency>
     ...
     ...
 </dependencies>

Import MicrometerJdbiTimingCollector class in your Application class

import io.github.maksymdolgykh.dropwizard.micrometer.MicrometerJdbiTimingCollector;

Set TimingColletor for Jdbi object

To use the class you just need to set TimingColletor for Jdbi object, where it should be done depends on how your Application is organized - it might be in the run method of your Application class, or you might have separate class to configure DAO.

database.setTimingCollector(new MicrometerJdbiTimingCollector());

With this setup all jdbi requests will be metered and metrics will be recorded within jdbi_requests_seconds histogram

License

This project is licensed under the Apache License 2.0 (LICENSE)