Skip to content

Commit 8ed72cd

Browse files
authored
docs(metrics): add documentation for AddDimensions method (#1237)
1 parent 50d614e commit 8ed72cd

5 files changed

Lines changed: 122 additions & 18 deletions

File tree

docs/core/metrics.md

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ You can use the Builder or Configure patterns in your Lambda class constructor t
174174
```
175175
### Adding dimensions
176176

177-
You can add dimensions to your metrics using **`AddDimension`** method.
177+
You can add a dimension to your metrics using the **`AddDimension`** method.
178178

179179
=== "Function.cs"
180180

@@ -185,32 +185,76 @@ You can add dimensions to your metrics using **`AddDimension`** method.
185185

186186
```json hl_lines="11 24"
187187
{
188-
"SuccessfulBooking": 1.0,
188+
"SuccessfulBooking": 1,
189189
"_aws": {
190190
"Timestamp": 1592234975665,
191191
"CloudWatchMetrics": [
192192
{
193-
"Namespace": "ExampleApplication",
194-
"Dimensions": [
195-
[
196-
"service",
197-
"Environment"
198-
]
199-
],
200-
"Metrics": [
193+
"Namespace": "ExampleApplication",
194+
"Dimensions": [
195+
[
196+
"Service",
197+
"Environment"
198+
]
199+
],
200+
"Metrics": [
201+
{
202+
"Name": "SuccessfulBooking",
203+
"Unit": "Count"
204+
}
205+
]
206+
}
207+
]
208+
},
209+
"Service": "Booking",
210+
"Environment": "Prod"
211+
}
212+
```
213+
214+
You can also add multiple dimensions at once using the **`AddDimensions`** method.
215+
216+
=== "Function.cs"
217+
218+
```csharp hl_lines="8-11"
219+
--8<-- "docs/snippets/metrics/AddingMultipleDimensions.cs:adding_multiple_dimensions"
220+
```
221+
=== "Example CloudWatch Logs excerpt"
222+
223+
```json hl_lines="11 12 25 26"
224+
{
225+
"SuccessfulBooking": 1,
226+
"_aws": {
227+
"Timestamp": 1592234975665,
228+
"CloudWatchMetrics": [
201229
{
202-
"Name": "SuccessfulBooking",
203-
"Unit": "Count"
230+
"Namespace": "ExampleApplication",
231+
"Dimensions": [
232+
[
233+
"Service",
234+
"Environment",
235+
"Region"
236+
]
237+
],
238+
"Metrics": [
239+
{
240+
"Name": "SuccessfulBooking",
241+
"Unit": "Count"
242+
}
243+
]
204244
}
205-
]
206-
}
207-
]
208-
},
209-
"service": "ExampleService",
210-
"Environment": "Prod"
245+
]
246+
},
247+
"Service": "Booking",
248+
"Environment": "Prod",
249+
"Region": "eu-west-1"
211250
}
212251
```
213252

253+
!!! info "Both methods produce the same result"
254+
`AddDimension` and `AddDimensions` both merge dimensions into the same dimension set in the EMF output. The only difference is ergonomics - multiple individual `AddDimension` calls vs. a single `AddDimensions` call with tuples.
255+
256+
The resulting CloudWatch metric is aggregated with all dimensions combined - default dimensions plus any dimensions added via either method.
257+
214258
### Flushing metrics
215259

216260
With **`MetricsAttribute`** all your metrics are validated, serialized and flushed to standard output when lambda handler completes execution or when you had the 100th metric to memory.

docs/snippets/metrics/AddingDimensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public async Task<APIGatewayProxyResponse> FunctionHandler(APIGatewayProxyReques
1313
{
1414
Metrics.AddDimension("Environment","Prod");
1515
Metrics.AddMetric("SuccessfulBooking", 1, MetricUnit.Count);
16+
...
1617
}
1718
}
1819
// --8<-- [end:adding_dimensions]
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// This file is referenced by docs/core/metrics.md
2+
// via pymdownx.snippets (mkdocs).
3+
4+
namespace AWS.Lambda.Powertools.Docs.Snippets.Metrics;
5+
6+
// --8<-- [start:adding_multiple_dimensions]
7+
using AWS.Lambda.Powertools.Metrics;
8+
9+
public class Function {
10+
11+
[Metrics(Namespace = "ExampleApplication", Service = "Booking")]
12+
public async Task<APIGatewayProxyResponse> FunctionHandler(APIGatewayProxyRequest apigProxyEvent, ILambdaContext context)
13+
{
14+
Metrics.AddDimensions(
15+
("Environment", "Prod"),
16+
("Region", "eu-west-1")
17+
);
18+
Metrics.AddMetric("SuccessfulBooking", 1, MetricUnit.Count);
19+
...
20+
}
21+
}
22+
// --8<-- [end:adding_multiple_dimensions]

libraries/tests/AWS.Lambda.Powertools.Metrics.Tests/EMFValidationTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,25 @@ public void AddDimensions_IncludesDefaultDimensions()
464464
Assert.Contains("\"dimension2\":\"2\"", result);
465465
}
466466

467+
[Trait("Category", "MetricsImplementation")]
468+
[Fact]
469+
public void AddDimension_And_AddDimensions_ProduceSingleDimensionSet()
470+
{
471+
// Act - use both AddDimension (singular) and AddDimensions (plural) together
472+
_handler.AddDimensionAndAddDimensionsTogether();
473+
474+
var result = _consoleOut.ToString();
475+
476+
// Assert - single dimension set with all keys
477+
Assert.Contains("\"Dimensions\":[[\"Service\",\"Region\",\"AZ\",\"Environment\"]]", result);
478+
479+
// Assert - check key properties without caring about dimension order
480+
Assert.Contains("\"Service\":\"testService\"", result);
481+
Assert.Contains("\"Environment\":\"prod\"", result);
482+
Assert.Contains("\"Region\":\"eu-west-1\"", result);
483+
Assert.Contains("\"AZ\":\"eu-west-1a\"", result);
484+
}
485+
467486
[Trait("Category", "MetricsImplementation")]
468487
[Fact]
469488
public void AddDefaultDimensionsAtRuntime_OnlyAppliedToNewDimensionSets()

libraries/tests/AWS.Lambda.Powertools.Metrics.Tests/Handlers/FunctionHandler.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,4 +359,22 @@ public void AddDefaultDimensionsAtRuntime()
359359

360360
Metrics.Flush();
361361
}
362+
363+
public void AddDimensionAndAddDimensionsTogether()
364+
{
365+
Metrics.SetNamespace("dotnet-powertools-test");
366+
Metrics.SetService("testService");
367+
368+
// Use AddDimension (singular) - merges into existing dimension set
369+
Metrics.AddDimension("Environment", "prod");
370+
371+
// Use AddDimensions (plural) - also merges into existing dimension set
372+
Metrics.AddDimensions(
373+
("Region", "eu-west-1"),
374+
("AZ", "eu-west-1a")
375+
);
376+
377+
Metrics.AddMetric("TestMetric", 1.0, MetricUnit.Count);
378+
Metrics.Flush();
379+
}
362380
}

0 commit comments

Comments
 (0)