From 706f49e136364e54a99072bf22ff32dec49e0d28 Mon Sep 17 00:00:00 2001 From: Alan West <3676547+alanwest@users.noreply.github.com> Date: Tue, 23 Nov 2021 15:04:29 -0800 Subject: [PATCH 01/13] Simplify ToOtlpResourceMetricsTest --- .../OtlpMetricsExporterTests.cs | 101 ++++-------------- 1 file changed, 21 insertions(+), 80 deletions(-) diff --git a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs index f19925b3a5b..cef3c4df419 100644 --- a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs +++ b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs @@ -14,20 +14,16 @@ // limitations under the License. // -using System; using System.Collections.Generic; using System.Diagnostics.Metrics; using System.Linq; -using System.Threading; using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation; using OpenTelemetry.Metrics; using OpenTelemetry.Resources; using OpenTelemetry.Tests; using OpenTelemetry.Trace; using Xunit; -using GrpcCore = Grpc.Core; using OtlpCollector = Opentelemetry.Proto.Collector.Metrics.V1; -using OtlpMetrics = Opentelemetry.Proto.Metrics.V1; namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests { @@ -49,100 +45,45 @@ public void ToOtlpResourceMetricsTest(bool includeServiceNameInResource) }); } - var tags = new KeyValuePair[] - { - new KeyValuePair("key1", "value1"), - new KeyValuePair("key2", "value2"), - }; - using var meter = new Meter($"{Utils.GetCurrentMethodName()}.{includeServiceNameInResource}", "0.0.1"); - var exportedItems = new List(); + var metrics = new List(); using var provider = Sdk.CreateMeterProviderBuilder() .SetResourceBuilder(resourceBuilder) .AddMeter(meter.Name) - .AddReader(new BaseExportingMetricReader(new InMemoryExporter(exportedItems)) - { - Temporality = AggregationTemporality.Delta, - }) + .AddInMemoryExporter(metrics) .Build(); var counter = meter.CreateCounter("counter"); - - counter.Add(100, tags); - - var testCompleted = false; + counter.Add(100); provider.ForceFlush(); - var batch = new Batch(exportedItems.ToArray(), exportedItems.Count); - RunTest(batch); - - Assert.True(testCompleted); - - void RunTest(Batch metrics) - { - var request = new OtlpCollector.ExportMetricsServiceRequest(); - request.AddMetrics(resourceBuilder.Build().ToOtlpResource(), metrics); - - Assert.Single(request.ResourceMetrics); - var resourceMetric = request.ResourceMetrics.First(); - var oltpResource = resourceMetric.Resource; - - if (includeServiceNameInResource) - { - Assert.Contains(oltpResource.Attributes, (kvp) => kvp.Key == ResourceSemanticConventions.AttributeServiceName && kvp.Value.StringValue == "service-name"); - Assert.Contains(oltpResource.Attributes, (kvp) => kvp.Key == ResourceSemanticConventions.AttributeServiceNamespace && kvp.Value.StringValue == "ns1"); - } - else - { - Assert.Contains(oltpResource.Attributes, (kvp) => kvp.Key == ResourceSemanticConventions.AttributeServiceName && kvp.Value.ToString().Contains("unknown_service:")); - } - - Assert.Single(resourceMetric.InstrumentationLibraryMetrics); - var instrumentationLibraryMetrics = resourceMetric.InstrumentationLibraryMetrics.First(); - Assert.Equal(string.Empty, instrumentationLibraryMetrics.SchemaUrl); - Assert.Equal(meter.Name, instrumentationLibraryMetrics.InstrumentationLibrary.Name); - Assert.Equal("0.0.1", instrumentationLibraryMetrics.InstrumentationLibrary.Version); + var batch = new Batch(metrics.ToArray(), metrics.Count); - Assert.Single(instrumentationLibraryMetrics.Metrics); + var request = new OtlpCollector.ExportMetricsServiceRequest(); + request.AddMetrics(resourceBuilder.Build().ToOtlpResource(), batch); - foreach (var metric in instrumentationLibraryMetrics.Metrics) - { - Assert.Equal(string.Empty, metric.Description); - Assert.Equal(string.Empty, metric.Unit); - Assert.Equal("counter", metric.Name); + Assert.Single(request.ResourceMetrics); + var resourceMetric = request.ResourceMetrics.First(); + var oltpResource = resourceMetric.Resource; - Assert.Equal(OtlpMetrics.Metric.DataOneofCase.Sum, metric.DataCase); - Assert.True(metric.Sum.IsMonotonic); - Assert.Equal(OtlpMetrics.AggregationTemporality.Delta, metric.Sum.AggregationTemporality); - - Assert.Single(metric.Sum.DataPoints); - var dataPoint = metric.Sum.DataPoints.First(); - Assert.True(dataPoint.StartTimeUnixNano > 0); - Assert.True(dataPoint.TimeUnixNano > 0); - Assert.Equal(OtlpMetrics.NumberDataPoint.ValueOneofCase.AsInt, dataPoint.ValueCase); - Assert.Equal(100, dataPoint.AsInt); - -#pragma warning disable CS0612 // Type or member is obsolete - Assert.Empty(dataPoint.Labels); -#pragma warning restore CS0612 // Type or member is obsolete - OtlpTestHelpers.AssertOtlpAttributes(tags.ToList(), dataPoint.Attributes); - - Assert.Empty(dataPoint.Exemplars); - } - - testCompleted = true; + if (includeServiceNameInResource) + { + Assert.Contains(oltpResource.Attributes, (kvp) => kvp.Key == ResourceSemanticConventions.AttributeServiceName && kvp.Value.StringValue == "service-name"); + Assert.Contains(oltpResource.Attributes, (kvp) => kvp.Key == ResourceSemanticConventions.AttributeServiceNamespace && kvp.Value.StringValue == "ns1"); } - } - - private class NoopMetricsServiceClient : OtlpCollector.MetricsService.IMetricsServiceClient - { - public OtlpCollector.ExportMetricsServiceResponse Export(OtlpCollector.ExportMetricsServiceRequest request, GrpcCore.Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default) + else { - return null; + Assert.Contains(oltpResource.Attributes, (kvp) => kvp.Key == ResourceSemanticConventions.AttributeServiceName && kvp.Value.ToString().Contains("unknown_service:")); } + + Assert.Single(resourceMetric.InstrumentationLibraryMetrics); + var instrumentationLibraryMetrics = resourceMetric.InstrumentationLibraryMetrics.First(); + Assert.Equal(string.Empty, instrumentationLibraryMetrics.SchemaUrl); + Assert.Equal(meter.Name, instrumentationLibraryMetrics.InstrumentationLibrary.Name); + Assert.Equal("0.0.1", instrumentationLibraryMetrics.InstrumentationLibrary.Version); } } } From 79d3877f907ba3cbdf8260879368f5162bde6de1 Mon Sep 17 00:00:00 2001 From: Alan West <3676547+alanwest@users.noreply.github.com> Date: Tue, 23 Nov 2021 15:09:43 -0800 Subject: [PATCH 02/13] Simplify ToOtlpResourceMetricsTest --- .../OtlpMetricsExporterTests.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs index cef3c4df419..d6c13b889d2 100644 --- a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs +++ b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs @@ -45,10 +45,9 @@ public void ToOtlpResourceMetricsTest(bool includeServiceNameInResource) }); } - using var meter = new Meter($"{Utils.GetCurrentMethodName()}.{includeServiceNameInResource}", "0.0.1"); - var metrics = new List(); + using var meter = new Meter($"{Utils.GetCurrentMethodName()}.{includeServiceNameInResource}", "0.0.1"); using var provider = Sdk.CreateMeterProviderBuilder() .SetResourceBuilder(resourceBuilder) .AddMeter(meter.Name) From d27c32cb2404079569ce143b70ffdce71f7ecce0 Mon Sep 17 00:00:00 2001 From: Alan West <3676547+alanwest@users.noreply.github.com> Date: Tue, 23 Nov 2021 15:29:16 -0800 Subject: [PATCH 03/13] Test OTLP gauge transform --- .../OtlpMetricsExporterTests.cs | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs index d6c13b889d2..9cb1d749581 100644 --- a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs +++ b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs @@ -24,6 +24,7 @@ using OpenTelemetry.Trace; using Xunit; using OtlpCollector = Opentelemetry.Proto.Collector.Metrics.V1; +using OtlpMetrics = Opentelemetry.Proto.Metrics.V1; namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests { @@ -84,5 +85,98 @@ public void ToOtlpResourceMetricsTest(bool includeServiceNameInResource) Assert.Equal(meter.Name, instrumentationLibraryMetrics.InstrumentationLibrary.Name); Assert.Equal("0.0.1", instrumentationLibraryMetrics.InstrumentationLibrary.Version); } + + [Theory] + [InlineData("test_gauge", null, null, 123, null)] + [InlineData("test_gauge", null, null, null, 123.45)] + [InlineData("test_gauge", "description", "unit", 123, null)] + public void TestGaugeToOtlpMetric(string name, string description, string unit, long? longValue, double? doubleValue, params object[] keysValues) + { + var metrics = new List(); + + using var meter = new Meter(Utils.GetCurrentMethodName()); + using var provider = Sdk.CreateMeterProviderBuilder() + .AddMeter(meter.Name) + .AddInMemoryExporter(metrics) + .Build(); + + var attributes = ToAttributes(keysValues).ToArray(); + if (longValue.HasValue) + { + meter.CreateObservableGauge(name, () => longValue.Value, unit, description); + } + else + { + meter.CreateObservableGauge(name, () => doubleValue.Value, unit, description); + } + + provider.ForceFlush(); + + var batch = new Batch(metrics.ToArray(), metrics.Count); + + var request = new OtlpCollector.ExportMetricsServiceRequest(); + request.AddMetrics(ResourceBuilder.CreateEmpty().Build().ToOtlpResource(), batch); + + var resourceMetric = request.ResourceMetrics.Single(); + var instrumentationLibraryMetrics = resourceMetric.InstrumentationLibraryMetrics.Single(); + var actual = instrumentationLibraryMetrics.Metrics.Single(); + + Assert.Equal(name, actual.Name); + Assert.Equal(description ?? string.Empty, actual.Description); + Assert.Equal(unit ?? string.Empty, actual.Unit); + + Assert.Equal(OtlpMetrics.Metric.DataOneofCase.Gauge, actual.DataCase); + + Assert.NotNull(actual.Gauge); + Assert.Null(actual.Sum); + Assert.Null(actual.Histogram); + Assert.Null(actual.ExponentialHistogram); + Assert.Null(actual.Summary); + + Assert.Single(actual.Gauge.DataPoints); + var dataPoint = actual.Gauge.DataPoints.First(); + Assert.True(dataPoint.StartTimeUnixNano > 0); + Assert.True(dataPoint.TimeUnixNano > 0); + + if (longValue.HasValue) + { + Assert.Equal(OtlpMetrics.NumberDataPoint.ValueOneofCase.AsInt, dataPoint.ValueCase); + Assert.Equal(longValue, dataPoint.AsInt); + } + else + { + Assert.Equal(OtlpMetrics.NumberDataPoint.ValueOneofCase.AsDouble, dataPoint.ValueCase); + Assert.Equal(doubleValue, dataPoint.AsDouble); + } + + if (attributes.Length > 0) + { + OtlpTestHelpers.AssertOtlpAttributes(attributes, dataPoint.Attributes); + } + else + { + Assert.Empty(dataPoint.Attributes); + } + + Assert.Empty(dataPoint.Exemplars); + +#pragma warning disable CS0612 // Type or member is obsolete + Assert.Null(actual.IntGauge); + Assert.Null(actual.IntSum); + Assert.Null(actual.IntHistogram); + Assert.Empty(dataPoint.Labels); +#pragma warning restore CS0612 // Type or member is obsolete + } + + private static IEnumerable> ToAttributes(object[] keysValues) + { + var keys = keysValues?.Where((_, index) => index % 2 == 0).ToArray(); + var values = keysValues?.Where((_, index) => index % 2 != 0).ToArray(); + + for (var i = 0; keys != null && i < keys.Length; ++i) + { + yield return new KeyValuePair(keys[i].ToString(), values[i]); + } + } } } From 390a7af36c25796a19a8f597fc7091aeecd63511 Mon Sep 17 00:00:00 2001 From: Alan West <3676547+alanwest@users.noreply.github.com> Date: Tue, 23 Nov 2021 15:31:47 -0800 Subject: [PATCH 04/13] Test OTLP sum transform --- .../OtlpMetricsExporterTests.cs | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) diff --git a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs index 9cb1d749581..57dad1e8941 100644 --- a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs +++ b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs @@ -160,6 +160,102 @@ public void TestGaugeToOtlpMetric(string name, string description, string unit, Assert.Empty(dataPoint.Exemplars); +#pragma warning disable CS0612 // Type or member is obsolete + Assert.Null(actual.IntGauge); + Assert.Null(actual.IntSum); + Assert.Null(actual.IntHistogram); + Assert.Empty(dataPoint.Labels); +#pragma warning restore CS0612 // Type or member is obsolete + } + + [Theory] + [InlineData("test_counter", null, null, 123, null, AggregationTemporality.Cumulative, true)] + [InlineData("test_counter", null, null, null, 123.45, AggregationTemporality.Cumulative, true)] + [InlineData("test_counter", null, null, 123, null, AggregationTemporality.Delta, true)] + [InlineData("test_counter", "description", "unit", 123, null, AggregationTemporality.Cumulative, true)] + [InlineData("test_counter", null, null, 123, null, AggregationTemporality.Delta, true, "key1", "value1", "key2", 123)] + public void TestCounterToOltpMetric(string name, string description, string unit, long? longValue, double? doubleValue, AggregationTemporality aggregationTemporality, bool isMonotonic, params object[] keysValues) + { + var metrics = new List(); + + var metricReader = new BaseExportingMetricReader(new InMemoryExporter(metrics)); + metricReader.Temporality = aggregationTemporality; + + using var meter = new Meter(Utils.GetCurrentMethodName()); + using var provider = Sdk.CreateMeterProviderBuilder() + .AddMeter(meter.Name) + .AddReader(metricReader) + .Build(); + + var attributes = ToAttributes(keysValues).ToArray(); + if (longValue.HasValue) + { + var counter = meter.CreateCounter(name, unit, description); + counter.Add(longValue.Value, attributes); + } + else + { + var counter = meter.CreateCounter(name, unit, description); + counter.Add(doubleValue.Value, attributes); + } + + provider.ForceFlush(); + + var batch = new Batch(metrics.ToArray(), metrics.Count); + + var request = new OtlpCollector.ExportMetricsServiceRequest(); + request.AddMetrics(ResourceBuilder.CreateEmpty().Build().ToOtlpResource(), batch); + + var resourceMetric = request.ResourceMetrics.Single(); + var instrumentationLibraryMetrics = resourceMetric.InstrumentationLibraryMetrics.Single(); + var actual = instrumentationLibraryMetrics.Metrics.Single(); + + Assert.Equal(name, actual.Name); + Assert.Equal(description ?? string.Empty, actual.Description); + Assert.Equal(unit ?? string.Empty, actual.Unit); + + Assert.Equal(OtlpMetrics.Metric.DataOneofCase.Sum, actual.DataCase); + + Assert.Null(actual.Gauge); + Assert.NotNull(actual.Sum); + Assert.Null(actual.Histogram); + Assert.Null(actual.ExponentialHistogram); + Assert.Null(actual.Summary); + + Assert.Equal(isMonotonic, actual.Sum.IsMonotonic); + + var otlpAggregationTemporality = aggregationTemporality == AggregationTemporality.Cumulative + ? OtlpMetrics.AggregationTemporality.Cumulative + : OtlpMetrics.AggregationTemporality.Delta; + Assert.Equal(otlpAggregationTemporality, actual.Sum.AggregationTemporality); + + Assert.Single(actual.Sum.DataPoints); + var dataPoint = actual.Sum.DataPoints.First(); + Assert.True(dataPoint.StartTimeUnixNano > 0); + Assert.True(dataPoint.TimeUnixNano > 0); + + if (longValue.HasValue) + { + Assert.Equal(OtlpMetrics.NumberDataPoint.ValueOneofCase.AsInt, dataPoint.ValueCase); + Assert.Equal(longValue, dataPoint.AsInt); + } + else + { + Assert.Equal(OtlpMetrics.NumberDataPoint.ValueOneofCase.AsDouble, dataPoint.ValueCase); + Assert.Equal(doubleValue, dataPoint.AsDouble); + } + + if (attributes.Length > 0) + { + OtlpTestHelpers.AssertOtlpAttributes(attributes, dataPoint.Attributes); + } + else + { + Assert.Empty(dataPoint.Attributes); + } + + Assert.Empty(dataPoint.Exemplars); + #pragma warning disable CS0612 // Type or member is obsolete Assert.Null(actual.IntGauge); Assert.Null(actual.IntSum); From 3da7f95ac415e0b8695d08ffb7c2e48f6919a6cc Mon Sep 17 00:00:00 2001 From: Alan West <3676547+alanwest@users.noreply.github.com> Date: Tue, 23 Nov 2021 15:32:13 -0800 Subject: [PATCH 05/13] Test OTLP histogram transform --- .../OtlpMetricsExporterTests.cs | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs index 57dad1e8941..b2f3cd48c24 100644 --- a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs +++ b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs @@ -256,6 +256,100 @@ public void TestCounterToOltpMetric(string name, string description, string unit Assert.Empty(dataPoint.Exemplars); +#pragma warning disable CS0612 // Type or member is obsolete + Assert.Null(actual.IntGauge); + Assert.Null(actual.IntSum); + Assert.Null(actual.IntHistogram); + Assert.Empty(dataPoint.Labels); +#pragma warning restore CS0612 // Type or member is obsolete + } + + [Theory] + [InlineData("test_histogram", null, null, 123, null, AggregationTemporality.Cumulative, true)] + [InlineData("test_histogram", null, null, null, 123.45, AggregationTemporality.Cumulative, true)] + [InlineData("test_histogram", null, null, 123, null, AggregationTemporality.Delta, true)] + [InlineData("test_histogram", "description", "unit", 123, null, AggregationTemporality.Cumulative, true)] + [InlineData("test_histogram", null, null, 123, null, AggregationTemporality.Delta, true, "key1", "value1", "key2", 123)] + public void TestHistogramToOltpMetric(string name, string description, string unit, long? longValue, double? doubleValue, AggregationTemporality aggregationTemporality, bool isMonotonic, params object[] keysValues) + { + var metrics = new List(); + + var metricReader = new BaseExportingMetricReader(new InMemoryExporter(metrics)); + metricReader.Temporality = aggregationTemporality; + + using var meter = new Meter(Utils.GetCurrentMethodName()); + using var provider = Sdk.CreateMeterProviderBuilder() + .AddMeter(meter.Name) + .AddReader(metricReader) + .Build(); + + var attributes = ToAttributes(keysValues).ToArray(); + if (longValue.HasValue) + { + var histogram = meter.CreateHistogram(name, unit, description); + histogram.Record(longValue.Value, attributes); + } + else + { + var histogram = meter.CreateHistogram(name, unit, description); + histogram.Record(doubleValue.Value, attributes); + } + + provider.ForceFlush(); + + var batch = new Batch(metrics.ToArray(), metrics.Count); + + var request = new OtlpCollector.ExportMetricsServiceRequest(); + request.AddMetrics(ResourceBuilder.CreateEmpty().Build().ToOtlpResource(), batch); + + var resourceMetric = request.ResourceMetrics.Single(); + var instrumentationLibraryMetrics = resourceMetric.InstrumentationLibraryMetrics.Single(); + var actual = instrumentationLibraryMetrics.Metrics.Single(); + + Assert.Equal(name, actual.Name); + Assert.Equal(description ?? string.Empty, actual.Description); + Assert.Equal(unit ?? string.Empty, actual.Unit); + + Assert.Equal(OtlpMetrics.Metric.DataOneofCase.Histogram, actual.DataCase); + + Assert.Null(actual.Gauge); + Assert.Null(actual.Sum); + Assert.NotNull(actual.Histogram); + Assert.Null(actual.ExponentialHistogram); + Assert.Null(actual.Summary); + + var otlpAggregationTemporality = aggregationTemporality == AggregationTemporality.Cumulative + ? OtlpMetrics.AggregationTemporality.Cumulative + : OtlpMetrics.AggregationTemporality.Delta; + Assert.Equal(otlpAggregationTemporality, actual.Histogram.AggregationTemporality); + + Assert.Single(actual.Histogram.DataPoints); + var dataPoint = actual.Histogram.DataPoints.First(); + Assert.True(dataPoint.StartTimeUnixNano > 0); + Assert.True(dataPoint.TimeUnixNano > 0); + + Assert.Equal(1UL, dataPoint.Count); + + if (longValue.HasValue) + { + Assert.Equal((double)longValue, dataPoint.Sum); + } + else + { + Assert.Equal(doubleValue, dataPoint.Sum); + } + + if (attributes.Length > 0) + { + OtlpTestHelpers.AssertOtlpAttributes(attributes, dataPoint.Attributes); + } + else + { + Assert.Empty(dataPoint.Attributes); + } + + Assert.Empty(dataPoint.Exemplars); + #pragma warning disable CS0612 // Type or member is obsolete Assert.Null(actual.IntGauge); Assert.Null(actual.IntSum); From 008b9be6d3385e38d5fdb2fea2e1a94fba786008 Mon Sep 17 00:00:00 2001 From: Alan West <3676547+alanwest@users.noreply.github.com> Date: Tue, 23 Nov 2021 15:32:44 -0800 Subject: [PATCH 06/13] Fix OTLP histogram transform bug --- .../Implementation/MetricItemExtensions.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/MetricItemExtensions.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/MetricItemExtensions.cs index e85cdcda231..45b11f5d543 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/MetricItemExtensions.cs +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/MetricItemExtensions.cs @@ -246,8 +246,8 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric) }; AddAttributes(metricPoint.Tags, dataPoint.Attributes); - dataPoint.Count = (ulong)metricPoint.LongValue; - dataPoint.Sum = metricPoint.DoubleValue; + dataPoint.Count = (ulong)metricPoint.GetHistogramCount(); + dataPoint.Sum = metricPoint.GetHistogramSum(); foreach (var histogramMeasurement in metricPoint.GetHistogramBuckets()) { From 643b895c3ae88910b606f7cc8d4f5520aadd5c27 Mon Sep 17 00:00:00 2001 From: Alan West <3676547+alanwest@users.noreply.github.com> Date: Tue, 23 Nov 2021 15:38:13 -0800 Subject: [PATCH 07/13] Remove unused parameter --- .../OtlpMetricsExporterTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs index b2f3cd48c24..ba7b5845d1c 100644 --- a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs +++ b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs @@ -270,7 +270,7 @@ public void TestCounterToOltpMetric(string name, string description, string unit [InlineData("test_histogram", null, null, 123, null, AggregationTemporality.Delta, true)] [InlineData("test_histogram", "description", "unit", 123, null, AggregationTemporality.Cumulative, true)] [InlineData("test_histogram", null, null, 123, null, AggregationTemporality.Delta, true, "key1", "value1", "key2", 123)] - public void TestHistogramToOltpMetric(string name, string description, string unit, long? longValue, double? doubleValue, AggregationTemporality aggregationTemporality, bool isMonotonic, params object[] keysValues) + public void TestHistogramToOltpMetric(string name, string description, string unit, long? longValue, double? doubleValue, AggregationTemporality aggregationTemporality, params object[] keysValues) { var metrics = new List(); From 177acaf7778f7daaa2d5f2479b711931ddb2f2c5 Mon Sep 17 00:00:00 2001 From: Alan West <3676547+alanwest@users.noreply.github.com> Date: Tue, 23 Nov 2021 15:49:00 -0800 Subject: [PATCH 08/13] Remove unused parameter --- .../OtlpMetricsExporterTests.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs index ba7b5845d1c..082c69574b1 100644 --- a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs +++ b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs @@ -265,11 +265,11 @@ public void TestCounterToOltpMetric(string name, string description, string unit } [Theory] - [InlineData("test_histogram", null, null, 123, null, AggregationTemporality.Cumulative, true)] - [InlineData("test_histogram", null, null, null, 123.45, AggregationTemporality.Cumulative, true)] - [InlineData("test_histogram", null, null, 123, null, AggregationTemporality.Delta, true)] - [InlineData("test_histogram", "description", "unit", 123, null, AggregationTemporality.Cumulative, true)] - [InlineData("test_histogram", null, null, 123, null, AggregationTemporality.Delta, true, "key1", "value1", "key2", 123)] + [InlineData("test_histogram", null, null, 123, null, AggregationTemporality.Cumulative)] + [InlineData("test_histogram", null, null, null, 123.45, AggregationTemporality.Cumulative)] + [InlineData("test_histogram", null, null, 123, null, AggregationTemporality.Delta)] + [InlineData("test_histogram", "description", "unit", 123, null, AggregationTemporality.Cumulative)] + [InlineData("test_histogram", null, null, 123, null, AggregationTemporality.Delta, "key1", "value1", "key2", 123)] public void TestHistogramToOltpMetric(string name, string description, string unit, long? longValue, double? doubleValue, AggregationTemporality aggregationTemporality, params object[] keysValues) { var metrics = new List(); From 0b329f2cafdacaf05928568ffadd055ff40d0379 Mon Sep 17 00:00:00 2001 From: Alan West <3676547+alanwest@users.noreply.github.com> Date: Tue, 23 Nov 2021 17:08:36 -0800 Subject: [PATCH 09/13] Can't set attributes on observable gauges --- .../OtlpMetricsExporterTests.cs | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs index 082c69574b1..2925e62cddf 100644 --- a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs +++ b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs @@ -90,7 +90,7 @@ public void ToOtlpResourceMetricsTest(bool includeServiceNameInResource) [InlineData("test_gauge", null, null, 123, null)] [InlineData("test_gauge", null, null, null, 123.45)] [InlineData("test_gauge", "description", "unit", 123, null)] - public void TestGaugeToOtlpMetric(string name, string description, string unit, long? longValue, double? doubleValue, params object[] keysValues) + public void TestGaugeToOtlpMetric(string name, string description, string unit, long? longValue, double? doubleValue) { var metrics = new List(); @@ -100,7 +100,6 @@ public void TestGaugeToOtlpMetric(string name, string description, string unit, .AddInMemoryExporter(metrics) .Build(); - var attributes = ToAttributes(keysValues).ToArray(); if (longValue.HasValue) { meter.CreateObservableGauge(name, () => longValue.Value, unit, description); @@ -149,14 +148,7 @@ public void TestGaugeToOtlpMetric(string name, string description, string unit, Assert.Equal(doubleValue, dataPoint.AsDouble); } - if (attributes.Length > 0) - { - OtlpTestHelpers.AssertOtlpAttributes(attributes, dataPoint.Attributes); - } - else - { - Assert.Empty(dataPoint.Attributes); - } + Assert.Empty(dataPoint.Attributes); Assert.Empty(dataPoint.Exemplars); From 898b74daaa82616121a5bab067b87f96e9d0759d Mon Sep 17 00:00:00 2001 From: Alan West <3676547+alanwest@users.noreply.github.com> Date: Tue, 23 Nov 2021 17:12:55 -0800 Subject: [PATCH 10/13] Remove metricReader local variable --- .../OtlpMetricsExporterTests.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs index 2925e62cddf..0bef0d0b96c 100644 --- a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs +++ b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs @@ -170,13 +170,13 @@ public void TestCounterToOltpMetric(string name, string description, string unit { var metrics = new List(); - var metricReader = new BaseExportingMetricReader(new InMemoryExporter(metrics)); - metricReader.Temporality = aggregationTemporality; - using var meter = new Meter(Utils.GetCurrentMethodName()); using var provider = Sdk.CreateMeterProviderBuilder() .AddMeter(meter.Name) - .AddReader(metricReader) + .AddReader(new BaseExportingMetricReader(new InMemoryExporter(metrics)) + { + Temporality = aggregationTemporality, + }) .Build(); var attributes = ToAttributes(keysValues).ToArray(); From 82992f62d1b9c29ba8d348b1fbe5cf031be4e8a2 Mon Sep 17 00:00:00 2001 From: Alan West <3676547+alanwest@users.noreply.github.com> Date: Tue, 23 Nov 2021 17:27:28 -0800 Subject: [PATCH 11/13] Fix formatting --- .../OtlpMetricsExporterTests.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs index 0bef0d0b96c..538dd8b8138 100644 --- a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs +++ b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs @@ -173,10 +173,11 @@ public void TestCounterToOltpMetric(string name, string description, string unit using var meter = new Meter(Utils.GetCurrentMethodName()); using var provider = Sdk.CreateMeterProviderBuilder() .AddMeter(meter.Name) - .AddReader(new BaseExportingMetricReader(new InMemoryExporter(metrics)) - { - Temporality = aggregationTemporality, - }) + .AddReader( + new BaseExportingMetricReader(new InMemoryExporter(exportedItems)) + { + Temporality = temporality, + }) .Build(); var attributes = ToAttributes(keysValues).ToArray(); From c55193032c535e0ec365b428250876625cdbecd2 Mon Sep 17 00:00:00 2001 From: Alan West <3676547+alanwest@users.noreply.github.com> Date: Tue, 23 Nov 2021 17:28:49 -0800 Subject: [PATCH 12/13] Fix formatting take 2 --- .../OtlpMetricsExporterTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs index 538dd8b8138..961455f0cfb 100644 --- a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs +++ b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs @@ -174,9 +174,9 @@ public void TestCounterToOltpMetric(string name, string description, string unit using var provider = Sdk.CreateMeterProviderBuilder() .AddMeter(meter.Name) .AddReader( - new BaseExportingMetricReader(new InMemoryExporter(exportedItems)) + new BaseExportingMetricReader(new InMemoryExporter(metrics)) { - Temporality = temporality, + Temporality = aggregationTemporality, }) .Build(); From 67e022a4b0050da171b46f4a7d44fc4ab9673ccf Mon Sep 17 00:00:00 2001 From: Alan West <3676547+alanwest@users.noreply.github.com> Date: Tue, 23 Nov 2021 18:30:26 -0800 Subject: [PATCH 13/13] Validate bucket counts --- .../OtlpMetricsExporterTests.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs index 961455f0cfb..34f55b7a3b6 100644 --- a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs +++ b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs @@ -332,6 +332,19 @@ public void TestHistogramToOltpMetric(string name, string description, string un Assert.Equal(doubleValue, dataPoint.Sum); } + int bucketIndex; + for (bucketIndex = 0; bucketIndex < dataPoint.ExplicitBounds.Count; ++bucketIndex) + { + if (dataPoint.Sum <= dataPoint.ExplicitBounds[bucketIndex]) + { + break; + } + + Assert.Equal(0UL, dataPoint.BucketCounts[bucketIndex]); + } + + Assert.Equal(1UL, dataPoint.BucketCounts[bucketIndex]); + if (attributes.Length > 0) { OtlpTestHelpers.AssertOtlpAttributes(attributes, dataPoint.Attributes);