AWS powertools documentation change
Summary
Added documentation for automatic metrics flushing using the 'using' keyword, updated navigation links, and added date
Security assessment
The change introduces resource management patterns but doesn't address vulnerabilities or security features. The added content focuses on operational reliability without security implications.
Diff
diff --git a/powertools/typescript/latest/features/metrics.md b/powertools/typescript/latest/features/metrics.md index f3a5cefbd..eb4325eeb 100644 --- a//powertools/typescript/latest/features/metrics.md +++ b//powertools/typescript/latest/features/metrics.md @@ -60,0 +61 @@ Features + * Using the using keyword @@ -81,0 +83 @@ Event Handler + * [ Data Masking ](../data-masking/) @@ -82,0 +85 @@ Event Handler + * [ Signer ](../signer/) @@ -124,0 +128 @@ Table of contents + * Using the using keyword @@ -1066,0 +1071 @@ You can flush metrics automatically using one of the following methods: + * the `using` keyword @@ -1072 +1077 @@ You can flush metrics automatically using one of the following methods: -Using the Middy middleware or decorator will **automatically validate, serialize, and flush** all your metrics. During metrics validation, if no metrics are provided then a warning will be logged, but no exception will be thrown. If you do not use the middleware or decorator, you have to flush your metrics manually. +Using the Middy middleware or decorator will **automatically validate, serialize, and flush** all your metrics. During metrics validation, if no metrics are provided then a warning will be logged, but no exception will be thrown. If you do not use the middleware or decorator, you have to flush your metrics manually or rely on the `using` keyword. @@ -1379,0 +1385,100 @@ handler.tsExample CloudWatch Logs excerpt +---|--- + +#### Using the `using` keyword¶ + +If you are running on Node.js 24 or newer, the `Metrics` class implements the [`Disposable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/dispose) interface. This means you can declare your instance with the [`using`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/using) keyword and have your metrics flushed automatically when the binding goes out of scope, including when an exception is thrown. This is a lightweight alternative to wrapping your handler in a `try/finally` block. + +Warning + +Metrics, dimensions and namespace validation still applies. + +handler.tsExample CloudWatch Logs excerpt + + + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15 + +| + + + import { Metrics, MetricUnit } from '@aws-lambda-powertools/metrics'; + + const metrics = new Metrics({ + namespace: 'serverlessAirline', + serviceName: 'orders', + }); + + export const handler = async ( + _event: unknown, + _context: unknown + ): Promise<void> => { + using _ = metrics; // (1)! + metrics.addMetric('successfulBooking', MetricUnit.Count, 10); + // metrics are flushed automatically when the scope exits, including on errors + }; + + +---|--- + + 1. The metrics are flushed automatically via `[Symbol.dispose]()` when the binding leaves the handler scope. + + + + + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19 + +| + + + { + "successfulBooking": 1.0, + "_aws": { + "Timestamp": 1592234975665, + "CloudWatchMetrics": [ + { + "Namespace": "successfulBooking", + "Dimensions": [["service"]], + "Metrics": [ + { + "Name": "successfulBooking", + "Unit": "Count" + } + ] + } + ] + }, + "service": "orders" + } + + @@ -1906 +2011 @@ Spying on emitted metrics -2026-05-27 +2026-07-10