From 7bd3de95a5633174103dbc0e8b116c48a2df109f Mon Sep 17 00:00:00 2001 From: Cijo Thomas Date: Thu, 23 Sep 2021 12:26:18 -0700 Subject: [PATCH] Unit test for metric stream duplication (#2408) --- .../Metrics/MetricAPITest.cs | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/test/OpenTelemetry.Tests/Metrics/MetricAPITest.cs b/test/OpenTelemetry.Tests/Metrics/MetricAPITest.cs index f2e06df3a31..0ddfa6fb9bf 100644 --- a/test/OpenTelemetry.Tests/Metrics/MetricAPITest.cs +++ b/test/OpenTelemetry.Tests/Metrics/MetricAPITest.cs @@ -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(); + int metricCount = 0; + var metricExporter = new TestExporter(ProcessExport); + + void ProcessExport(Batch 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("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("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("name1"); + anotherCounterSameNameDiffMeter.Add(10); + metricCount = 0; + metricReader.Collect(); + Assert.Equal(1, metricCount); + } + [Theory] [InlineData(true)] [InlineData(false)]