dropwizard-header-metric

Create metrics and send it to the preferable reporter if request header matches any of the headers configured to be measured.

License

License

The MIT License
Categories

Categories

DropWizard Container Microservices
GroupId

GroupId

com.github.dikhan
ArtifactId

ArtifactId

dropwizard-header-metric
Last Version

Last Version

3.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

dropwizard-header-metric
Create metrics and send it to the preferable reporter if request header matches any of the headers configured to be measured.
Project URL

Project URL

https://github.com/dikhan/dropwizard-header-metric
Source Code Management

Source Code Management

https://github.com/dikhan/dropwizard-header-metric.git

Download dropwizard-header-metric

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.dikhan/dropwizard-header-metric/ -->
<dependency>
    <groupId>com.github.dikhan</groupId>
    <artifactId>dropwizard-header-metric</artifactId>
    <version>3.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.dikhan/dropwizard-header-metric/
implementation 'com.github.dikhan:dropwizard-header-metric:3.0.0'
// https://jarcasting.com/artifacts/com.github.dikhan/dropwizard-header-metric/
implementation ("com.github.dikhan:dropwizard-header-metric:3.0.0")
'com.github.dikhan:dropwizard-header-metric:jar:3.0.0'
<dependency org="com.github.dikhan" name="dropwizard-header-metric" rev="3.0.0">
  <artifact name="dropwizard-header-metric" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.dikhan', module='dropwizard-header-metric', version='3.0.0')
)
libraryDependencies += "com.github.dikhan" % "dropwizard-header-metric" % "3.0.0"
[com.github.dikhan/dropwizard-header-metric "3.0.0"]

Dependencies

compile (11)

Group / Artifact Type Version
io.dropwizard : dropwizard-core jar
io.dropwizard : dropwizard-jersey jar
io.dropwizard : dropwizard-configuration jar
io.dropwizard : dropwizard-metrics-graphite jar
io.dropwizard.metrics : metrics-core jar
javax.ws.rs : javax.ws.rs-api jar 2.0.1
commons-io : commons-io jar 2.5
org.slf4j : slf4j-api jar
junit : junit jar
org.assertj : assertj-core jar
io.dropwizard : dropwizard-testing jar

test (3)

Group / Artifact Type Version
io.federecio : dropwizard-junit jar 0.6
com.jayway.restassured : rest-assured jar 1.7.2
org.mockito : mockito-all jar 1.9.5

Project Modules

There are no modules declared in this project.

DropWizard Header Metrics Build Status

Would you like to track down certain headers from the consumers of your service or even know who is calling your service and be able to report the usage to many different external services via DropWizard supported reporters such as console, csv, slf4j, graphite, ganglia, etc? If the answer is YES, then you are in the right place :)

This bundle lets you create metrics (based on expected headers) and send it to the preferable reporter if the request header matches any of the headers configured to be measured. Internally, for every match, the Counter metric associated with the header in question will be increased and depending upon the reporters frequency the metric will eventually get pushed to the configured reporting services.

Take a look at the 'How to use it' section to check out how easy it is to integrate this module in your application and start tracking all the headers you wish!

License | version | Build Status

How to use it

  • Include the following dependency in the the Maven pom file (check out the versions available in Maven Central)
<dependency>
    <groupId>com.github.dikhan</groupId>
    <artifactId>dropwizard-header-metric</artifactId>
    <version>${dropwizard-header-metric.version}</version>
</dependency>

All versions available in Maven Central can be seen at Maven Repo1

  • Add the @TraceConfiguredHeaders annotation (providing a value to the property 'name') to the end points you would like to trace the expected headers:
    @GET
    @TraceConfiguredHeaders(name="sayHelloWorld")
    public String sayHelloWorld() {
        return "Hello World";
    }

The annotation supports the configuration of a name. This value will be used to register the metrics and be able to identify what exact endpoint which received the expected headers.

  • Add the following to your DropWizard Configuration class:
public class YourApplicationConfiguration extends Configuration {

    @JsonProperty("traceHeaders")
    private TraceHeadersBundleConfiguration traceHeadersBundleConfiguration;

    public TraceHeadersBundleConfiguration getTraceHeadersBundleConfiguration() {
        return traceHeadersBundleConfiguration;
    }

    public void setTraceHeadersBundleConfiguration(TraceHeadersBundleConfiguration traceHeadersBundleConfiguration) {
        this.traceHeadersBundleConfiguration = traceHeadersBundleConfiguration;
    }
}

  • Update DropWizard's application yml configuration file:

Both properties (headersToTraceJson and metricPrefix) are optional and can be left empty, though the empty String "" needs to be set. The headersToTraceJson property value has to be properly formatted as Json object and the double quotes need to be escaped. The following structures are supported: 0. Empty Json object: {} No headers will be tracked in this case

  1. Single key value: {"y-custom-header": "y-custom-header-value1"}
  2. Key with multiple values: {"x-custom-header": ["x-custom-header-value1", "x-custom-header-value2"]}
traceHeaders:
  headersToTraceJson: "{\"x-custom-header\": [\"x-custom-header-value1\", \"x-custom-header-value2\"], \"y-custom-header\": \"y-custom-header-value1\"}"    
  metricPrefix: HeaderMetricPrefix
  • In your Application class:
    @Override
    public void initialize(Bootstrap<TraceHeadersApplicationConfiguration> bootstrap) {
        MultivaluedMap<String, String> headersToMeasure = getHeadersToMeasure();
        bootstrap.addBundle(new TraceHeadersBundle<TraceHeadersApplicationConfiguration>(headersToMeasure, bootstrap.getMetricRegistry()) {
            @Override
            protected TraceHeadersBundleConfiguration getTraceHeadersBundleConfiguration(TraceHeadersApplicationConfiguration configuration) {
                return configuration.getTraceHeadersBundleConfiguration();
            }
        });
    }

Examples

The library contains a sample application already set up to work with the bundle. The application yml file is configured to trace certain headers and the HelloWorldResource @Path("/hello-world") @GET sayHelloWorld() endpoint is annotated with the custom annotation thus anytime the endpoint is called with some of the headers configured we should see the specific header counter increased.

For info purposes, the applicaiton will log on start up the headers that are registered per end point. The following shows a sneak peak of the print out:

INFO  [2017-02-01 11:39:35,090] com.github.dikhan.dropwizard.headermetric.features.HeaderMetricFeature: New Header Metric registered -> com.github.dikhan.dropwizard.headermetric.resources.HelloWorldResource.sayHelloWorld.HeaderMetric.x-custom-header.x-custom-header-value1
INFO  [2017-02-01 11:39:35,090] com.github.dikhan.dropwizard.headermetric.features.HeaderMetricFeature: New Header Metric registered -> com.github.dikhan.dropwizard.headermetric.resources.HelloWorldResource.sayHelloWorld.HeaderMetric.x-custom-header.x-custom-header-value2
INFO  [2017-02-01 11:39:35,090] com.github.dikhan.dropwizard.headermetric.features.HeaderMetricFeature: New Header Metric registered -> com.github.dikhan.dropwizard.headermetric.resources.HelloWorldResource.sayHelloWorld.HeaderMetric.y-custom-header.y-custom-header-value1

The structure of the composed metric name is as follows: package-name.resource-class.end-point-with-annotation.[prefix].header-name.header-value

The prefix is optional and can be specified in the yml file.

The sample application is also configured to use the 'console' reporter, so we are able to see what metrics are reported from time to time depending upon the frequency value. Below is one of the print outs in the console:

-- Counters --------------------------------------------------------------------
com.github.dikhan.dropwizard.headermetric.resources.HelloWorldResource.sayHelloWorld.HeaderMetric.x-custom-header.x-custom-header-value1 count = 0
com.github.dikhan.dropwizard.headermetric.resources.HelloWorldResource.sayHelloWorld.HeaderMetric.x-custom-header.x-custom-header-value2 count = 0
com.github.dikhan.dropwizard.headermetric.resources.HelloWorldResource.sayHelloWorld.HeaderMetric.y-custom-header.y-custom-header-value1 count = 0

If we were to perform a GET request to the end point annotated with @TraceConfiguredHeaders(name="sayHelloWorld") passing in for instance the header x-custom-header with value x-custom-header-value1. Then we should expect the counter of the given endpoint/header to be incresed by one.

GET /hello-world HTTP/1.1
HOST: localhost:<SERVER_PORT>
x-custom-header: X-CUSTOM-HEADER-VALUE1

And the reporter in use should capture the change and in the sample app case the logs should show:

-- Counters --------------------------------------------------------------------
com.github.dikhan.dropwizard.headermetric.resources.HelloWorldResource.sayHelloWorld.HeaderMetric.x-custom-header.x-custom-header-value1 count = 1

Contributing

  • Fork it!
  • Create your feature branch: git checkout -b my-new-feature
  • Commit your changes: git commit -am 'Add some feature'
  • Push to the branch: git push origin my-new-feature
  • Submit a pull request :D

Authors

Daniel I. Khan Ramiro - Cisco Systems

See also the list of contributors who participated in this project.

Acknowledgements:

Versions

Version
3.0.0
2.0.6
2.0.5
2.0.4
2.0.3
2.0.2
1.3
1.0.4