Skip to content

Commit

Permalink
Unit test for metric stream duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
cijothomas committed Sep 23, 2021
1 parent 080927d commit 1af2cd0
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions test/OpenTelemetry.Tests/Metrics/MetricAPITest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,60 @@ public MetricApiTest(ITestOutputHelper output)
this.output = output;
}

[Theory]
[InlineData(AggregationTemporality.Cumulative)]
[InlineData(AggregationTemporality.Delta)]
public void StreamNamesDuplicatesAreNotAllowedTest(AggregationTemporality temporality)
{
var metricItems = new List<Metric>();
int metricCount = 0;
var metricExporter = new TestExporter<Metric>(ProcessExport);

void ProcessExport(Batch<Metric> batch)
{
foreach (var metric in batch)
{
metricCount++;
}
}

var metricReader = new BaseExportingMetricReader(metricExporter)
{
PreferredAggregationTemporality = temporality,
};
using var meter1 = new Meter("TestDuplicateMetricName1");
using var meter2 = new Meter("TestDuplicateMetricName2");
using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddSource("TestDuplicateMetricName1")
.AddSource("TestDuplicateMetricName2")
.AddMetricReader(metricReader)
.Build();

// Expecting one metric stream.
var counterLong = meter1.CreateCounter<long>("name1");
counterLong.Add(10);
metricReader.Collect();
Assert.Equal(1, metricCount);

// The following will be ignored as
// metric of same name exists.
// Metric stream will remain one.
var anotherCounterSameName = meter1.CreateCounter<long>("name1");
anotherCounterSameName.Add(10);
metricCount = 0;
metricReader.Collect();
Assert.Equal(1, metricCount);

// The following will also be ignored
// as the name is same.
// (the Meter name is not part of stream name)
var anotherCounterSameNameDiffMeter = meter2.CreateCounter<long>("name1");
anotherCounterSameNameDiffMeter.Add(10);
metricCount = 0;
metricReader.Collect();
Assert.Equal(1, metricCount);
}

[Theory]
[InlineData(true)]
[InlineData(false)]
Expand Down

0 comments on commit 1af2cd0

Please sign in to comment.