Skip to content

Commit b4094f2

Browse files
committed
Added a readme to annotations
1 parent 6a83a64 commit b4094f2

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,12 @@ config.setShouldWriteToStdout(true);
391391
AWS_EMF_WRITE_TO_STDOUT="true"
392392
```
393393

394+
## Annotations
395+
396+
This library includes a package implmenting annotations that log metrics about method behaviour. This package was seperated so that it can be optionally imported because it adds a large dependency.
397+
398+
Please see the [README](annotations/README.md) in the annotations project folder to learn more.
399+
394400
## Thread-safety
395401

396402
### Internal Synchronization

annotations/README.md

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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 as `""` 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

Comments
 (0)