diff --git a/src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.cs b/src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.cs index 63a76846e55..064c1dad14a 100644 --- a/src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.cs +++ b/src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.cs @@ -101,7 +101,7 @@ public override ExportResult Export(in Batch batch) bool isFirstIteration = true; double previousExplicitBound = default; - foreach (var histogramMeasurement in metricPoint.HistogramBuckets) + foreach (var histogramMeasurement in metricPoint.GetHistogramBuckets()) { if (isFirstIteration) { diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/MetricItemExtensions.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/MetricItemExtensions.cs index 28c66974db8..e85cdcda231 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/MetricItemExtensions.cs +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/MetricItemExtensions.cs @@ -249,7 +249,7 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric) dataPoint.Count = (ulong)metricPoint.LongValue; dataPoint.Sum = metricPoint.DoubleValue; - foreach (var histogramMeasurement in metricPoint.HistogramBuckets) + foreach (var histogramMeasurement in metricPoint.GetHistogramBuckets()) { dataPoint.BucketCounts.Add((ulong)histogramMeasurement.BucketCount); if (histogramMeasurement.ExplicitBound != double.PositiveInfinity) diff --git a/src/OpenTelemetry.Exporter.Prometheus/Implementation/PrometheusSerializerExt.cs b/src/OpenTelemetry.Exporter.Prometheus/Implementation/PrometheusSerializerExt.cs index c7e07742791..ea554caa82b 100644 --- a/src/OpenTelemetry.Exporter.Prometheus/Implementation/PrometheusSerializerExt.cs +++ b/src/OpenTelemetry.Exporter.Prometheus/Implementation/PrometheusSerializerExt.cs @@ -92,7 +92,7 @@ public static int WriteMetric(byte[] buffer, int cursor, Metric metric) var timestamp = metricPoint.EndTime.ToUnixTimeMilliseconds(); long totalCount = 0; - foreach (var histogramMeasurement in metricPoint.HistogramBuckets) + foreach (var histogramMeasurement in metricPoint.GetHistogramBuckets()) { totalCount += histogramMeasurement.BucketCount; diff --git a/src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt index 6971a01db58..abde8e7515a 100644 --- a/src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt @@ -23,7 +23,7 @@ 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.HistogramBuckets.get -> OpenTelemetry.Metrics.HistogramBuckets +OpenTelemetry.Metrics.MetricPoint.GetHistogramBuckets() -> OpenTelemetry.Metrics.HistogramBuckets OpenTelemetry.Metrics.MetricPointsAccessor OpenTelemetry.Metrics.MetricPointsAccessor.MetricPointsAccessor() -> void OpenTelemetry.Metrics.MetricPointsAccessor.Dispose() -> void diff --git a/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt index 6971a01db58..abde8e7515a 100644 --- a/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt @@ -23,7 +23,7 @@ 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.HistogramBuckets.get -> OpenTelemetry.Metrics.HistogramBuckets +OpenTelemetry.Metrics.MetricPoint.GetHistogramBuckets() -> OpenTelemetry.Metrics.HistogramBuckets OpenTelemetry.Metrics.MetricPointsAccessor OpenTelemetry.Metrics.MetricPointsAccessor.MetricPointsAccessor() -> void OpenTelemetry.Metrics.MetricPointsAccessor.Dispose() -> void diff --git a/src/OpenTelemetry/CHANGELOG.md b/src/OpenTelemetry/CHANGELOG.md index 6771c21cf9a..e9cace8ad15 100644 --- a/src/OpenTelemetry/CHANGELOG.md +++ b/src/OpenTelemetry/CHANGELOG.md @@ -14,9 +14,11 @@ aggregation is not implemented yet. ([#2660](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2660)) -* Renamed `HistogramMeasurements` to `HistogramBuckets` and added an enumerator - of type `HistogramBucket` for enumerating `BucketCounts` and `ExplicitBounds`. - Removed `GetBucketCounts` and `GetExplicitBounds` methods from `MetricPoint`. +* Removed the public property `HistogramMeasurements` and added a public method + `GetHistogramBuckets` instead. Renamed the class `HistogramMeasurements` to + `HistogramBuckets` and added an enumerator of type `HistogramBucket` for + enumerating `BucketCounts` and `ExplicitBounds`. Removed `GetBucketCounts` and + `GetExplicitBounds` methods from `MetricPoint`. ([#2664](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2664)) ## 1.2.0-beta2 diff --git a/src/OpenTelemetry/Metrics/MetricPoint.cs b/src/OpenTelemetry/Metrics/MetricPoint.cs index 4dc0a5ab9dc..2e696a3bb8e 100644 --- a/src/OpenTelemetry/Metrics/MetricPoint.cs +++ b/src/OpenTelemetry/Metrics/MetricPoint.cs @@ -23,6 +23,7 @@ namespace OpenTelemetry.Metrics public struct MetricPoint { private readonly AggregationType aggType; + private readonly HistogramBuckets histogramBuckets; private long longVal; private long lastLongSum; private double doubleVal; @@ -51,15 +52,15 @@ internal MetricPoint( if (this.aggType == AggregationType.Histogram) { - this.HistogramBuckets = new HistogramBuckets(histogramBounds); + this.histogramBuckets = new HistogramBuckets(histogramBounds); } else if (this.aggType == AggregationType.HistogramSumCount) { - this.HistogramBuckets = new HistogramBuckets(null); + this.histogramBuckets = new HistogramBuckets(null); } else { - this.HistogramBuckets = null; + this.histogramBuckets = null; } } @@ -76,15 +77,13 @@ internal MetricPoint( public double DoubleValue { get; internal set; } - public readonly HistogramBuckets HistogramBuckets { get; } - internal MetricPointStatus MetricPointStatus { get; private set; } public long GetHistogramCount() { if (this.aggType == AggregationType.Histogram || this.aggType == AggregationType.HistogramSumCount) { - return this.HistogramBuckets.Count; + return this.histogramBuckets.Count; } else { @@ -96,7 +95,7 @@ public double GetHistogramSum() { if (this.aggType == AggregationType.Histogram || this.aggType == AggregationType.HistogramSumCount) { - return this.HistogramBuckets.Sum; + return this.histogramBuckets.Sum; } else { @@ -104,6 +103,18 @@ public double GetHistogramSum() } } + public HistogramBuckets GetHistogramBuckets() + { + if (this.aggType == AggregationType.Histogram || this.aggType == AggregationType.HistogramSumCount) + { + return this.histogramBuckets; + } + else + { + throw new NotSupportedException($"{nameof(this.GetHistogramBuckets)} is not supported for this metric type."); + } + } + internal void Update(long number) { switch (this.aggType) @@ -179,20 +190,20 @@ internal void Update(double number) case AggregationType.Histogram: { int i; - for (i = 0; i < this.HistogramBuckets.ExplicitBounds.Length; i++) + for (i = 0; i < this.histogramBuckets.ExplicitBounds.Length; i++) { // Upper bound is inclusive - if (number <= this.HistogramBuckets.ExplicitBounds[i]) + if (number <= this.histogramBuckets.ExplicitBounds[i]) { break; } } - lock (this.HistogramBuckets.LockObject) + lock (this.histogramBuckets.LockObject) { - this.HistogramBuckets.CountVal++; - this.HistogramBuckets.SumVal += number; - this.HistogramBuckets.BucketCounts[i]++; + this.histogramBuckets.CountVal++; + this.histogramBuckets.SumVal += number; + this.histogramBuckets.BucketCounts[i]++; } break; @@ -200,10 +211,10 @@ internal void Update(double number) case AggregationType.HistogramSumCount: { - lock (this.HistogramBuckets.LockObject) + lock (this.histogramBuckets.LockObject) { - this.HistogramBuckets.CountVal++; - this.HistogramBuckets.SumVal += number; + this.histogramBuckets.CountVal++; + this.histogramBuckets.SumVal += number; } break; @@ -325,22 +336,22 @@ internal void TakeSnapshot(bool outputDelta) case AggregationType.Histogram: { - lock (this.HistogramBuckets.LockObject) + lock (this.histogramBuckets.LockObject) { - this.HistogramBuckets.Count = this.HistogramBuckets.CountVal; - this.HistogramBuckets.Sum = this.HistogramBuckets.SumVal; + this.histogramBuckets.Count = this.histogramBuckets.CountVal; + this.histogramBuckets.Sum = this.histogramBuckets.SumVal; if (outputDelta) { - this.HistogramBuckets.CountVal = 0; - this.HistogramBuckets.SumVal = 0; + this.histogramBuckets.CountVal = 0; + this.histogramBuckets.SumVal = 0; } - for (int i = 0; i < this.HistogramBuckets.BucketCounts.Length; i++) + for (int i = 0; i < this.histogramBuckets.BucketCounts.Length; i++) { - this.HistogramBuckets.AggregatedBucketCounts[i] = this.HistogramBuckets.BucketCounts[i]; + this.histogramBuckets.AggregatedBucketCounts[i] = this.histogramBuckets.BucketCounts[i]; if (outputDelta) { - this.HistogramBuckets.BucketCounts[i] = 0; + this.histogramBuckets.BucketCounts[i] = 0; } } @@ -352,14 +363,14 @@ internal void TakeSnapshot(bool outputDelta) case AggregationType.HistogramSumCount: { - lock (this.HistogramBuckets.LockObject) + lock (this.histogramBuckets.LockObject) { - this.HistogramBuckets.Count = this.HistogramBuckets.CountVal; - this.HistogramBuckets.Sum = this.HistogramBuckets.SumVal; + this.histogramBuckets.Count = this.histogramBuckets.CountVal; + this.histogramBuckets.Sum = this.histogramBuckets.SumVal; if (outputDelta) { - this.HistogramBuckets.CountVal = 0; - this.HistogramBuckets.SumVal = 0; + this.histogramBuckets.CountVal = 0; + this.histogramBuckets.SumVal = 0; } this.MetricPointStatus = MetricPointStatus.NoCollectPending; diff --git a/test/OpenTelemetry.Tests/Metrics/AggregatorTest.cs b/test/OpenTelemetry.Tests/Metrics/AggregatorTest.cs index a08904be899..8a276f5d618 100644 --- a/test/OpenTelemetry.Tests/Metrics/AggregatorTest.cs +++ b/test/OpenTelemetry.Tests/Metrics/AggregatorTest.cs @@ -54,7 +54,7 @@ public void HistogramDistributeToAllBucketsDefault() Assert.Equal(22, count); int actualCount = 0; - foreach (var histogramMeasurement in histogramPoint.HistogramBuckets) + foreach (var histogramMeasurement in histogramPoint.GetHistogramBuckets()) { Assert.Equal(2, histogramMeasurement.BucketCount); actualCount++; @@ -92,7 +92,7 @@ public void HistogramDistributeToAllBucketsCustom() int index = 0; int actualCount = 0; var expectedBucketCounts = new long[] { 5, 2, 0 }; - foreach (var histogramMeasurement in histogramPoint.HistogramBuckets) + foreach (var histogramMeasurement in histogramPoint.GetHistogramBuckets()) { Assert.Equal(expectedBucketCounts[index], histogramMeasurement.BucketCount); index++; @@ -128,7 +128,7 @@ public void HistogramWithOnlySumCount() Assert.Equal(7, count); // There should be no enumeration of BucketCounts and ExplicitBounds for HistogramSumCount - var enumerator = histogramPoint.HistogramBuckets.GetEnumerator(); + var enumerator = histogramPoint.GetHistogramBuckets().GetEnumerator(); Assert.False(enumerator.MoveNext()); } } diff --git a/test/OpenTelemetry.Tests/Metrics/MetricViewTests.cs b/test/OpenTelemetry.Tests/Metrics/MetricViewTests.cs index 152b242c4e5..d3e356072bc 100644 --- a/test/OpenTelemetry.Tests/Metrics/MetricViewTests.cs +++ b/test/OpenTelemetry.Tests/Metrics/MetricViewTests.cs @@ -397,7 +397,7 @@ public void ViewToProduceCustomHistogramBound() int index = 0; int actualCount = 0; var expectedBucketCounts = new long[] { 2, 1, 2, 2, 0, 0, 0, 0, 0, 0, 0 }; - foreach (var histogramMeasurement in histogramPoint.HistogramBuckets) + foreach (var histogramMeasurement in histogramPoint.GetHistogramBuckets()) { Assert.Equal(expectedBucketCounts[index], histogramMeasurement.BucketCount); index++; @@ -424,7 +424,7 @@ public void ViewToProduceCustomHistogramBound() index = 0; actualCount = 0; expectedBucketCounts = new long[] { 5, 2, 0 }; - foreach (var histogramMeasurement in histogramPoint.HistogramBuckets) + foreach (var histogramMeasurement in histogramPoint.GetHistogramBuckets()) { Assert.Equal(expectedBucketCounts[index], histogramMeasurement.BucketCount); index++;