Skip to content

Commit 42b0fb7

Browse files
authored
docs(metrics): clarify single metrics metadata & publish (#3189)
1 parent 16adea6 commit 42b0fb7

File tree

5 files changed

+22
-63
lines changed

5 files changed

+22
-63
lines changed

docs/core/metrics.md

+8-14
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ This has the advantage of keeping cold start metric separate from your applicati
398398

399399
You can add high-cardinality data as part of your Metrics log with the `addMetadata` method. This is useful when you want to search highly contextual information along with your metrics in your logs.
400400

401-
!!! warning
401+
!!! info
402402
**This will not be available during metrics visualization** - Use **dimensions** for this purpose
403403

404404
=== "handler.ts"
@@ -440,24 +440,18 @@ You can add high-cardinality data as part of your Metrics log with the `addMetad
440440

441441
CloudWatch EMF uses the same dimensions across all your metrics. Use `singleMetric` if you have a metric that should have different dimensions.
442442

443-
!!! info
444-
For cost-efficiency, this feature would be used sparsely since you [pay for unique metric](https://aws.amazon.com/cloudwatch/pricing). Keep the following formula in mind:
443+
Generally, using different dimensions would be an edge case since you [pay for unique metric](https://aws.amazon.com/cloudwatch/pricing).
445444

446-
**unique metric = (metric_name + dimension_name + dimension_value)**
445+
Keep the following formula in mind: `unique metric = (metric_name + dimension_name + dimension_value)`.
447446

448-
=== "Middy Middleware"
447+
=== "Single Metric"
449448

450-
```typescript hl_lines="21 23-24"
451-
--8<-- "examples/snippets/metrics/singleMetricDifferentDimsMiddy.ts"
449+
```typescript hl_lines="9"
450+
--8<-- "examples/snippets/metrics/singleMetric.ts"
452451
```
453452

454-
=== "logMetrics decorator"
455-
456-
```typescript hl_lines="16 18-19"
457-
--8<-- "examples/snippets/metrics/singleMetricDifferentDimsDecorator.ts"
458-
```
459-
460-
1. Binding your handler method allows your handler to access `this` within the class methods.
453+
1. Metadata should be added before calling `addMetric()` to ensure it's included in the same EMF blob.
454+
2. Single metrics are emitted as soon as `addMetric()` is called, so you don't need to call `publishStoredMetrics()`.
461455

462456
## Testing your code
463457

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { MetricUnit, Metrics } from '@aws-lambda-powertools/metrics';
2+
3+
const metrics = new Metrics({
4+
namespace: 'serverlessAirline',
5+
serviceName: 'orders',
6+
});
7+
8+
export const handler = async (event: { orderId: string }) => {
9+
const singleMetric = metrics.singleMetric();
10+
singleMetric.addDimension('metricType', 'business');
11+
singleMetric.addMetadata('orderId', event.orderId); // (1)!
12+
singleMetric.addMetric('successfulBooking', MetricUnit.Count, 1); // (2)!
13+
};

examples/snippets/metrics/singleMetricDifferentDimsDecorator.ts

-24
This file was deleted.

examples/snippets/metrics/singleMetricDifferentDimsMiddy.ts

-24
This file was deleted.

packages/metrics/src/Metrics.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ class Metrics extends Utility implements MetricsInterface {
718718
* export const handler = async () => {
719719
* const singleMetric = metrics.singleMetric();
720720
* // The single metric will be emitted immediately
721-
* singleMetric.addMetric('coldStart', MetricUnit.Count, 1);
721+
* singleMetric.addMetric('ColdStart', MetricUnit.Count, 1);
722722
*
723723
* // These other metrics will be buffered and emitted when calling `publishStoredMetrics()`
724724
* metrics.addMetric('successfulBooking', MetricUnit.Count, 1);

0 commit comments

Comments
 (0)