CodaHale Aggregated Metrics CloudWatch Reporter

This is a CloudWatch Reporter for the stable version of Dropwizard Metrics (formerly CodaHale & Yammer Metrics). The reporter is an implementation of ScheduledReporter from Dropwizard Metrics v4.0.2

Table of Contents

Prerequisites

AWS Java SDK v1 and v2

The master branch is now on AWS Java SDK v2.x.x. If you want to submit pull requests for v1.x.x, please use com_amazonaws_aws_cloudwatch_1.x.x branch.

Summary

Reportable Metrics

Currently the only metric values that are reportable through configuration are:

Please note:

Metric Dimensions

A dimension is a name/value pair that helps you to uniquely identify a metric. Every metric has specific characteristics that describe it, and you can think of dimensions as categories for those characteristics.

Global Dimensions

The reporter can be configured with a set of global dimensions. These will be added to all reported metrics.

Per Metric Dimensions

The reporter will look for dimensions encoded in the metric name when the metric is reported. To add dimensions to a metric, encode them inside square brackets at the end of the metric name;

"metricName[dimension1:value1,dimension2:value2]"

Use DimensionedName to more easily create metric names with dimensions;

final DimensionedName dimensionedName = DimensionedName.withName("test")
        .withDimension("key1", "val1")
        .withDimension("key2", "val2")
        .withDimension("key3", "val3")
        .build();

metricRegistry.counter(dimensionedName.encode()).inc();

It's also possible to derive a new DimensionedName from an existing one;

final DimensionedName dimensionedName = DimensionedName
        .withName("test")
        .withDimension("key1", "val1")
        .withDimension("key2", "val2")
        .withDimension("key3", "val3")
        .build();

final DimensionedName derivedDimensionedName = dimensionedName
        .withDimension("key3", "replaced_value")
        .withDimension("key4", "val4")
        .build();

metricRegistry.counter(dimensionedName.encode()).inc();
metricRegistry.counter(derivedDimensionedName.encode()).inc();

Since DimensionedName is immutable, the string representation returned by encode() is cached. Multiple calls to encode() will return the same String.

Defaults

The Reporter uses the following defaults which can be configured:

Type Metric Name
1-min-mean-rate [per-second] com.example.component.SomeComponent.timer
snapshot-mean [in-milliseconds] com.example.component.SomeComponent.timer
snapshot-mean com.example.component.SomeComponent.histogram
95% com.example.component.SomeComponent.timer
99.5% com.example.component.SomeComponent.timer
99.5% com.example.component.SomeComponent.histogram
count com.example.component.SomeComponent.counter

The only metrics that are reportable by default are:

All other metrics have to be configured for reporting by invoking their respective withXXXX() methods on the CloudWatchReporter.Builder instance

Adding cloudwatch reporter to your project

The library artifact ID is dropwizard-metrics-cloudwatch if you want to search for it on Maven Central

The following is an example how to include the library in your project using Gradle:

repositories {
    mavenCentral()
}

dependencies { 
    compile("io.github.azagniotov:dropwizard-metrics-cloudwatch:2.0.5")
}

Usage

The reporter provides a fine-grained configuration options through its builder to configure what metrics should be reported to CloudWatch. Since AWS costs money, you probably do not want to report all the values from Metric classes or Snapshot, but only what's really useful to you.

    final CloudWatchAsyncClient amazonCloudWatchAsync =
            CloudWatchAsyncClient
                    .builder()
                    .region(Region.US_WEST_2)
                    .build();

    final CloudWatchReporter cloudWatchReporter =
            CloudWatchReporter.forRegistry(metricRegistry, amazonCloudWatchAsync, Main.class.getName())
                    .convertRatesTo(TimeUnit.SECONDS)
                    .convertDurationsTo(TimeUnit.MILLISECONDS)
                    .filter(MetricFilter.ALL)
                    .withPercentiles(Percentile.P75, Percentile.P99)
                    .withOneMinuteMeanRate()
                    .withFiveMinuteMeanRate()
                    .withFifteenMinuteMeanRate()
                    .withMeanRate()
                    .withArithmeticMean()
                    .withStdDev()
                    .withStatisticSet()
                    .withZeroValuesSubmission()
                    .withReportRawCountValue()
                    .withHighResolution()
                    .withMeterUnitSentToCW(StandardUnit.BYTES)
                    .withJvmMetrics()
                    .withGlobalDimensions("Region=us-west-2", "Instance=stage")
                    .withDryRun()
                    .build();

    cloudWatchReporter.start(10, TimeUnit.SECONDS);

Dry run

The reporter can be configured to run in DRY RUN mode by invoking .withDryRun() on the Builder. In that case, the reporter will log.DEBUG the created instance of PutMetricDataRequest (AWS), instead of doing a real POST to CloudWatch.

Credits

Changelog

2.0.5

2.0.4

2.0.3

2.0.2

2.0.1

2.0.0

1.0.13

1.0.12

1.0.11

1.0.10

1.0.9

1.0.8

1.0.7

1.0.6

1.0.5

1.0.4

1.0.3

1.0.2

1.0.1

1.0.0

License

MIT