Async Codahale Metrics

Leverage metrics for asynchronously returning methods

License

License

Categories

Categories

IDE Development Tools Metrics Application Testing & Monitoring Monitoring
GroupId

GroupId

com.github.isopropylcyanide
ArtifactId

ArtifactId

async-codahale-metrics
Last Version

Last Version

1.0
Release Date

Release Date

Type

Type

jar
Description

Description

Async Codahale Metrics
Leverage metrics for asynchronously returning methods
Project URL

Project URL

https://github.com/isopropylcyanide/async-metrics-codahale
Source Code Management

Source Code Management

https://github.com/isopropylcyanide/async-metrics-codahale/tree/master

Download async-codahale-metrics

How to add to project

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

Dependencies

compile (4)

Group / Artifact Type Version
org.apache.commons : commons-lang3 jar 3.7
com.codahale.metrics : metrics-core jar 3.0.2
com.google.inject : guice jar 4.2.2
junit : junit jar 4.12

test (1)

Group / Artifact Type Version
org.mockito : mockito-core jar 2.23.4

Project Modules

There are no modules declared in this project.

Async Metrics Using Guice AOP

Travis (.org) Codecov Maven Central GitHub

Leverage Codahale metrics (or any Metric Registry for that matter) to provide metrics on methods that complete asynchronously. The library requires Guice AOP to work (bundled by default)

Maven Artifacts

This project is available on Maven Central. To add it to your project you can add the following dependency to your pom.xml:

    <dependency>
        <groupId>com.github.isopropylcyanide</groupId>
        <artifactId>async-codahale-metrics</artifactId>
        <version>1.0</version>
     </dependency>

Features

Annotation Codahale Equivalent
@AsyncTimed @Timed
@AsyncMetered @Meter
@AsyncExceptionMetered @ExceptionMetered

Usage

  • Initialise the Module in your Guice Injector
    ...    
    MetricRegistry registry = new MetricRegistry();
    Guice.createInjector(new AsyncMetricsModule(metricRegistry));    
    ...
  • Annotate the required methods with the required annotation
@AsyncTimed
@AsyncExceptionMetered
CompletableFuture<Integer> asyncMethodThatCompletesNormally() {
    return CompletableFuture
            .supplyAsync(() -> StringUtils.split(getClass().getName(), "."))
           .thenApply(s -> s.length);
}
@AsyncMetered
CompletableFuture<String> asyncMethodThatCompletesNormally() {
    return CompletableFuture.supplyAsync(() -> "Hello World");
}

Why another metrics library?

We wish to leverage Dropwizard metrics for async callbacks. The likes of @Timed or @ExceptionMetered for methods that return Futures or CompletableFutures or Callbacks

These annotation for marking a method of an annotated object make the code much more readable and separate the unnecessary boilerplate of marking metrics from the business logic

@Timed                                          @Timed
@ExceptionMetered                               @ExceptionMetered                              
public X getX() {                               public Future<X> getX() {
    //business logic                                //business logic that might execute asynchronously
    return x;                                       return Future.of(x)        
}                                               }
     
  Works as expected                             Doesn't work as expected

However, the second approach won't produce correct results as if the calling thread dispatches the work to another thread in the pool then this method execution completes without waiting for the result. Ideally, we want to mark our metrics and figure out a way to do meta stuff once the callback resolves either successfully or exceptionally. That is why this library as created.

Enhancements

  • Feel free to extend this to mark any custom metrics. All you need is an annotation and the corresponding aspects
  • Instead of Codahale Metric Registry it can be extended to any registry.
  • Instead of completable future, it can be any random callback. The only requirement is of a hook to execute action post callback resolution that doesn't block.

Testing

  • Create a driver class with a new metric registry
  • Install the module
  • Set up a local Console reporter
ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics)
       .convertRatesTo(TimeUnit.SECONDS)
       .convertDurationsTo(TimeUnit.MILLISECONDS)
       .build();
   reporter.start(1, TimeUnit.SECONDS);

Wiki

https://medium.com/@aman_garg/leveraging-async-metrics-using-aspects-81838b9b887e

Support

Please file bug reports and feature requests in GitHub issues.

License

Copyright (c) 2012-2020 Aman Garg

This library is licensed under the Apache License, Version 2.0.

See http://www.apache.org/licenses/LICENSE-2.0.html or the LICENSE file in this repository for the full license text.

Versions

Version
1.0