From 9e83587a5507ce8d803193df79a896d816ec3af3 Mon Sep 17 00:00:00 2001 From: Utkarsh Umesan Pillai Date: Wed, 24 Nov 2021 11:46:06 -0800 Subject: [PATCH] Update Metric Point for Counter and Gauge (#2667) --- .../ConsoleMetricExporter.cs | 22 ++++- .../Implementation/MetricItemExtensions.cs | 28 +++--- .../Implementation/PrometheusSerializerExt.cs | 22 ++++- .../.publicApi/net461/PublicAPI.Unshipped.txt | 10 ++- .../netstandard2.0/PublicAPI.Unshipped.txt | 10 ++- src/OpenTelemetry/CHANGELOG.md | 6 ++ src/OpenTelemetry/Metrics/MetricPoint.cs | 85 +++++++++++++++---- .../Metrics/MetricCollectBenchmarks.cs | 4 +- .../Metrics/InMemoryExporterTests.cs | 6 +- .../Metrics/MetricAPITest.cs | 22 ++++- .../Metrics/MultipleReadersTests.cs | 9 +- 11 files changed, 169 insertions(+), 55 deletions(-) diff --git a/src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.cs b/src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.cs index ca535e5c953..cfe3023e170 100644 --- a/src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.cs +++ b/src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.cs @@ -138,18 +138,32 @@ public override ExportResult Export(in Batch batch) } else if (metricType.IsDouble()) { - valueDisplay = metricPoint.DoubleValue.ToString(CultureInfo.InvariantCulture); + if (metricType.IsSum()) + { + valueDisplay = metricPoint.GetCounterSumDouble().ToString(CultureInfo.InvariantCulture); + } + else + { + valueDisplay = metricPoint.GetGaugeLastValueDouble().ToString(CultureInfo.InvariantCulture); + } } else if (metricType.IsLong()) { - valueDisplay = metricPoint.LongValue.ToString(CultureInfo.InvariantCulture); + if (metricType.IsSum()) + { + valueDisplay = metricPoint.GetCounterSumLong().ToString(CultureInfo.InvariantCulture); + } + else + { + valueDisplay = metricPoint.GetGaugeLastValueLong().ToString(CultureInfo.InvariantCulture); + } } msg = new StringBuilder(); msg.Append('('); - msg.Append(metricPoint.StartTime.ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ", CultureInfo.InvariantCulture)); + msg.Append(metricPoint.GetStartTime().ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ", CultureInfo.InvariantCulture)); msg.Append(", "); - msg.Append(metricPoint.EndTime.ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ", CultureInfo.InvariantCulture)); + msg.Append(metricPoint.GetEndTime().ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ", CultureInfo.InvariantCulture)); msg.Append("] "); msg.Append(tags); if (tags != string.Empty) diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/MetricItemExtensions.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/MetricItemExtensions.cs index 45b11f5d543..2d1da9504b0 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/MetricItemExtensions.cs +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/MetricItemExtensions.cs @@ -152,13 +152,13 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric) { var dataPoint = new OtlpMetrics.NumberDataPoint { - StartTimeUnixNano = (ulong)metricPoint.StartTime.ToUnixTimeNanoseconds(), - TimeUnixNano = (ulong)metricPoint.EndTime.ToUnixTimeNanoseconds(), + StartTimeUnixNano = (ulong)metricPoint.GetStartTime().ToUnixTimeNanoseconds(), + TimeUnixNano = (ulong)metricPoint.GetEndTime().ToUnixTimeNanoseconds(), }; AddAttributes(metricPoint.Tags, dataPoint.Attributes); - dataPoint.AsInt = metricPoint.LongValue; + dataPoint.AsInt = metricPoint.GetCounterSumLong(); sum.DataPoints.Add(dataPoint); } @@ -176,13 +176,13 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric) { var dataPoint = new OtlpMetrics.NumberDataPoint { - StartTimeUnixNano = (ulong)metricPoint.StartTime.ToUnixTimeNanoseconds(), - TimeUnixNano = (ulong)metricPoint.EndTime.ToUnixTimeNanoseconds(), + StartTimeUnixNano = (ulong)metricPoint.GetStartTime().ToUnixTimeNanoseconds(), + TimeUnixNano = (ulong)metricPoint.GetEndTime().ToUnixTimeNanoseconds(), }; AddAttributes(metricPoint.Tags, dataPoint.Attributes); - dataPoint.AsDouble = metricPoint.DoubleValue; + dataPoint.AsDouble = metricPoint.GetCounterSumDouble(); sum.DataPoints.Add(dataPoint); } @@ -197,13 +197,13 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric) { var dataPoint = new OtlpMetrics.NumberDataPoint { - StartTimeUnixNano = (ulong)metricPoint.StartTime.ToUnixTimeNanoseconds(), - TimeUnixNano = (ulong)metricPoint.EndTime.ToUnixTimeNanoseconds(), + StartTimeUnixNano = (ulong)metricPoint.GetStartTime().ToUnixTimeNanoseconds(), + TimeUnixNano = (ulong)metricPoint.GetEndTime().ToUnixTimeNanoseconds(), }; AddAttributes(metricPoint.Tags, dataPoint.Attributes); - dataPoint.AsInt = metricPoint.LongValue; + dataPoint.AsInt = metricPoint.GetGaugeLastValueLong(); gauge.DataPoints.Add(dataPoint); } @@ -218,13 +218,13 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric) { var dataPoint = new OtlpMetrics.NumberDataPoint { - StartTimeUnixNano = (ulong)metricPoint.StartTime.ToUnixTimeNanoseconds(), - TimeUnixNano = (ulong)metricPoint.EndTime.ToUnixTimeNanoseconds(), + StartTimeUnixNano = (ulong)metricPoint.GetStartTime().ToUnixTimeNanoseconds(), + TimeUnixNano = (ulong)metricPoint.GetEndTime().ToUnixTimeNanoseconds(), }; AddAttributes(metricPoint.Tags, dataPoint.Attributes); - dataPoint.AsDouble = metricPoint.DoubleValue; + dataPoint.AsDouble = metricPoint.GetGaugeLastValueDouble(); gauge.DataPoints.Add(dataPoint); } @@ -241,8 +241,8 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric) { var dataPoint = new OtlpMetrics.HistogramDataPoint { - StartTimeUnixNano = (ulong)metricPoint.StartTime.ToUnixTimeNanoseconds(), - TimeUnixNano = (ulong)metricPoint.EndTime.ToUnixTimeNanoseconds(), + StartTimeUnixNano = (ulong)metricPoint.GetStartTime().ToUnixTimeNanoseconds(), + TimeUnixNano = (ulong)metricPoint.GetEndTime().ToUnixTimeNanoseconds(), }; AddAttributes(metricPoint.Tags, dataPoint.Attributes); diff --git a/src/OpenTelemetry.Exporter.Prometheus/Implementation/PrometheusSerializerExt.cs b/src/OpenTelemetry.Exporter.Prometheus/Implementation/PrometheusSerializerExt.cs index ea554caa82b..3be3d1edeb8 100644 --- a/src/OpenTelemetry.Exporter.Prometheus/Implementation/PrometheusSerializerExt.cs +++ b/src/OpenTelemetry.Exporter.Prometheus/Implementation/PrometheusSerializerExt.cs @@ -40,7 +40,7 @@ public static int WriteMetric(byte[] buffer, int cursor, Metric metric) foreach (ref var metricPoint in metric.GetMetricPoints()) { var tags = metricPoint.Tags; - var timestamp = metricPoint.EndTime.ToUnixTimeMilliseconds(); + var timestamp = metricPoint.GetEndTime().ToUnixTimeMilliseconds(); // Counter and Gauge cursor = WriteMetricName(buffer, cursor, metric.Name, metric.Unit); @@ -70,11 +70,25 @@ public static int WriteMetric(byte[] buffer, int cursor, Metric metric) // for each MetricPoint if (((int)metric.MetricType & 0b_0000_1111) == 0x0a /* I8 */) { - cursor = WriteLong(buffer, cursor, metricPoint.LongValue); + if (metric.MetricType.IsSum()) + { + cursor = WriteLong(buffer, cursor, metricPoint.GetCounterSumLong()); + } + else + { + cursor = WriteLong(buffer, cursor, metricPoint.GetGaugeLastValueLong()); + } } else { - cursor = WriteDouble(buffer, cursor, metricPoint.DoubleValue); + if (metric.MetricType.IsSum()) + { + cursor = WriteDouble(buffer, cursor, metricPoint.GetCounterSumDouble()); + } + else + { + cursor = WriteDouble(buffer, cursor, metricPoint.GetGaugeLastValueDouble()); + } } buffer[cursor++] = unchecked((byte)' '); @@ -89,7 +103,7 @@ public static int WriteMetric(byte[] buffer, int cursor, Metric metric) foreach (ref var metricPoint in metric.GetMetricPoints()) { var tags = metricPoint.Tags; - var timestamp = metricPoint.EndTime.ToUnixTimeMilliseconds(); + var timestamp = metricPoint.GetEndTime().ToUnixTimeMilliseconds(); long totalCount = 0; foreach (var histogramMeasurement in metricPoint.GetHistogramBuckets()) diff --git a/src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt index 9d03d52e435..e6b63dcadc4 100644 --- a/src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt @@ -21,7 +21,13 @@ OpenTelemetry.Metrics.HistogramBuckets.Enumerator.Current.get -> OpenTelemetry.M OpenTelemetry.Metrics.HistogramBuckets.Enumerator.Enumerator() -> void OpenTelemetry.Metrics.HistogramBuckets.Enumerator.MoveNext() -> bool OpenTelemetry.Metrics.HistogramBuckets.GetEnumerator() -> OpenTelemetry.Metrics.HistogramBuckets.Enumerator +OpenTelemetry.Metrics.MetricPoint.GetCounterSumDouble() -> double +OpenTelemetry.Metrics.MetricPoint.GetCounterSumLong() -> long +OpenTelemetry.Metrics.MetricPoint.GetEndTime() -> System.DateTimeOffset +OpenTelemetry.Metrics.MetricPoint.GetGaugeLastValueDouble() -> double +OpenTelemetry.Metrics.MetricPoint.GetGaugeLastValueLong() -> long OpenTelemetry.Metrics.MetricPoint.GetHistogramBuckets() -> OpenTelemetry.Metrics.HistogramBuckets +OpenTelemetry.Metrics.MetricPoint.GetStartTime() -> System.DateTimeOffset OpenTelemetry.Metrics.MetricPointsAccessor OpenTelemetry.Metrics.MetricPointsAccessor.MetricPointsAccessor() -> void OpenTelemetry.Metrics.MetricPointsAccessor.Dispose() -> void @@ -59,13 +65,9 @@ OpenTelemetry.Metrics.Metric.Name.get -> string OpenTelemetry.Metrics.Metric.Temporality.get -> OpenTelemetry.Metrics.AggregationTemporality OpenTelemetry.Metrics.Metric.Unit.get -> string OpenTelemetry.Metrics.MetricPoint -OpenTelemetry.Metrics.MetricPoint.DoubleValue.get -> double -OpenTelemetry.Metrics.MetricPoint.EndTime.get -> System.DateTimeOffset OpenTelemetry.Metrics.MetricPoint.GetHistogramCount() -> long OpenTelemetry.Metrics.MetricPoint.GetHistogramSum() -> double -OpenTelemetry.Metrics.MetricPoint.LongValue.get -> long OpenTelemetry.Metrics.MetricPoint.MetricPoint() -> void -OpenTelemetry.Metrics.MetricPoint.StartTime.get -> System.DateTimeOffset OpenTelemetry.Metrics.MetricPoint.Tags.get -> OpenTelemetry.ReadOnlyTagCollection OpenTelemetry.Metrics.MetricReader OpenTelemetry.Metrics.MetricReader.Collect(int timeoutMilliseconds = -1) -> bool diff --git a/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt index 9d03d52e435..e6b63dcadc4 100644 --- a/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt @@ -21,7 +21,13 @@ OpenTelemetry.Metrics.HistogramBuckets.Enumerator.Current.get -> OpenTelemetry.M OpenTelemetry.Metrics.HistogramBuckets.Enumerator.Enumerator() -> void OpenTelemetry.Metrics.HistogramBuckets.Enumerator.MoveNext() -> bool OpenTelemetry.Metrics.HistogramBuckets.GetEnumerator() -> OpenTelemetry.Metrics.HistogramBuckets.Enumerator +OpenTelemetry.Metrics.MetricPoint.GetCounterSumDouble() -> double +OpenTelemetry.Metrics.MetricPoint.GetCounterSumLong() -> long +OpenTelemetry.Metrics.MetricPoint.GetEndTime() -> System.DateTimeOffset +OpenTelemetry.Metrics.MetricPoint.GetGaugeLastValueDouble() -> double +OpenTelemetry.Metrics.MetricPoint.GetGaugeLastValueLong() -> long OpenTelemetry.Metrics.MetricPoint.GetHistogramBuckets() -> OpenTelemetry.Metrics.HistogramBuckets +OpenTelemetry.Metrics.MetricPoint.GetStartTime() -> System.DateTimeOffset OpenTelemetry.Metrics.MetricPointsAccessor OpenTelemetry.Metrics.MetricPointsAccessor.MetricPointsAccessor() -> void OpenTelemetry.Metrics.MetricPointsAccessor.Dispose() -> void @@ -59,13 +65,9 @@ OpenTelemetry.Metrics.Metric.Name.get -> string OpenTelemetry.Metrics.Metric.Temporality.get -> OpenTelemetry.Metrics.AggregationTemporality OpenTelemetry.Metrics.Metric.Unit.get -> string OpenTelemetry.Metrics.MetricPoint -OpenTelemetry.Metrics.MetricPoint.DoubleValue.get -> double -OpenTelemetry.Metrics.MetricPoint.EndTime.get -> System.DateTimeOffset OpenTelemetry.Metrics.MetricPoint.GetHistogramCount() -> long OpenTelemetry.Metrics.MetricPoint.GetHistogramSum() -> double -OpenTelemetry.Metrics.MetricPoint.LongValue.get -> long OpenTelemetry.Metrics.MetricPoint.MetricPoint() -> void -OpenTelemetry.Metrics.MetricPoint.StartTime.get -> System.DateTimeOffset OpenTelemetry.Metrics.MetricPoint.Tags.get -> OpenTelemetry.ReadOnlyTagCollection OpenTelemetry.Metrics.MetricReader OpenTelemetry.Metrics.MetricReader.Collect(int timeoutMilliseconds = -1) -> bool diff --git a/src/OpenTelemetry/CHANGELOG.md b/src/OpenTelemetry/CHANGELOG.md index b4c160336dd..0594704e3be 100644 --- a/src/OpenTelemetry/CHANGELOG.md +++ b/src/OpenTelemetry/CHANGELOG.md @@ -24,6 +24,12 @@ * Refactored temporality setting to align with the latest spec. ([#2666](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2666)) +* Removed the public properties `LongValue`, `DoubleValue`, `StartTime`, and + `EndTime` in favor of their counterpart public methods `GetCounterSumLong`, + `GetCounterSumDouble`, `GetGaugeLastValueLong`, `GetGaugeLastValueDouble`, + `GetStartTime`, and `GetEndTime`. + ([#2667](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2667)) + ## 1.2.0-beta2 Released 2021-Nov-19 diff --git a/src/OpenTelemetry/Metrics/MetricPoint.cs b/src/OpenTelemetry/Metrics/MetricPoint.cs index 2e696a3bb8e..754960abf40 100644 --- a/src/OpenTelemetry/Metrics/MetricPoint.cs +++ b/src/OpenTelemetry/Metrics/MetricPoint.cs @@ -22,11 +22,16 @@ namespace OpenTelemetry.Metrics { public struct MetricPoint { + internal DateTimeOffset StartTime; + internal DateTimeOffset EndTime; + private readonly AggregationType aggType; private readonly HistogramBuckets histogramBuckets; private long longVal; + private long longValue; private long lastLongSum; private double doubleVal; + private double doubleValue; private double lastDoubleSum; internal MetricPoint( @@ -42,10 +47,10 @@ internal MetricPoint( this.StartTime = startTime; this.Tags = new ReadOnlyTagCollection(keys, values); this.EndTime = default; - this.LongValue = default; + this.longValue = default; this.longVal = default; this.lastLongSum = default; - this.DoubleValue = default; + this.doubleValue = default; this.doubleVal = default; this.lastDoubleSum = default; this.MetricPointStatus = MetricPointStatus.NoCollectPending; @@ -69,15 +74,65 @@ internal MetricPoint( /// public ReadOnlyTagCollection Tags { get; } - public DateTimeOffset StartTime { get; internal set; } + internal MetricPointStatus MetricPointStatus { get; private set; } - public DateTimeOffset EndTime { get; internal set; } + public DateTimeOffset GetStartTime() + { + return this.StartTime; + } - public long LongValue { get; internal set; } + public DateTimeOffset GetEndTime() + { + return this.EndTime; + } - public double DoubleValue { get; internal set; } + public long GetCounterSumLong() + { + if (this.aggType == AggregationType.LongSumIncomingDelta || this.aggType == AggregationType.LongSumIncomingCumulative) + { + return this.longValue; + } + else + { + throw new NotSupportedException($"{nameof(this.GetCounterSumLong)} is not supported for this metric type."); + } + } - internal MetricPointStatus MetricPointStatus { get; private set; } + public double GetCounterSumDouble() + { + if (this.aggType == AggregationType.DoubleSumIncomingDelta || this.aggType == AggregationType.DoubleSumIncomingCumulative) + { + return this.doubleValue; + } + else + { + throw new NotSupportedException($"{nameof(this.GetCounterSumDouble)} is not supported for this metric type."); + } + } + + public long GetGaugeLastValueLong() + { + if (this.aggType == AggregationType.LongGauge) + { + return this.longValue; + } + else + { + throw new NotSupportedException($"{nameof(this.GetGaugeLastValueLong)} is not supported for this metric type."); + } + } + + public double GetGaugeLastValueDouble() + { + if (this.aggType == AggregationType.DoubleGauge) + { + return this.doubleValue; + } + else + { + throw new NotSupportedException($"{nameof(this.GetGaugeLastValueDouble)} is not supported for this metric type."); + } + } public long GetHistogramCount() { @@ -245,7 +300,7 @@ internal void TakeSnapshot(bool outputDelta) if (outputDelta) { long initValue = Interlocked.Read(ref this.longVal); - this.LongValue = initValue - this.lastLongSum; + this.longValue = initValue - this.lastLongSum; this.lastLongSum = initValue; this.MetricPointStatus = MetricPointStatus.NoCollectPending; @@ -258,7 +313,7 @@ internal void TakeSnapshot(bool outputDelta) } else { - this.LongValue = Interlocked.Read(ref this.longVal); + this.longValue = Interlocked.Read(ref this.longVal); } break; @@ -275,7 +330,7 @@ internal void TakeSnapshot(bool outputDelta) // the exchange (to 0.0) will never occur, // but we get the original value atomically. double initValue = Interlocked.CompareExchange(ref this.doubleVal, 0.0, double.NegativeInfinity); - this.DoubleValue = initValue - this.lastDoubleSum; + this.doubleValue = initValue - this.lastDoubleSum; this.lastDoubleSum = initValue; this.MetricPointStatus = MetricPointStatus.NoCollectPending; @@ -293,7 +348,7 @@ internal void TakeSnapshot(bool outputDelta) // As long as the value is not -ve infinity, // the exchange (to 0.0) will never occur, // but we get the original value atomically. - this.DoubleValue = Interlocked.CompareExchange(ref this.doubleVal, 0.0, double.NegativeInfinity); + this.doubleValue = Interlocked.CompareExchange(ref this.doubleVal, 0.0, double.NegativeInfinity); } break; @@ -301,12 +356,12 @@ internal void TakeSnapshot(bool outputDelta) case AggregationType.LongGauge: { - this.LongValue = Interlocked.Read(ref this.longVal); + this.longValue = Interlocked.Read(ref this.longVal); this.MetricPointStatus = MetricPointStatus.NoCollectPending; // Check again if value got updated, if yes reset status. // This ensures no Updates get Lost. - if (this.LongValue != Interlocked.Read(ref this.longVal)) + if (this.longValue != Interlocked.Read(ref this.longVal)) { this.MetricPointStatus = MetricPointStatus.CollectPending; } @@ -321,12 +376,12 @@ internal void TakeSnapshot(bool outputDelta) // As long as the value is not -ve infinity, // the exchange (to 0.0) will never occur, // but we get the original value atomically. - this.DoubleValue = Interlocked.CompareExchange(ref this.doubleVal, 0.0, double.NegativeInfinity); + this.doubleValue = Interlocked.CompareExchange(ref this.doubleVal, 0.0, double.NegativeInfinity); this.MetricPointStatus = MetricPointStatus.NoCollectPending; // Check again if value got updated, if yes reset status. // This ensures no Updates get Lost. - if (this.DoubleValue != Interlocked.CompareExchange(ref this.doubleVal, 0.0, double.NegativeInfinity)) + if (this.doubleValue != Interlocked.CompareExchange(ref this.doubleVal, 0.0, double.NegativeInfinity)) { this.MetricPointStatus = MetricPointStatus.CollectPending; } diff --git a/test/Benchmarks/Metrics/MetricCollectBenchmarks.cs b/test/Benchmarks/Metrics/MetricCollectBenchmarks.cs index 231d3b34a4f..4fa6c2f7667 100644 --- a/test/Benchmarks/Metrics/MetricCollectBenchmarks.cs +++ b/test/Benchmarks/Metrics/MetricCollectBenchmarks.cs @@ -71,7 +71,7 @@ void ProcessExport(Batch batch) // The performant way of iterating. foreach (ref var metricPoint in metric.GetMetricPoints()) { - sum += metricPoint.LongValue; + sum += metricPoint.GetCounterSumDouble(); } } else @@ -80,7 +80,7 @@ void ProcessExport(Batch batch) // This is still "correct", but less performant. foreach (var metricPoint in metric.GetMetricPoints()) { - sum += metricPoint.LongValue; + sum += metricPoint.GetCounterSumDouble(); } } } diff --git a/test/OpenTelemetry.Tests/Metrics/InMemoryExporterTests.cs b/test/OpenTelemetry.Tests/Metrics/InMemoryExporterTests.cs index 77cbd7516b2..95060d411f6 100644 --- a/test/OpenTelemetry.Tests/Metrics/InMemoryExporterTests.cs +++ b/test/OpenTelemetry.Tests/Metrics/InMemoryExporterTests.cs @@ -49,7 +49,7 @@ public void InMemoryExporterShouldDeepCopyMetricPoints() var metricPointsEnumerator = metric.GetMetricPoints().GetEnumerator(); Assert.True(metricPointsEnumerator.MoveNext()); // One MetricPoint is emitted for the Metric ref var metricPointForFirstExport = ref metricPointsEnumerator.Current; - Assert.Equal(10, metricPointForFirstExport.LongValue); + Assert.Equal(10, metricPointForFirstExport.GetCounterSumLong()); // Emit 25 for the MetricPoint with a single key-vaue pair: ("tag1", "value1") counter.Add(25, new KeyValuePair("tag1", "value1")); @@ -60,10 +60,10 @@ public void InMemoryExporterShouldDeepCopyMetricPoints() metricPointsEnumerator = metric.GetMetricPoints().GetEnumerator(); Assert.True(metricPointsEnumerator.MoveNext()); // One MetricPoint is emitted for the Metric var metricPointForSecondExport = metricPointsEnumerator.Current; - Assert.Equal(25, metricPointForSecondExport.LongValue); + Assert.Equal(25, metricPointForSecondExport.GetCounterSumLong()); // MetricPoint.LongValue for the first exporter metric should still be 10 - Assert.Equal(10, metricPointForFirstExport.LongValue); + Assert.Equal(10, metricPointForFirstExport.GetCounterSumLong()); } } } diff --git a/test/OpenTelemetry.Tests/Metrics/MetricAPITest.cs b/test/OpenTelemetry.Tests/Metrics/MetricAPITest.cs index ad9d9abe40d..051c4191d84 100644 --- a/test/OpenTelemetry.Tests/Metrics/MetricAPITest.cs +++ b/test/OpenTelemetry.Tests/Metrics/MetricAPITest.cs @@ -65,7 +65,7 @@ public void ObserverCallbackTest() Assert.Single(metricPoints); var metricPoint = metricPoints[0]; - Assert.Equal(100, metricPoint.LongValue); + Assert.Equal(100, metricPoint.GetGaugeLastValueLong()); Assert.True(metricPoint.Tags.Count > 0); } @@ -95,7 +95,7 @@ public void ObserverCallbackExceptionTest() Assert.Single(metricPoints); var metricPoint = metricPoints[0]; - Assert.Equal(100, metricPoint.LongValue); + Assert.Equal(100, metricPoint.GetGaugeLastValueLong()); Assert.True(metricPoint.Tags.Count > 0); } @@ -698,7 +698,14 @@ private static long GetLongSum(List metrics) { foreach (ref var metricPoint in metric.GetMetricPoints()) { - sum += metricPoint.LongValue; + if (metric.MetricType.IsSum()) + { + sum += metricPoint.GetCounterSumLong(); + } + else + { + sum += metricPoint.GetGaugeLastValueLong(); + } } } @@ -712,7 +719,14 @@ private static double GetDoubleSum(List metrics) { foreach (ref var metricPoint in metric.GetMetricPoints()) { - sum += metricPoint.DoubleValue; + if (metric.MetricType.IsSum()) + { + sum += metricPoint.GetCounterSumDouble(); + } + else + { + sum += metricPoint.GetGaugeLastValueDouble(); + } } } diff --git a/test/OpenTelemetry.Tests/Metrics/MultipleReadersTests.cs b/test/OpenTelemetry.Tests/Metrics/MultipleReadersTests.cs index f0e1b7f6593..575ac330be3 100644 --- a/test/OpenTelemetry.Tests/Metrics/MultipleReadersTests.cs +++ b/test/OpenTelemetry.Tests/Metrics/MultipleReadersTests.cs @@ -113,7 +113,14 @@ private void AssertLongSumValueForMetric(Metric metric, long value) var metricPointsEnumerator = metricPoints.GetEnumerator(); Assert.True(metricPointsEnumerator.MoveNext()); // One MetricPoint is emitted for the Metric ref var metricPointForFirstExport = ref metricPointsEnumerator.Current; - Assert.Equal(value, metricPointForFirstExport.LongValue); + if (metric.MetricType.IsSum()) + { + Assert.Equal(value, metricPointForFirstExport.GetCounterSumLong()); + } + else + { + Assert.Equal(value, metricPointForFirstExport.GetGaugeLastValueLong()); + } } } }