Skip to content

Commit 05700f6

Browse files
reyangcijothomas
andauthored
[metrics] Document view level cardinality limit (#5321)
Co-authored-by: Cijo Thomas <[email protected]>
1 parent 7527782 commit 05700f6

File tree

4 files changed

+33
-15
lines changed

4 files changed

+33
-15
lines changed

docs/metrics/README.md

+2-7
Original file line numberDiff line numberDiff line change
@@ -356,18 +356,13 @@ predictable and reliable behavior when excessive cardinality happens, whether it
356356
was due to a malicious attack or developer making mistakes while writing code.
357357

358358
OpenTelemetry has a default cardinality limit of `2000` per metric. This limit
359-
can be configured at `MeterProvider` level using
359+
can be configured at `MeterProvider` level using the
360360
`SetMaxMetricPointsPerMetricStream` method, or at individual
361361
[view](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#view)
362-
level. Refer to this
362+
level using `MetricStreamConfiguration.CardinalityLimit`. Refer to this
363363
[doc](../../docs/metrics/customizing-the-sdk/README.md#changing-maximum-metricpoints-per-metricstream)
364364
for more information.
365365

366-
> [!NOTE]
367-
> Setting cardinality limit per view is not yet implemented in OpenTelemetry
368-
.NET. You can track the progress by following this
369-
[issue](https://github.com/open-telemetry/opentelemetry-dotnet/issues/5296).
370-
371366
Given a metric, once the cardinality limit is reached, any new measurement which
372367
cannot be independently aggregated because of the limit will be aggregated using
373368
the [overflow

docs/metrics/customizing-the-sdk/README.md

+10-4
Original file line numberDiff line numberDiff line change
@@ -444,10 +444,16 @@ AnotherFruitCounter.Add(5, new("name", "banana"), new("color", "yellow")); // Ex
444444
AnotherFruitCounter.Add(4, new("name", "mango"), new("color", "yellow")); // Not exported
445445
```
446446

447-
> [!NOTE]
448-
> The above limit is *per* metric stream, and applies to all the metric
449-
streams. There is no ability to apply different limits for each instrument at
450-
this moment.
447+
To set the [cardinality limit](../README.md#cardinality-limits) at individual
448+
metric level, use `MetricStreamConfiguration.CardinalityLimit`:
449+
450+
```csharp
451+
var meterProvider = Sdk.CreateMeterProviderBuilder()
452+
.AddMeter("MyCompany.MyProduct.MyLibrary")
453+
.AddView(instrumentName: "MyFruitCounter", new MetricStreamConfiguration { CardinalityLimit = 10 })
454+
.AddConsoleExporter()
455+
.Build();
456+
```
451457

452458
### Exemplars
453459

docs/metrics/getting-started-console/Program.cs

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ public static void Main()
1717
.AddConsoleExporter()
1818
.Build();
1919

20+
// In this example, we have low cardinality which is below the 2000
21+
// default limit. If you have high cardinality, you need to set the
22+
// cardinality limit properly.
2023
MyFruitCounter.Add(1, new("name", "apple"), new("color", "red"));
2124
MyFruitCounter.Add(2, new("name", "lemon"), new("color", "yellow"));
2225
MyFruitCounter.Add(1, new("name", "lemon"), new("color", "yellow"));

docs/metrics/getting-started-console/README.md

+18-4
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ MyFruitCounter.Add(2, new("name", "lemon"), new("color", "yellow"));
6969
MyFruitCounter.Add(1, new("name", "lemon"), new("color", "yellow"));
7070
```
7171

72-
An OpenTelemetry
73-
[MeterProvider](#meterprovider)
74-
is configured to subscribe to instruments from the Meter
75-
`MyCompany.MyProduct.MyLibrary`, and aggregate the measurements in-memory. The
72+
An OpenTelemetry [MeterProvider](#meterprovider) is configured to subscribe to
73+
an instrument named "MyFruitCounter" from the Meter
74+
`MyCompany.MyProduct.MyLibrary`, and aggregate the measurements in-memory with a
75+
default [cardinality limit](../README.md#cardinality-limits) of `2000`. The
7676
pre-aggregated metrics are exported to a `ConsoleExporter`.
7777

7878
```csharp
@@ -82,6 +82,20 @@ var meterProvider = Sdk.CreateMeterProviderBuilder()
8282
.Build();
8383
```
8484

85+
> [!NOTE]
86+
> If you need to collect metrics with cardinality higher than the default limit
87+
`2000`, please follow the [cardinality
88+
limits](../README.md#cardinality-limits) guidance. Here is a quick example of
89+
how to change the cardinality limit to `10` for this particular metric:
90+
91+
```csharp
92+
var meterProvider = Sdk.CreateMeterProviderBuilder()
93+
.AddMeter("MyCompany.MyProduct.MyLibrary")
94+
.AddView(instrumentName: "MyFruitCounter", new MetricStreamConfiguration { CardinalityLimit = 10 })
95+
.AddConsoleExporter()
96+
.Build();
97+
```
98+
8599
```mermaid
86100
graph LR
87101

0 commit comments

Comments
 (0)