diff --git a/src/OpenTelemetry/Metrics/MetricReaderExt.cs b/src/OpenTelemetry/Metrics/MetricReaderExt.cs index c89fb1810bd..46c3b464557 100644 --- a/src/OpenTelemetry/Metrics/MetricReaderExt.cs +++ b/src/OpenTelemetry/Metrics/MetricReaderExt.cs @@ -137,12 +137,14 @@ internal List AddMetricsListWithViews(Instrument instrument, List(); -#pragma warning disable CS0618 // Type or member is obsolete - using var container = this.BuildMeterProvider(out var meterProvider, builder => builder - .AddMeter(meter.Name) - .SetMaxMetricPointsPerMetricStream(3) - .AddView((instrument) => + using var container = this.BuildMeterProvider(out var meterProvider, builder => + { + if (setDefault) { - return new MetricStreamConfiguration() { Name = "MetricStreamA", CardinalityLimit = 10000 }; - }) - .AddInMemoryExporter(exportedItems)); +#pragma warning disable CS0618 // Type or member is obsolete + builder.SetMaxMetricPointsPerMetricStream(3); #pragma warning restore CS0618 // Type or member is obsolete + } + + builder + .AddMeter(meter.Name) + .AddView((instrument) => + { + if (instrument.Name == "counter2") + { + return new MetricStreamConfiguration() { Name = "MetricStreamA", CardinalityLimit = 10000 }; + } + + return null; + }) + .AddInMemoryExporter(exportedItems); + }); - var counter = meter.CreateCounter("counter"); - counter.Add(100); + var counter1 = meter.CreateCounter("counter1"); + counter1.Add(100); + + var counter2 = meter.CreateCounter("counter2"); + counter2.Add(100); + + var counter3 = meter.CreateCounter("counter3"); + counter3.Add(100); meterProvider.ForceFlush(MaxTimeToAllowForFlush); - var metric = exportedItems[0]; + Assert.Equal(3, exportedItems.Count); - Assert.Equal(10000, metric.AggregatorStore.CardinalityLimit); + Assert.Equal(10000, exportedItems[1].AggregatorStore.CardinalityLimit); + if (setDefault) + { + Assert.Equal(3, exportedItems[0].AggregatorStore.CardinalityLimit); + Assert.Equal(3, exportedItems[2].AggregatorStore.CardinalityLimit); + } + else + { + Assert.Equal(2000, exportedItems[0].AggregatorStore.CardinalityLimit); + Assert.Equal(2000, exportedItems[2].AggregatorStore.CardinalityLimit); + } } [Fact]