Skip to content

Commit 97343af

Browse files
ysolomchenkoMrAliaspellared
authored
sdk/metric: Add Unit Tests for Cardinality Limits (#7164)
Fixes #6978 Towards #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 <[email protected]> Co-authored-by: Robert Pająk <[email protected]>
1 parent 3cf53cc commit 97343af

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

CHANGELOG.md

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

sdk/metric/provider_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,73 @@ func TestMeterProviderMixingOnRegisterErrors(t *testing.T) {
174174
"Metrics produced for instrument collected by different MeterProvider",
175175
)
176176
}
177+
178+
func TestMeterProviderCardinalityLimit(t *testing.T) {
179+
const uniqueAttributesCount = 10
180+
181+
tests := []struct {
182+
name string
183+
options []Option
184+
wantDataPoints int
185+
}{
186+
{
187+
name: "no limit (default)",
188+
options: nil,
189+
wantDataPoints: uniqueAttributesCount,
190+
},
191+
{
192+
name: "no limit (limit=0)",
193+
options: []Option{WithCardinalityLimit(0)},
194+
wantDataPoints: uniqueAttributesCount,
195+
},
196+
{
197+
name: "no limit (negative)",
198+
options: []Option{WithCardinalityLimit(-5)},
199+
wantDataPoints: uniqueAttributesCount,
200+
},
201+
{
202+
name: "limit=5",
203+
options: []Option{WithCardinalityLimit(5)},
204+
wantDataPoints: 5,
205+
},
206+
}
207+
208+
for _, tt := range tests {
209+
t.Run(tt.name, func(t *testing.T) {
210+
reader := NewManualReader()
211+
212+
opts := append(tt.options, WithReader(reader))
213+
mp := NewMeterProvider(opts...)
214+
215+
meter := mp.Meter("test-meter")
216+
counter, err := meter.Int64Counter("metric")
217+
require.NoError(t, err, "failed to create counter")
218+
219+
for i := range uniqueAttributesCount {
220+
counter.Add(
221+
context.Background(),
222+
1,
223+
api.WithAttributes(attribute.Int("key", i)),
224+
)
225+
}
226+
227+
var rm metricdata.ResourceMetrics
228+
err = reader.Collect(context.Background(), &rm)
229+
require.NoError(t, err, "failed to collect metrics")
230+
231+
require.Len(t, rm.ScopeMetrics, 1, "expected 1 ScopeMetrics")
232+
require.Len(t, rm.ScopeMetrics[0].Metrics, 1, "expected 1 Metric")
233+
234+
data := rm.ScopeMetrics[0].Metrics[0].Data
235+
require.IsType(t, metricdata.Sum[int64]{}, data, "expected metricdata.Sum[int64]")
236+
237+
sumData := data.(metricdata.Sum[int64])
238+
assert.Len(
239+
t,
240+
sumData.DataPoints,
241+
tt.wantDataPoints,
242+
"unexpected number of data points",
243+
)
244+
})
245+
}
246+
}

0 commit comments

Comments
 (0)