Batch Asynchronous publisher of metrics in Cloudwatch

This is a library that can be used to publish metrics in AWS Cloudwatch in batches and asynchronously.

License

License

Categories

Categories

Metrics Application Testing & Monitoring Monitoring
GroupId

GroupId

cloud.cirrusup
ArtifactId

ArtifactId

cloudwatch-async-batch-metrics-publisher
Last Version

Last Version

1.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

Batch Asynchronous publisher of metrics in Cloudwatch
This is a library that can be used to publish metrics in AWS Cloudwatch in batches and asynchronously.

Download cloudwatch-async-batch-metrics-publisher

How to add to project

<!-- https://jarcasting.com/artifacts/cloud.cirrusup/cloudwatch-async-batch-metrics-publisher/ -->
<dependency>
    <groupId>cloud.cirrusup</groupId>
    <artifactId>cloudwatch-async-batch-metrics-publisher</artifactId>
    <version>1.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/cloud.cirrusup/cloudwatch-async-batch-metrics-publisher/
implementation 'cloud.cirrusup:cloudwatch-async-batch-metrics-publisher:1.1.0'
// https://jarcasting.com/artifacts/cloud.cirrusup/cloudwatch-async-batch-metrics-publisher/
implementation ("cloud.cirrusup:cloudwatch-async-batch-metrics-publisher:1.1.0")
'cloud.cirrusup:cloudwatch-async-batch-metrics-publisher:jar:1.1.0'
<dependency org="cloud.cirrusup" name="cloudwatch-async-batch-metrics-publisher" rev="1.1.0">
  <artifact name="cloudwatch-async-batch-metrics-publisher" type="jar" />
</dependency>
@Grapes(
@Grab(group='cloud.cirrusup', module='cloudwatch-async-batch-metrics-publisher', version='1.1.0')
)
libraryDependencies += "cloud.cirrusup" % "cloudwatch-async-batch-metrics-publisher" % "1.1.0"
[cloud.cirrusup/cloudwatch-async-batch-metrics-publisher "1.1.0"]

Dependencies

compile (6)

Group / Artifact Type Version
com.amazonaws : aws-java-sdk-cloudwatch jar 1.11.163
org.slf4j : slf4j-api jar 1.7.5
org.slf4j : slf4j-log4j12 jar 1.7.5
commons-lang : commons-lang jar 2.6
com.google.guava : guava jar 22.0
com.google.code.gson : gson jar 2.8.0

test (2)

Group / Artifact Type Version
junit : junit jar 4.8.1
org.mockito : mockito-all jar 1.9.5

Project Modules

There are no modules declared in this project.

Maven Central

AWS CloudWatch Async&Batch Metrics Publisher

Description

This library is an abstraction over AWS CloudWatch, but also it comes with 2 relevant improvements:

  • Asynchronous metrics publishing: Ideally, code orchestration (metrics and logging) must be achieved without impacting its performance. When this orchestration is done using a remote service, an implicit latency is added when this service is called. In order to avoid this, we offer 2 solutions: using blocking queue or using a file. The first one has the advantage of being simpler, while the second one ensures data integrity: If AWS CloudWatch is down for a while, metrics are going to accumulate on a file and, when CW becomes up are published.
  • Batch publishing: AWS CloudWatch changes you per number of calls you make. Any call could push up to 20 metrics or up to 40Kb of data. If the former condition is harder to achieve, the first one could bring an important cost reduction if for example we buffer multiple metrics per AWS call.

How to use it

i. Declare the dependency in your pom.xml file. cloud.cirrusup cloudwatch-async-batch-metrics-publisher 1.1.0

ii. Create a MetricsPublisher object.

Version 1.1.0 offers 2 types of MetricsPublisher:

  • QueueBasedMetricsPublisher - this publisher uses an internal queue to batch metrics. It has the advantage of being very fast, but if the call to AWS CloudWatch fails for whatever reason, all metrics in that batch are lost.
 MetricsPublisher publisher = new QueueBasedMetricsPublisher(awsCloudWatchClient,
                                        "my-service-namespace",
                                        5000);
  • FileBasedMetricsPublisher - this publisher writes metrics in a file that is rotated after each hour and from there the metrics are read, batched and send to AWS CloudWatch. If call to CW fails, metrics are send again until the call is done with success. Use this one if your application cannot afford to lose any metric.
 MetricsPublisher publisher =  new FileBasedMetricsPublisher(awsCloudWatchClient,
                                         "/tmp/cw-experiment/", 
                                         "test", 
                                         5000);

Observation: the last parameter is the maximum time interval in millis while publisher waits for the metrics. If you put this parameter very high (more than 24 hours), it could take a long time - up to 2 days - to see these metrics. So, our advice is to set this parameter high enough to accumulate some metrics, but not too big because it can delays your metrics.

iii. Create a MetricsFactory using the MetricsPublisher defined above.

 MetricsFactory metricsFactory = new AWSCloudWatchMetricsFactory(publisher);    

iv. Use the MetricsFactory in your code.

public class DemoService {

    private final MetricsFactory metricsFactory;
    private final UserDataStore dataStore;

    public DemoService(MetricsFactory metricsFactory, UserDataStore dataStore) {

        this.metricsFactory = metricsFactory;
        this.dataStore = dataStore;
    }

    public UserDetails getUserDetails(String userId) throws IOException {

        boolean success = true;
        Metric metric = metricsFactory.newMetric("GetUserDetails");
        metric.open();
        try {
            List<User> users = dataStore.getUserDetails(userId);
            metric.addMeasure("UsersReturned", users.size());
            return users;
        } catch (IOException e) {
            success = false;
            throw e;
        } finally {
            metric.addMeasure("Success", success ? 1.0 : 0.0);
            metric.addMeasure("Failure", !success ? 1.0 : 0.0);
            metric.close();
        }
    }
}

The above code produces 4 metrics for method call: duration, number of users loaded from data store, success and failure.

If you have any question or if you want to contribute, please contact us: http://www.cirrusup.cloud/contact/

Versions

Version
1.1.0
1.0.0