|
| 1 | +# Annotations |
| 2 | + |
| 3 | +## Setup |
| 4 | + |
| 5 | +Getting this package to work requires the following additions to a gradle build file: |
| 6 | +```gradle |
| 7 | +plugins { |
| 8 | + ... |
| 9 | + id "io.freefair.aspectj.post-compile-weaving" version "6.4.3" |
| 10 | +} |
| 11 | +
|
| 12 | +dependencies { |
| 13 | + ... |
| 14 | + implementation project(':annotations') |
| 15 | + implementation "org.aspectj:aspectjweaver:1.9.8.RC3" |
| 16 | +} |
| 17 | +
|
| 18 | +``` |
| 19 | + |
| 20 | +## Usage |
| 21 | + |
| 22 | +This pacakge adds `@CountMetric` and `@ExecutionTimeMetric` method annotations. |
| 23 | + |
| 24 | +### @CountMetric |
| 25 | +@CountMetric will log a value of 1 whenever it is triggered. |
| 26 | + |
| 27 | +### @ExecutionTimeMetric |
| 28 | +@ExecutionTimeMetric will log the execution time of a method whenever it is triggered. |
| 29 | + |
| 30 | +### Annotation Parameters |
| 31 | + |
| 32 | +- `String Name` (Default: `""`) |
| 33 | + - The name the metric is published under |
| 34 | + - If the name is left null it will be replaced with `"<ClassName>.<MethodName>.<AnnotationType>"` when being sent to CWL |
| 35 | +- `Boolean logSuccess` (Default `True` ) |
| 36 | + - Determines if this annotation will log a metric when the method exits successfully |
| 37 | +- `Class<? extends Throwable>[] LogErrors` (Default: `[Throwable.class]` ) |
| 38 | + - Determines if this annotation will log a metric when the method exits with an error (will log if the method exits by a throwing an in the given list) |
| 39 | +- `String Logger` (Default: `""`) |
| 40 | + - Determines which logger this annotation’s metric will be put into. |
| 41 | + - If `null` or there is no logger associated with that key then the default logger will be used |
| 42 | +- `bool flush` (Default: `false` ) |
| 43 | + - Determines whether the metric logger attached to this annotation should flush after this function |
| 44 | + |
| 45 | + |
| 46 | +### Loggers |
| 47 | +This package also implements a singleton `MetricAnnotationMediator` which handles the logging of all metrics created by annotations. This singleton contains a default `MetricsLogger` instance that all annotations will default to; however, other loggers can be added to it using its `addLogger(name, logger)` method. Annotations can then be sent to the added logger by specifying the name of the logger as an annotation parameter. |
| 48 | + |
| 49 | +There are two ways to flush these metrics to CloudWatch: |
| 50 | +1. Call `MetricAnnotationMediator.flushAll()` which flushes all the loggers that the singleton has a reference to |
| 51 | +2. Flush the logger that the metrics have been added to. Loggers can be retrieved from the `MetricAnnotationMediator` using the methods `MetricAnnotationMediator.getDefaultLogger()` and `MetricAnnotationMediator.getLogger(name)` |
| 52 | + |
| 53 | +```java |
| 54 | +import software.amazon.cloudwatchlogs.emf.annotations.CountMetric; |
| 55 | +import software.amazon.cloudwatchlogs.emf.annotations.ExecutionTimeMetric; |
| 56 | +import software.amazon.cloudwatchlogs.emf.annotations.MetricAnnotationMediator; |
| 57 | + |
| 58 | +class Example { |
| 59 | + |
| 60 | + @CountMetric |
| 61 | + @ExecutionTimeMetric |
| 62 | + public static void doSomething() { |
| 63 | + // Do something |
| 64 | + ... |
| 65 | + } |
| 66 | + |
| 67 | + public static void main(String[] args) { |
| 68 | + MetricsLogger metrics = new MetricsLogger(); |
| 69 | + |
| 70 | + doSomething(); |
| 71 | + |
| 72 | + MetricAnnotationMediator.flushAll(); |
| 73 | + } |
| 74 | +} |
| 75 | +``` |
0 commit comments