Skip to content

Commit fa1c37d

Browse files
phipagsvozza
andauthored
docs(metrics): Add upgrade guide for re-designed Metrics utility (#1868)
* Add upgrade guide for re-designed metrics utility. Update roadmap with completed items. * Apply language suggestions. Co-authored-by: Stefano Vozza <svozza@gmail.com> --------- Co-authored-by: Stefano Vozza <svozza@gmail.com>
1 parent 57ae153 commit fa1c37d

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

docs/roadmap.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Security and operational excellence take precedence above all else. This means b
1717

1818
Our top priority is to establish the processes and infrastructure needed for a fully automated and secure end-to-end release process of new versions to Maven Central.
1919

20-
- [ ] Implement GitHub workflows and create infrastructure to release to Maven Central
20+
- [x] [Implement GitHub workflows](https://github.com/aws-powertools/powertools-lambda-java/issues/1231){target="\_blank"} and create infrastructure to release to Maven Central
2121
- [x] [Implement end-to-end tests](https://github.com/aws-powertools/powertools-lambda-java/issues/1815){target="\_blank"}
2222
- [x] Implement [OpenSSF Scorecard](https://openssf.org/projects/scorecard/){target="\_blank"}
2323

@@ -27,9 +27,10 @@ As part of a new major version `v2` release, we prioritize the Java project's co
2727

2828
##### Core Utilities
2929

30-
- [ ] [Review public interfaces and reduce public API surface area](https://github.com/aws-powertools/powertools-lambda-java/issues/1283){target="\_blank"}
30+
- [x] [Review public interfaces and reduce public API surface area](https://github.com/aws-powertools/powertools-lambda-java/issues/1283){target="\_blank"}
3131
- [x] [Release Logging `v2` module](https://github.com/aws-powertools/powertools-lambda-java/issues/965){target="\_blank"} allowing customers to choose the logging framework and adding support for logging deeply nested objects as JSON
3232
- [x] [Support high resolution metrics](https://github.com/aws-powertools/powertools-lambda-java/issues/1041){target="\_blank"}
33+
- [x] [Improve modularity of metrics module](https://github.com/aws-powertools/powertools-lambda-java/issues/1848){target="\_blank"} to remove coupling with EMF library and enable future support for additional metrics providers / backends
3334

3435
##### Ecosystem
3536

docs/upgrade.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,53 @@ public class PaymentFunction implements RequestHandler<APIGatewayProxyRequestEve
162162
## Updated Metrics utility interface
163163

164164
<!-- - Remove deprecated methods: https://github.com/aws-powertools/powertools-lambda-java/pull/1624/files#diff-0afede8005aa2baeba2770f66d611bf0e8ee3969205be27e803682a7f2d6520a -->
165+
<!-- - Re-designed metrics module: https://github.com/aws-powertools/powertools-lambda-java/issues/1848 -->
165166

166-
The Metrics utility is currently undergoing changes to the public interface as part of GitHub issue [#1848](https://github.com/aws-powertools/powertools-lambda-java/issues/1848). We will keep this upgrade guide updated with the most recent changes as soon as they are released. Stay tuned for updates!
167+
The Metrics utility was redesigned to be more modular and allow for the addition of new metrics providers in the future. The same EMF-based metrics logging still applies but will be called via an updated public interface. Consider the following list to understand some of changes:
168+
169+
- `#!java @Metrics` was renamed to `#!java @FlushMetrics`
170+
- `#!java MetricsLogger.metricsLogger()` was renamed to `#!java MetricsFactory.getMetricsInstance()`
171+
- `put*` calls such as `#!java putMetric()` where replaced with `add*` nomenclature such as `#!java addMetric()`
172+
- All direct imports from `software.amazon.cloudwatchlogs.emf` need to be replaced with Powertools counterparts from `software.amazon.lambda.powertools.metrics` (see example below)
173+
- The `withSingleMetric` and `withMetricsLogger` methods were removed in favor of `#!java metrics.flushSingleMetric()`
174+
- It is no longer valid to skip declaration of a namespace. If no namespace is provided, an exception will be raised instead of using the default `aws-embedded-metrics` namespace.
175+
176+
The following example shows a common Lambda handler using the Metrics utility and required refactorings.
177+
178+
```diff
179+
// Metrics is not a decorator anymore but the replacement for the `MetricsLogger` Singleton
180+
import software.amazon.lambda.powertools.metrics.Metrics;
181+
+ import software.amazon.lambda.powertools.metrics.FlushMetrics;
182+
- import software.amazon.lambda.powertools.metrics.MetricsUtils;
183+
+ import software.amazon.lambda.powertools.metrics.MetricsFactory;
184+
- import software.amazon.cloudwatchlogs.emf.logger.MetricsLogger;
185+
- import software.amazon.cloudwatchlogs.emf.model.DimensionSet;
186+
- import software.amazon.cloudwatchlogs.emf.model.Unit;
187+
+ import software.amazon.lambda.powertools.metrics.model.DimensionSet;
188+
+ import software.amazon.lambda.powertools.metrics.model.MetricUnit;
189+
190+
public class MetricsEnabledHandler implements RequestHandler<Object, Object> {
191+
192+
// This is still a Singleton
193+
- MetricsLogger metricsLogger = MetricsUtils.metricsLogger();
194+
+ Metrics metrics = MetricsFactory.getMetricsInstance();
195+
196+
@Override
197+
- @Metrics(namespace = "ExampleApplication", service = "booking")
198+
+ @FlushMetrics(namespace = "ExampleApplication", service = "booking")
199+
public Object handleRequest(Object input, Context context) {
200+
- metricsLogger.putDimensions(DimensionSet.of("environment", "prod"));
201+
+ metrics.addDimension(DimensionSet.of("environment", "prod"));
202+
// New method overload for adding 2D dimensions more conveniently
203+
+ metrics.addDimension("environment", "prod");
204+
- metricsLogger.putMetric("SuccessfulBooking", 1, Unit.COUNT);
205+
+ metrics.addMetric("SuccessfulBooking", 1, MetricUnit.COUNT);
206+
...
207+
}
208+
}
209+
```
210+
211+
Learn more about the redesigned Metrics utility in the [Metrics documentation](./core/metrics.md).
167212

168213
## Deprecated capture mode related `@Tracing` annotation parameters
169214

0 commit comments

Comments
 (0)