Skip to content

Commit 20fd055

Browse files
committed
Update metrics docs highlighting functional API.
1 parent da3e96b commit 20fd055

File tree

1 file changed

+53
-29
lines changed

1 file changed

+53
-29
lines changed

docs/core/metrics.md

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Visit the AWS documentation for a complete explanation for [Amazon CloudWatch co
4848
</dependencies>
4949
...
5050
<!-- configure the aspectj-maven-plugin to compile-time weave (CTW) the aws-lambda-powertools-java aspects into your project -->
51+
<!-- Note: This AspectJ configuration is not needed when using the functional approach -->
5152
<build>
5253
<plugins>
5354
...
@@ -89,18 +90,19 @@ Visit the AWS documentation for a complete explanation for [Amazon CloudWatch co
8990

9091
=== "Gradle"
9192

92-
```groovy hl_lines="3 11"
93+
```groovy hl_lines="3 11 12"
9394
plugins {
9495
id 'java'
95-
id 'io.freefair.aspectj.post-compile-weaving' version '8.1.0'
96+
id 'io.freefair.aspectj.post-compile-weaving' version '8.1.0' // Not needed when using the functional approach
9697
}
9798

9899
repositories {
99100
mavenCentral()
100101
}
101102

102103
dependencies {
103-
aspect 'software.amazon.lambda:powertools-metrics:{{ powertools.version }}'
104+
aspect 'software.amazon.lambda:powertools-metrics:{{ powertools.version }}' // Not needed when using the functional approach
105+
implementation 'software.amazon.lambda:powertools-metrics:{{ powertools.version }}' // Use this instead of 'aspect' when using the functional approach
104106
}
105107

106108
sourceCompatibility = 11
@@ -127,27 +129,12 @@ Metrics has three global settings that will be used across all metrics emitted.
127129
The `Metrics` Singleton can be configured by three different interfaces. The following order of precedence applies:
128130

129131
1. `@FlushMetrics` annotation
130-
2. `MetricsBuilder` using Builder pattern (see [Advanced section](#usage-without-metrics-annotation))
132+
2. `MetricsBuilder` using Builder pattern (see [Advanced section](#usage-without-flushmetrics-annotation))
131133
3. Environment variables (recommended)
132134

133135
For most use-cases, we recommend using Environment variables and only overwrite settings in code where needed using either the `@FlushMetrics` annotation or `MetricsBuilder` if the annotation cannot be used.
134136

135-
=== "template.yaml"
136-
137-
```yaml hl_lines="9 10"
138-
Resources:
139-
HelloWorldFunction:
140-
Type: AWS::Serverless::Function
141-
Properties:
142-
...
143-
Runtime: java11
144-
Environment:
145-
Variables:
146-
POWERTOOLS_SERVICE_NAME: payment
147-
POWERTOOLS_METRICS_NAMESPACE: ServerlessAirline
148-
```
149-
150-
=== "MetricsEnabledHandler.java"
137+
=== "@FlushMetrics annotation"
151138

152139
```java hl_lines="9"
153140
import software.amazon.lambda.powertools.metrics.FlushMetrics;
@@ -165,9 +152,45 @@ For most use-cases, we recommend using Environment variables and only overwrite
165152
}
166153
```
167154

168-
`Metrics` is implemented as a Singleton to keep track of your aggregate metrics in memory and make them accessible anywhere in your code. To guarantee that metrics are flushed properly the `@FlushMetrics` annotation must be added on the lambda handler.
155+
=== "MetricsBuilder"
156+
157+
```java hl_lines="7-8"
158+
import software.amazon.lambda.powertools.metrics.Metrics;
159+
import software.amazon.lambda.powertools.metrics.MetricsBuilder;
160+
161+
public class MetricsEnabledHandler implements RequestHandler<Object, Object> {
162+
163+
private static final Metrics metrics = MetricsBuilder.builder()
164+
.withNamespace("ServerlessAirline")
165+
.withService("payment")
166+
.build();
167+
168+
@Override
169+
public Object handleRequest(Object input, Context context) {
170+
// ...
171+
metrics.flush();
172+
}
173+
}
174+
```
175+
176+
=== "Environment variables"
177+
178+
```yaml hl_lines="9 10"
179+
Resources:
180+
HelloWorldFunction:
181+
Type: AWS::Serverless::Function
182+
Properties:
183+
...
184+
Runtime: java11
185+
Environment:
186+
Variables:
187+
POWERTOOLS_SERVICE_NAME: payment
188+
POWERTOOLS_METRICS_NAMESPACE: ServerlessAirline
189+
```
190+
191+
`Metrics` is implemented as a Singleton to keep track of your aggregate metrics in memory and make them accessible anywhere in your code. The `@FlushMetrics` annotation automatically flushes metrics at the end of the Lambda handler execution. Alternatively, you can use the functional approach and manually flush metrics using `metrics.flush()`.
169192

170-
!!!info "You can use the Metrics utility without the `@FlushMetrics` annotation and flush manually. Read more in the [advanced section below](#usage-without-metrics-annotation)."
193+
!!!info "Read more about the functional approach in the [advanced section below](#usage-without-flushmetrics-annotation)."
171194

172195
## Creating metrics
173196

@@ -381,7 +404,7 @@ You can use `addMetadata` for advanced use cases, where you want to add metadata
381404
This will not be available during metrics visualization, use Dimensions for this purpose.
382405

383406
!!! info
384-
Adding metadata with a key that is the same as an existing metric will be ignored
407+
Adding metadata with a key that is the same as an existing metric will be ignored.
385408
<!-- prettier-ignore-end -->
386409

387410
=== "App.java"
@@ -468,7 +491,7 @@ You can create metrics with different configurations e.g. different namespace an
468491

469492
=== "App.java"
470493

471-
```java hl_lines="12-18"
494+
```java hl_lines="12-22"
472495
import software.amazon.lambda.powertools.metrics.Metrics;
473496
import software.amazon.lambda.powertools.metrics.MetricsFactory;
474497
import software.amazon.lambda.powertools.metrics.model.DimensionSet;
@@ -504,22 +527,22 @@ You can create metrics with different configurations e.g. different namespace an
504527

505528
### Usage without `@FlushMetrics` annotation
506529

507-
The `Metrics` Singleton provides all configuration options via `MetricsBuilder` in addition to the `@FlushMetrics` annotation. This can be useful if work in an environment or framework that does not leverage the vanilla Lambda `handleRequest` method.
530+
You can use the **functional API** approach (see [usage patterns](../usage-patterns.md#functional-approach)) to work with Metrics without the `@FlushMetrics` annotation. The `Metrics` Singleton provides all configuration options via `MetricsBuilder`. This approach eliminates the AspectJ runtime dependency and is useful if you work in an environment or framework that does not leverage the vanilla Lambda `handleRequest` method.
508531

509532
!!!info "The environment variables for Service and Namespace configuration still apply but can be overwritten with `MetricsBuilder` if needed."
510533

511-
The following example shows how to configure a custom `Metrics` Singleton using the Builder pattern. Note that it is necessary to manually flush metrics now.
534+
The following example shows how to configure a custom `Metrics` Singleton using the Builder pattern. With the functional approach, you must manually flush metrics using `metrics.flush()`.
512535

513536
=== "App.java"
514537

515-
```java hl_lines="7-12 19 23"
538+
```java hl_lines="7-12 19 24"
516539
import software.amazon.lambda.powertools.metrics.Metrics;
517540
import software.amazon.lambda.powertools.metrics.MetricsBuilder;
518541
import software.amazon.lambda.powertools.metrics.model.DimensionSet;
519542
import software.amazon.lambda.powertools.metrics.model.MetricUnit;
520543

521544
public class App implements RequestHandler<Object, Object> {
522-
// Create and configure a Metrics singleton without annotation
545+
// Create and configure a Metrics singleton using the functional approach
523546
private static final Metrics metrics = MetricsBuilder.builder()
524547
.withNamespace("ServerlessAirline")
525548
.withRaiseOnEmptyMetrics(true)
@@ -533,8 +556,9 @@ The following example shows how to configure a custom `Metrics` Singleton using
533556
// Dimensions are also optional.
534557
metrics.captureColdStartMetric(context, DimensionSet.of("FunctionName", "MyFunction", "Service", "payment"));
535558

536-
// Add metrics to the custom metrics singleton
559+
// Add metrics
537560
metrics.addMetric("CustomMetric", 1, MetricUnit.COUNT);
561+
// Manually flush metrics
538562
metrics.flush();
539563
}
540564
}

0 commit comments

Comments
 (0)