Skip to content

Commit ba09a50

Browse files
committed
Add example for high cardinality dimensions using DimensionSet.
1 parent b05634d commit ba09a50

File tree

1 file changed

+49
-3
lines changed

1 file changed

+49
-3
lines changed

docs/core/metrics.md

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ You can create metrics using `addMetric`, and manually create dimensions for all
201201
### Adding high-resolution metrics
202202

203203
You can create [high-resolution metrics](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/publishingMetrics.html#high-resolution-metrics)
204-
passing a `MetricResolution.HIGH` to the `addMetric` method:
204+
passing a `#!java MetricResolution.HIGH` to the `addMetric` method. If nothing is passed `#!java MetricResolution.STANDARD` will be used.
205205

206206
=== "HigResMetricsHandler.java"
207207

@@ -228,6 +228,52 @@ passing a `MetricResolution.HIGH` to the `addMetric` method:
228228
High-resolution metrics are data with a granularity of one second and are very useful in several situations such as telemetry, time series, real-time incident management, and others.
229229
<!-- prettier-ignore-end -->
230230

231+
### Adding dimensions
232+
233+
You can add dimensions to your metrics using the `addDimension` method. You can either pass key-value pairs or you can create higher cardinality dimensions using `DimensionSet`.
234+
235+
=== "KeyValueDimensionHandler.java"
236+
237+
```java hl_lines="3 13"
238+
import software.amazon.lambda.powertools.metrics.Metrics;
239+
import software.amazon.lambda.powertools.metrics.MetricsLogger;
240+
import software.amazon.lambda.powertools.metrics.model.MetricResolution;
241+
242+
public class MetricsEnabledHandler implements RequestHandler<Object, Object> {
243+
244+
private static final MetricsLogger metricsLogger = MetricsLoggerFactory.getMetricsLogger();
245+
246+
@Override
247+
@Metrics(namespace = "ServerlessAirline", service = "payment")
248+
public Object handleRequest(Object input, Context context) {
249+
metricsLogger.addDimension("Dimension", "Value");
250+
metricsLogger.addMetric("SuccessfulBooking", 1, MetricUnit.COUNT);
251+
}
252+
}
253+
```
254+
255+
=== "HighCardinalityDimensionHandler.java"
256+
257+
```java hl_lines="4 13-14"
258+
import software.amazon.lambda.powertools.metrics.Metrics;
259+
import software.amazon.lambda.powertools.metrics.MetricsLogger;
260+
import software.amazon.lambda.powertools.metrics.model.MetricResolution;
261+
import software.amazon.lambda.powertools.metrics.model.DimensionSet;
262+
263+
public class MetricsEnabledHandler implements RequestHandler<Object, Object> {
264+
265+
private static final MetricsLogger metricsLogger = MetricsLoggerFactory.getMetricsLogger();
266+
267+
@Override
268+
@Metrics(namespace = "ServerlessAirline", service = "payment")
269+
public Object handleRequest(Object input, Context context) {
270+
// You can add up to 30 dimensions in a single DimensionSet
271+
metricsLogger.addDimension(DimensionSet.of("Dimension1", "Value1", "Dimension2", "Value2"));
272+
metricsLogger.addMetric("SuccessfulBooking", 1, MetricUnit.COUNT);
273+
}
274+
}
275+
```
276+
231277
### Flushing metrics
232278

233279
The `@Metrics` annotation **validates**, **serializes**, and **flushes** all your metrics. During metrics validation,
@@ -236,12 +282,12 @@ not met, `IllegalStateException` or `IllegalArgumentException` exceptions will b
236282

237283
<!-- prettier-ignore-start -->
238284
!!! tip "Metric validation"
239-
- Maximum of 9 dimensions
285+
- Maximum of 30 dimensions (`Service` default dimension counts as a regular dimension)
240286
- Dimension keys and values cannot be null or empty
241287
- Metric values must be valid numbers
242288
<!-- prettier-ignore-end -->
243289

244-
If you want to ensure that at least one metric is emitted, you can pass `raiseOnEmptyMetrics = true` to the **@Metrics** annotation:
290+
If you want to ensure that at least one metric is emitted, you can pass `raiseOnEmptyMetrics = true` to the `@Metrics` annotation:
245291

246292
=== "MetricsRaiseOnEmpty.java"
247293

0 commit comments

Comments
 (0)