Skip to content

Commit

Permalink
Bug fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeBlanch committed Feb 8, 2024
1 parent 695eb66 commit f1b62b0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 16 deletions.
6 changes: 4 additions & 2 deletions src/OpenTelemetry/Metrics/MetricReaderExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,14 @@ internal List<Metric> AddMetricsListWithViews(Instrument instrument, List<Metric
{
bool shouldReclaimUnusedMetricPoints = this.parentProvider is MeterProviderSdk meterProviderSdk && meterProviderSdk.ShouldReclaimUnusedMetricPoints;

var cardinalityLimit = this.cardinalityLimit;

if (metricStreamConfig != null && metricStreamConfig.CardinalityLimit != null)
{
this.cardinalityLimit = metricStreamConfig.CardinalityLimit.Value;
cardinalityLimit = metricStreamConfig.CardinalityLimit.Value;
}

Metric metric = new(metricStreamIdentity, this.GetAggregationTemporality(metricStreamIdentity.InstrumentType), this.cardinalityLimit, this.emitOverflowAttribute, shouldReclaimUnusedMetricPoints, this.exemplarFilter);
Metric metric = new(metricStreamIdentity, this.GetAggregationTemporality(metricStreamIdentity.InstrumentType), cardinalityLimit, this.emitOverflowAttribute, shouldReclaimUnusedMetricPoints, this.exemplarFilter);

this.instrumentIdentityToMetric[metricStreamIdentity] = metric;
this.metrics![index] = metric;
Expand Down
58 changes: 44 additions & 14 deletions test/OpenTelemetry.Tests/Metrics/MetricViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -919,31 +919,61 @@ public void ViewConflict_OneInstrument_DifferentDescription()
Assert.Equal(10, metricPoint2.GetSumLong());
}

[Fact]
public void CardinalityLimitofMatchingViewTakesPrecedenceOverMetricProviderWhenBothWereSet()
[Theory]
[InlineData(true)]
[InlineData(false)]
public void CardinalityLimitofMatchingViewTakesPrecedenceOverMeterProvider(bool setDefault)
{
using var meter = new Meter(Utils.GetCurrentMethodName());
var exportedItems = new List<Metric>();

#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<long>("counter");
counter.Add(100);
var counter1 = meter.CreateCounter<long>("counter1");
counter1.Add(100);

var counter2 = meter.CreateCounter<long>("counter2");
counter2.Add(100);

var counter3 = meter.CreateCounter<long>("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]
Expand Down

0 comments on commit f1b62b0

Please sign in to comment.