Skip to content

Commit 2a60d86

Browse files
committed
docs: update metrics documentation to explain addDimensions() behavior for issue #3777
1 parent 0157499 commit 2a60d86

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

docs/features/metrics.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,18 @@ You can create metrics using the `addMetric` method, and you can create dimensio
119119
--8<-- "examples/snippets/metrics/customDimensions.ts"
120120
```
121121

122+
### Creating dimension sets
123+
124+
You can create separate dimension sets for your metrics using the `addDimensions` method. This allows you to group metrics by different dimension combinations.
125+
126+
When you call `addDimensions()`, it creates a new dimension set rather than adding to the existing dimensions. This is useful when you want to track the same metric across different dimension combinations.
127+
128+
=== "handler.ts"
129+
130+
```typescript hl_lines="9 12-15 18-21"
131+
--8<-- "examples/snippets/metrics/dimensionSets.ts"
132+
```
133+
122134
!!! tip "Autocomplete Metric Units"
123135
Use the `MetricUnit` enum to easily find a supported metric unit by CloudWatch. Alternatively, you can pass the value as a string if you already know them e.g. "Count".
124136

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 (
9+
_event: unknown,
10+
_context: unknown
11+
): Promise<void> => {
12+
// Add a single dimension
13+
metrics.addDimension('environment', 'prod');
14+
15+
// Add a new dimension set
16+
metrics.addDimensions({
17+
dimension1: '1',
18+
dimension2: '2',
19+
});
20+
21+
// Add another dimension set
22+
metrics.addDimensions({
23+
region: 'us-east-1',
24+
category: 'books',
25+
});
26+
27+
// Add metrics
28+
metrics.addMetric('successfulBooking', MetricUnit.Count, 1);
29+
metrics.publishStoredMetrics();
30+
};

0 commit comments

Comments
 (0)