You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
104
106
}
105
107
106
108
sourceCompatibility = 11
@@ -127,27 +129,12 @@ Metrics has three global settings that will be used across all metrics emitted.
127
129
The `Metrics` Singleton can be configured by three different interfaces. The following order of precedence applies:
128
130
129
131
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))
131
133
3. Environment variables (recommended)
132
134
133
135
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.
@@ -165,9 +152,45 @@ For most use-cases, we recommend using Environment variables and only overwrite
165
152
}
166
153
```
167
154
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.
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()`.
169
192
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)."
171
194
172
195
## Creating metrics
173
196
@@ -381,7 +404,7 @@ You can use `addMetadata` for advanced use cases, where you want to add metadata
381
404
This will not be available during metrics visualization, use Dimensions for this purpose.
382
405
383
406
!!! 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.
385
408
<!-- prettier-ignore-end -->
386
409
387
410
=== "App.java"
@@ -468,7 +491,7 @@ You can create metrics with different configurations e.g. different namespace an
@@ -504,22 +527,22 @@ You can create metrics with different configurations e.g. different namespace an
504
527
505
528
### Usage without `@FlushMetrics` annotation
506
529
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.
508
531
509
532
!!!info "The environment variables for Service and Namespace configuration still apply but can be overwritten with `MetricsBuilder` if needed."
510
533
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()`.
0 commit comments