1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-09-16 09:26:25 +02:00

sdk/metric: Add Unit Tests for Cardinality Limits (#7164)

Fixes https://github.com/open-telemetry/opentelemetry-go/issues/6978
Towards https://github.com/open-telemetry/opentelemetry-go/issues/6887

## What
 
- Unit tests cover all scenarios for cardinality limits.
- Tests validate configuration, enforcement, and default behavior.

## Notes

I've added more user-oriented usage tests. The targeted unit tests
already exist in the
`sdk/metric/internal/aggregate/_test` files.

---------

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
Co-authored-by: Robert Pająk <pellared@hotmail.com>
This commit is contained in:
Yevhenii Solomchenko
2025-08-12 09:22:43 +02:00
committed by GitHub
parent 3cf53ccf5d
commit 97343af813
2 changed files with 71 additions and 1 deletions

View File

@@ -41,7 +41,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- `RPCGRPCResponseMetadata`
- Add `ErrorType` attribute helper function to the `go.opentelmetry.io/otel/semconv/v1.34.0` package. (#6962)
- Add `WithAllowKeyDuplication` in `go.opentelemetry.io/otel/sdk/log` which can be used to disable deduplication for log records. (#6968)
- Add `WithCardinalityLimit` option to configure the cardinality limit in `go.opentelemetry.io/otel/sdk/metric`. (#6996, #7065, #7081)
- Add `WithCardinalityLimit` option to configure the cardinality limit in `go.opentelemetry.io/otel/sdk/metric`. (#6996, #7065, #7081, #7164)
- Add `Clone` method to `Record` in `go.opentelemetry.io/otel/log` that returns a copy of the record with no shared state. (#7001)
- The `go.opentelemetry.io/otel/semconv/v1.36.0` package.
The package contains semantic conventions from the `v1.36.0` version of the OpenTelemetry Semantic Conventions.

View File

@@ -174,3 +174,73 @@ func TestMeterProviderMixingOnRegisterErrors(t *testing.T) {
"Metrics produced for instrument collected by different MeterProvider",
)
}
func TestMeterProviderCardinalityLimit(t *testing.T) {
const uniqueAttributesCount = 10
tests := []struct {
name string
options []Option
wantDataPoints int
}{
{
name: "no limit (default)",
options: nil,
wantDataPoints: uniqueAttributesCount,
},
{
name: "no limit (limit=0)",
options: []Option{WithCardinalityLimit(0)},
wantDataPoints: uniqueAttributesCount,
},
{
name: "no limit (negative)",
options: []Option{WithCardinalityLimit(-5)},
wantDataPoints: uniqueAttributesCount,
},
{
name: "limit=5",
options: []Option{WithCardinalityLimit(5)},
wantDataPoints: 5,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
reader := NewManualReader()
opts := append(tt.options, WithReader(reader))
mp := NewMeterProvider(opts...)
meter := mp.Meter("test-meter")
counter, err := meter.Int64Counter("metric")
require.NoError(t, err, "failed to create counter")
for i := range uniqueAttributesCount {
counter.Add(
context.Background(),
1,
api.WithAttributes(attribute.Int("key", i)),
)
}
var rm metricdata.ResourceMetrics
err = reader.Collect(context.Background(), &rm)
require.NoError(t, err, "failed to collect metrics")
require.Len(t, rm.ScopeMetrics, 1, "expected 1 ScopeMetrics")
require.Len(t, rm.ScopeMetrics[0].Metrics, 1, "expected 1 Metric")
data := rm.ScopeMetrics[0].Metrics[0].Data
require.IsType(t, metricdata.Sum[int64]{}, data, "expected metricdata.Sum[int64]")
sumData := data.(metricdata.Sum[int64])
assert.Len(
t,
sumData.DataPoints,
tt.wantDataPoints,
"unexpected number of data points",
)
})
}
}