Support for Dropwizard metric names with encoded tags

The Metrics library (v4.x) does not natively support tags, however tags can be encoded as part of the metric name. This library offers a modified MetricName class for converting tagged metrics to the legay format.

License

License

Categories

Categories

DropWizard Container Microservices Metrics Application Testing & Monitoring Monitoring Jersey Program Interface REST Frameworks
GroupId

GroupId

de.peetzen.dropwizard.metrics
ArtifactId

ArtifactId

metrics-tagging-jersey2
Last Version

Last Version

1.0.2
Release Date

Release Date

Type

Type

pom.sha512
Description

Description

Support for Dropwizard metric names with encoded tags
The Metrics library (v4.x) does not natively support tags, however tags can be encoded as part of the metric name. This library offers a modified MetricName class for converting tagged metrics to the legay format.
Project URL

Project URL

https://github.com/peetzen/dropwizard-metrics-tagging-jersey2
Source Code Management

Source Code Management

https://github.com/peetzen/dropwizard-metrics-tagging-jersey2

Download metrics-tagging-jersey2

Dependencies

compile (1)

Group / Artifact Type Version
de.peetzen.dropwizard.metrics : metrics-tagging jar 1.0.1

runtime (3)

Group / Artifact Type Version
io.dropwizard.metrics : metrics-annotation jar 4.0.0
io.dropwizard.metrics : metrics-core jar 4.0.0
org.glassfish.jersey.core : jersey-server jar 2.25.1

Project Modules

There are no modules declared in this project.

Tagging support for Dropwizard Jersey 2 Metrics

CircleCI Maven Central License

Dropwizard Dropwizard Dropwizard

The Metrics project from Dropwizard does not natively support tags in version v4.x. However, tags can be encoded as part of the metric name. This library extends the Metrics Jersey 2 offering and provides support for it.

Documentation

The Metrics project comes with built-in support for collecting useful metrics for exposed JAX-RS resource methods. In certain use cases it might be helpful or even necessary to expose more granular default metrics, for example by adding a tenant dimension. This can be achieved by implementing custom metrics, but comes with quite some boiler plate code.

This library tries to combine the power of Dropwizard`s default Metrics annotations with a flexible approach to add tags dynamically that are automatically evaluated by the Jersey server framework.

Dependencies

The implementation is based on io.dropwizard.metrics:metrics-jersey2:4.1.12.1. It is compatible with Metrics since version v4.0.0, bundled with Dropwizard since version v1.3.0.

Getting started

The artifacts including source and binaries are available on the central Maven repositories.

For maven:

<dependency>
  <groupId>de.peetzen.dropwizard.metrics</groupId>
  <artifactId>metrics-tagging-jersey2</artifactId>
  <version>1.0.2</version>
</dependency>

For gradle:

implementation group: 'de.peetzen.dropwizard.metrics', name: 'metrics-tagging-jersey2', version: '1.0.2'

Usage

The MetricDynamicTaggingFeature needs to be registered with the Jersey environment and afterwards tags can be added to the default metrics using static MetricTaggingContext#put(..) from within the resource methods.

To expose metrics, the resource methods need to be annotated with at least one of the Metrics annotations

  • @Metered
  • @Timed
  • @ExceptionMetered
  • @ResponseMetered

The collected metrics of a @Timed annotated resource method can be controlled using the MetricDynamicTaggingFeature constructors, analogue to the built-in MetricsFeature.

To differentiate between the default metrics and the ones with tags, a suffix is added to the metric names. The default is tagged and can be controlled using MetricDynamicTaggingFeature constructors.

If no tags are present, nothing will be captured. In this case the MetricFeature metrics are equivalent.

Dropwizard Example

Register the Jersey feature MetricDynamicTaggingFeature within your Dropwizard application.

public class MyApplication extends Application<MyConfiguration> {

    @Override
    public void run(MyConfiguration configuration, Environment environment) {
        doOtherInitialisations();
        
        // register feature to support dynamic tags using the dropwizard default annotation names
        environment.jersey().register(new MetricsDynamicTaggingFeature(environment.metrics()));
    }
}

Add dynamic tags within resource implementation using MetricTaggingContext#put(..).

@Path("/")
@Produces(MediaType.APPLICATION_JSON)
class MyResource {

    @GET
    @Timed
    @ExceptionMetered
    @ResponseMetered
    Response getStatus() {
    
        // add dynamic "tenant" tag that will be part of tagged resource metrics
        MetricTaggingContext.put("tenant", getTenant());
        
        doSomething();
        
        return Response.ok("{\"status\":\"active\"}").build();
    }
}

Exposed Metrics

Classic metrics:

  • my.package.MyResource.getStatus
  • my.package.MyResource.getStatus.request.filtering
  • my.package.MyResource.getStatus.response.filtering
  • my.package.MyResource.getStatus.total
  • my.package.MyResource.getStatus.Xxx-responses (1xx, .. 5xx)
  • my.package.MyResource.getStatus.exceptions

Additional tagged metrics (using default MetricsDynamicTaggingFeature configuration):

  • my.package.MyResource.getStatus.tagged[tenant:tenant_id]
  • my.package.MyResource.getStatus.tagged.Xxx-responses[tenant:tenant_id] (1xx, .. 5xx)
  • my.package.MyResource.getStatus.tagged.exceptions[tenant:tenant_id]

Versions

Version
1.0.2
1.0.1
1.0.0