https://github.com/isopropylcyanide/async-metrics-codahale
Leverage Codahale metrics (or any Metric Registry for that matter) to provide metrics on methods that complete asynchronously.
https://github.com/isopropylcyanide/async-metrics-codahale
aop aspectj async codahale-metrics completable-future dropwizard-metrics guice guice-aop java-8
Last synced: 7 months ago
JSON representation
Leverage Codahale metrics (or any Metric Registry for that matter) to provide metrics on methods that complete asynchronously.
- Host: GitHub
- URL: https://github.com/isopropylcyanide/async-metrics-codahale
- Owner: isopropylcyanide
- License: apache-2.0
- Created: 2019-09-29T20:14:27.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-10-13T16:22:59.000Z (almost 5 years ago)
- Last Synced: 2025-01-12T00:11:25.568Z (9 months ago)
- Topics: aop, aspectj, async, codahale-metrics, completable-future, dropwizard-metrics, guice, guice-aop, java-8
- Language: Java
- Homepage:
- Size: 40 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Async Metrics Using Guice AOP



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`:```xml
com.github.isopropylcyanide
async-codahale-metrics
1.0
```
## Features| Annotation | Codahale Equivalent |
| ------------- | ------------- |
| @AsyncTimed | @Timed |
| @AsyncMetered | @Meter |
| @AsyncExceptionMetered | @ExceptionMetered |## Usage
- Initialise the Module in your Guice Injector
```java
...
MetricRegistry registry = new MetricRegistry();
Guice.createInjector(new AsyncMetricsModule(metricRegistry));
...
```- Annotate the required methods with the required annotation
```java
@AsyncTimed
@AsyncExceptionMetered
CompletableFuture asyncMethodThatCompletesNormally() {
return CompletableFuture
.supplyAsync(() -> StringUtils.split(getClass().getName(), "."))
.thenApply(s -> s.length);
}
``````java
@AsyncMetered
CompletableFuture asyncMethodThatCompletesNormally() {
return CompletableFuture.supplyAsync(() -> "Hello World");
}
```## Why another metrics library?
We wish to leverage [`Dropwizard metrics`](https://github.com/dropwizard/metrics/tree/4.1-development/metrics-annotation/src/main/java/com/codahale/metrics/annotation) 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 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```java
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](https://github.com/isopropylcyanide/async-metrics-codahale/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.