Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update MetricPoint for Histograms #2657

Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,38 +95,42 @@ public override ExportResult Export(in Batch<Metric> batch)
if (metricType.IsHistogram())
{
var bucketsBuilder = new StringBuilder();
bucketsBuilder.Append($"Sum: {metricPoint.DoubleValue} Count: {metricPoint.LongValue} \n");
var sum = metricPoint.GetHistogramSum();
var count = metricPoint.GetHistogramCount();
bucketsBuilder.Append($"Sum: {sum} Count: {count} \n");

if (metricPoint.ExplicitBounds != null)
var explicitBounds = metricPoint.GetExplicitBounds();
if (explicitBounds != null)
{
for (int i = 0; i < metricPoint.ExplicitBounds.Length + 1; i++)
var bucketCounts = metricPoint.GetBucketCounts();
for (int i = 0; i < explicitBounds.Length + 1; i++)
{
if (i == 0)
{
bucketsBuilder.Append("(-Infinity,");
bucketsBuilder.Append(metricPoint.ExplicitBounds[i]);
bucketsBuilder.Append(explicitBounds[i]);
bucketsBuilder.Append(']');
bucketsBuilder.Append(':');
bucketsBuilder.Append(metricPoint.BucketCounts[i]);
bucketsBuilder.Append(bucketCounts[i]);
}
else if (i == metricPoint.ExplicitBounds.Length)
else if (i == explicitBounds.Length)
{
bucketsBuilder.Append('(');
bucketsBuilder.Append(metricPoint.ExplicitBounds[i - 1]);
bucketsBuilder.Append(explicitBounds[i - 1]);
bucketsBuilder.Append(',');
bucketsBuilder.Append("+Infinity]");
bucketsBuilder.Append(':');
bucketsBuilder.Append(metricPoint.BucketCounts[i]);
bucketsBuilder.Append(bucketCounts[i]);
}
else
{
bucketsBuilder.Append('(');
bucketsBuilder.Append(metricPoint.ExplicitBounds[i - 1]);
bucketsBuilder.Append(explicitBounds[i - 1]);
bucketsBuilder.Append(',');
bucketsBuilder.Append(metricPoint.ExplicitBounds[i]);
bucketsBuilder.Append(explicitBounds[i]);
bucketsBuilder.Append(']');
bucketsBuilder.Append(':');
bucketsBuilder.Append(metricPoint.BucketCounts[i]);
bucketsBuilder.Append(bucketCounts[i]);
}

bucketsBuilder.AppendLine();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,16 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric)
dataPoint.Count = (ulong)metricPoint.LongValue;
dataPoint.Sum = metricPoint.DoubleValue;

if (metricPoint.BucketCounts != null)
var bucketCounts = metricPoint.GetBucketCounts();
if (bucketCounts != null)
{
for (int i = 0; i < metricPoint.BucketCounts.Length; i++)
var explicitBounds = metricPoint.GetExplicitBounds();
for (int i = 0; i < bucketCounts.Length; i++)
{
dataPoint.BucketCounts.Add((ulong)metricPoint.BucketCounts[i]);
if (i < metricPoint.BucketCounts.Length - 1)
dataPoint.BucketCounts.Add((ulong)bucketCounts[i]);
if (i < bucketCounts.Length - 1)
{
dataPoint.ExplicitBounds.Add(metricPoint.ExplicitBounds[i]);
dataPoint.ExplicitBounds.Add(explicitBounds[i]);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ public static int WriteMetric(byte[] buffer, int cursor, Metric metric)
var tags = metricPoint.Tags;
cijothomas marked this conversation as resolved.
Show resolved Hide resolved
var timestamp = metricPoint.EndTime.ToUnixTimeMilliseconds();

if (metricPoint.BucketCounts != null)
var bucketCounts = metricPoint.GetBucketCounts();
if (bucketCounts != null)
{
// Histogram buckets
var bucketCounts = metricPoint.BucketCounts;
var explicitBounds = metricPoint.ExplicitBounds;
var explicitBounds = metricPoint.GetExplicitBounds();
long totalCount = 0;
for (int idxBound = 0; idxBound < explicitBounds.Length + 1; idxBound++)
{
Expand Down Expand Up @@ -133,6 +133,9 @@ public static int WriteMetric(byte[] buffer, int cursor, Metric metric)
}

// Histogram sum
var count = metricPoint.GetHistogramCount();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to declare these variables here, or just use them inline?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed this in #2664

var sum = metricPoint.GetHistogramSum();

cursor = WriteMetricName(buffer, cursor, metric.Name, metric.Unit);
cursor = WriteAsciiStringNoEscape(buffer, cursor, "_sum");

Expand All @@ -156,7 +159,7 @@ public static int WriteMetric(byte[] buffer, int cursor, Metric metric)

buffer[cursor++] = unchecked((byte)' ');

cursor = WriteDouble(buffer, cursor, metricPoint.DoubleValue);
cursor = WriteDouble(buffer, cursor, sum);
buffer[cursor++] = unchecked((byte)' ');

cursor = WriteLong(buffer, cursor, timestamp);
Expand Down Expand Up @@ -187,7 +190,7 @@ public static int WriteMetric(byte[] buffer, int cursor, Metric metric)

buffer[cursor++] = unchecked((byte)' ');

cursor = WriteLong(buffer, cursor, metricPoint.LongValue);
cursor = WriteLong(buffer, cursor, count);
buffer[cursor++] = unchecked((byte)' ');

cursor = WriteLong(buffer, cursor, timestamp);
Expand Down
6 changes: 4 additions & 2 deletions src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,12 @@ 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.BucketCounts.get -> long[]
OpenTelemetry.Metrics.MetricPoint.DoubleValue.get -> double
OpenTelemetry.Metrics.MetricPoint.EndTime.get -> System.DateTimeOffset
OpenTelemetry.Metrics.MetricPoint.ExplicitBounds.get -> double[]
OpenTelemetry.Metrics.MetricPoint.GetBucketCounts() -> long[]
OpenTelemetry.Metrics.MetricPoint.GetExplicitBounds() -> double[]
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,12 @@ 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.BucketCounts.get -> long[]
OpenTelemetry.Metrics.MetricPoint.DoubleValue.get -> double
OpenTelemetry.Metrics.MetricPoint.EndTime.get -> System.DateTimeOffset
OpenTelemetry.Metrics.MetricPoint.ExplicitBounds.get -> double[]
OpenTelemetry.Metrics.MetricPoint.GetBucketCounts() -> long[]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as a follow up we need to do something similar to whats done for Tags. (readonly something)

OpenTelemetry.Metrics.MetricPoint.GetExplicitBounds() -> double[]
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
Expand Down
8 changes: 6 additions & 2 deletions src/OpenTelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
`Keys`+`Values`
([#2642](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2642))

* Refactored `MetricPoint` and added public methods: `GetBucketCounts`,
`GetExplicitBounds`, `GetHistogramCount`, and `GetHistogramSum`
([#2657](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2657))

## 1.2.0-beta2

Released 2021-Nov-19

* Renamed `HistogramConfiguration` to `ExplicitBucketHistogramConfiguration`
and changed its member `BucketBounds` to `Boundaries`.
* Renamed `HistogramConfiguration` to `ExplicitBucketHistogramConfiguration` and
changed its member `BucketBounds` to `Boundaries`.
([#2638](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2638))

* Metrics with the same name but from different meters are allowed.
Expand Down
45 changes: 45 additions & 0 deletions src/OpenTelemetry/Metrics/HistogramMeasurements.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// <copyright file="HistogramMeasurements.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

namespace OpenTelemetry.Metrics
{
internal class HistogramMeasurements
cijothomas marked this conversation as resolved.
Show resolved Hide resolved
{
internal readonly long[] BucketCounts;

internal readonly long[] AggregatedBucketCounts;

internal readonly double[] ExplicitBounds;

internal readonly object LockObject;

internal long CountVal;

internal long Count;

internal double SumVal;

internal double Sum;

internal HistogramMeasurements(double[] histogramBounds)
{
this.ExplicitBounds = histogramBounds;
this.BucketCounts = histogramBounds != null ? new long[histogramBounds.Length + 1] : null;
this.AggregatedBucketCounts = histogramBounds != null ? new long[histogramBounds.Length + 1] : null;
this.LockObject = new object();
}
}
}
Loading