Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
6 changes: 3 additions & 3 deletions docs/metrics/customizing-the-sdk/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public static void Main(string[] args)
// Rename an instrument to new name.
.AddView(instrumentName: "MyCounter", name: "MyCounterRenamed")

// Change Histogram bounds
.AddView(instrumentName: "MyHistogram", new HistogramConfiguration() { BucketBounds = new double[] { 10, 20 } })
// Change Histogram boundaries
.AddView(instrumentName: "MyHistogram", new ExplicitBucketHistogramConfiguration() { Boundaries = new double[] { 10, 20 } })

// For the instrument "MyCounterCustomTags", aggregate with only the keys "tag1", "tag2".
.AddView(instrumentName: "MyCounterCustomTags", new MetricStreamConfiguration() { TagKeys = new string[] { "tag1", "tag2" } })
Expand All @@ -48,7 +48,7 @@ public static void Main(string[] args)
if (instrument.Meter.Name.Equals("CompanyA.ProductB.Library2") &&
instrument.GetType().Name.Contains("Histogram"))
{
return new HistogramConfiguration() { BucketBounds = new double[] { 10, 20 } };
return new ExplicitBucketHistogramConfiguration() { Boundaries = new double[] { 10, 20 } };
}

return null;
Expand Down
14 changes: 7 additions & 7 deletions docs/metrics/customizing-the-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,14 @@ with the metric are of interest to you.
})
```

#### Specify custom bounds for Histogram
#### Specify custom boundaries for Histogram

By default, the bounds used for a Histogram are [`{ 0, 5, 10, 25, 50, 75, 100,
250, 500,
1000}`](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#explicit-bucket-histogram-aggregation).
Views can be used to provide custom bounds for a Histogram. The measurements are
then aggregated using the custom bounds provided instead of the the default
bounds. This requires the use of `HistogramConfiguration`.
bounds. This requires the use of `ExplicitBucketHistogramConfiguration`.

```csharp
// Change Histogram bounds to count measurements under the following buckets:
Expand All @@ -234,14 +234,14 @@ bounds. This requires the use of `HistogramConfiguration`.
// (20, +inf)
.AddView(
instrumentName: "MyHistogram",
new HistogramConfiguration{ BucketBounds = new double[] { 10, 20 } })
new ExplicitBucketHistogramConfiguration{ BucketBounds = new double[] { 10, 20 } })

// If you provide an empty `double` array as `BucketBounds` to the `HistogramConfiguration`,
// If you provide an empty `double` array as `BucketBounds` to the `ExplicitBucketHistogramConfiguration`,
Comment thread
cijothomas marked this conversation as resolved.
Outdated
// the SDK will only export the sum and count for the measurements.
// There are no buckets exported in this case.
.AddView(
instrumentName: "MyHistogram",
new HistogramConfiguration { BucketBounds = new double[] { } })
new ExplicitBucketHistogramConfiguration { BucketBounds = new double[] { } })
Comment thread
cijothomas marked this conversation as resolved.
Outdated
```

```csharp
Expand All @@ -251,8 +251,8 @@ bounds. This requires the use of `HistogramConfiguration`.
if (instrument.Meter.Name == "CompanyA.ProductB.LibraryC" &&
instrument.Name == "MyHistogram")
{
// `HistogramConfiguration` is a child class of `MetricStreamConfiguration`
return new HistogramConfiguration
// `ExplicitBucketHistogramConfiguration` is a child class of `MetricStreamConfiguration`
return new ExplicitBucketHistogramConfiguration
{
BucketBounds = new double[] { 10, 20 },
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="HistogramConfiguration.cs" company="OpenTelemetry Authors">
// <copyright file="ExplicitBucketHistogramConfiguration.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -18,17 +18,18 @@

namespace OpenTelemetry.Metrics
{
public class HistogramConfiguration : MetricStreamConfiguration
public class ExplicitBucketHistogramConfiguration : MetricStreamConfiguration
{
private Aggregation aggregation = Aggregation.Histogram;

/// <summary>
/// Gets or sets the custom histogram bounds.
/// Gets or sets the values representing explicit histogram bucket
/// boundary values.
/// </summary>
/// <remarks>
/// The array must be in ascending order with distinct values.
/// </remarks>
public double[] BucketBounds { get; set; }
public double[] Boundaries { get; set; }
Comment thread
cijothomas marked this conversation as resolved.

public override Aggregation Aggregation
{
Expand Down
8 changes: 4 additions & 4 deletions src/OpenTelemetry/Metrics/MeterProviderBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ public static MeterProviderBuilder AddView(this MeterProviderBuilder meterProvid
throw new ArgumentException($"Custom view name {metricStreamConfiguration.Name} is invalid.", nameof(metricStreamConfiguration.Name));
}

if (metricStreamConfiguration is HistogramConfiguration histogramConfiguration)
if (metricStreamConfiguration is ExplicitBucketHistogramConfiguration histogramConfiguration)
{
// Validate histogram bounds
if (histogramConfiguration.BucketBounds != null && !IsSortedAndDistinct(histogramConfiguration.BucketBounds))
// Validate histogram boundaries
if (histogramConfiguration.Boundaries != null && !IsSortedAndDistinct(histogramConfiguration.Boundaries))
{
throw new ArgumentException($"Histogram bounds must be in ascending order with distinct values", nameof(histogramConfiguration.BucketBounds));
throw new ArgumentException($"Histogram boundaries must be in ascending order with distinct values", nameof(histogramConfiguration.Boundaries));
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/OpenTelemetry/Metrics/MeterProviderSdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ internal MeterProviderSdk(
Metric metric;
var metricDescription = metricStreamConfig?.Description ?? instrument.Description;
string[] tagKeysInteresting = metricStreamConfig?.TagKeys;
double[] histogramBucketBounds = (metricStreamConfig is HistogramConfiguration histogramConfig
&& histogramConfig.BucketBounds != null) ? histogramConfig.BucketBounds : null;
double[] histogramBucketBounds = (metricStreamConfig is ExplicitBucketHistogramConfiguration histogramConfig
&& histogramConfig.Boundaries != null) ? histogramConfig.Boundaries : null;
metric = new Metric(instrument, temporality, metricName, metricDescription, histogramBucketBounds, tagKeysInteresting);

this.metrics[index] = metric;
Expand Down
10 changes: 5 additions & 5 deletions test/OpenTelemetry.Tests/Metrics/AggregatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public void HistogramDistributeToAllBucketsDefault()
[Fact]
public void HistogramDistributeToAllBucketsCustom()
{
var bounds = new double[] { 10, 20 };
var histogramPoint = new MetricPoint(AggregationType.Histogram, DateTimeOffset.Now, null, null, bounds);
var boundaries = new double[] { 10, 20 };
var histogramPoint = new MetricPoint(AggregationType.Histogram, DateTimeOffset.Now, null, null, boundaries);

// 5 recordings <=10
histogramPoint.Update(-10);
Expand All @@ -80,7 +80,7 @@ public void HistogramDistributeToAllBucketsCustom()

// Count = # of recordings
Assert.Equal(7, histogramPoint.LongValue);
Assert.Equal(bounds.Length + 1, histogramPoint.BucketCounts.Length);
Assert.Equal(boundaries.Length + 1, histogramPoint.BucketCounts.Length);
Assert.Equal(5, histogramPoint.BucketCounts[0]);
Assert.Equal(2, histogramPoint.BucketCounts[1]);
Assert.Equal(0, histogramPoint.BucketCounts[2]);
Expand All @@ -89,8 +89,8 @@ public void HistogramDistributeToAllBucketsCustom()
[Fact]
public void HistogramWithOnlySumCount()
{
var bounds = new double[] { };
var histogramPoint = new MetricPoint(AggregationType.HistogramSumCount, DateTimeOffset.Now, null, null, bounds);
var boundaries = new double[] { };
var histogramPoint = new MetricPoint(AggregationType.HistogramSumCount, DateTimeOffset.Now, null, null, boundaries);

histogramPoint.Update(-10);
histogramPoint.Update(0);
Expand Down
2 changes: 1 addition & 1 deletion test/OpenTelemetry.Tests/Metrics/MetricTestData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static IEnumerable<object[]> ValidInstrumentNames
new object[] { new string('m', 63) },
};

public static IEnumerable<object[]> InvalidHistogramBounds
public static IEnumerable<object[]> InvalidHistogramBoundaries
=> new List<object[]>
{
new object[] { new double[] { 0, 0 } },
Expand Down
16 changes: 8 additions & 8 deletions test/OpenTelemetry.Tests/Metrics/MetricViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ public void AddViewWithNullMetricStreamConfigurationThrowsArgumentnullException(
}

[Theory]
[MemberData(nameof(MetricTestData.InvalidHistogramBounds), MemberType = typeof(MetricTestData))]
public void AddViewWithInvalidHistogramBoundsThrowsArgumentException(double[] bounds)
[MemberData(nameof(MetricTestData.InvalidHistogramBoundaries), MemberType = typeof(MetricTestData))]
public void AddViewWithInvalidHistogramBoundsThrowsArgumentException(double[] boundaries)
{
var ex = Assert.Throws<ArgumentException>(() => Sdk.CreateMeterProviderBuilder()
.AddView("name1", new HistogramConfiguration { BucketBounds = bounds }));
.AddView("name1", new ExplicitBucketHistogramConfiguration { Boundaries = boundaries }));

Assert.Contains("Histogram bounds must be in ascending order with distinct values", ex.Message);
Assert.Contains("Histogram boundaries must be in ascending order with distinct values", ex.Message);
}

[Theory]
Expand Down Expand Up @@ -355,11 +355,11 @@ public void ViewToProduceCustomHistogramBound()
{
using var meter = new Meter(Utils.GetCurrentMethodName());
var exportedItems = new List<Metric>();
var bounds = new double[] { 10, 20 };
var boundaries = new double[] { 10, 20 };
using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddMeter(meter.Name)
.AddView("MyHistogram", new HistogramConfiguration() { Name = "MyHistogramDefaultBound" })
.AddView("MyHistogram", new HistogramConfiguration() { BucketBounds = bounds })
.AddView("MyHistogram", new ExplicitBucketHistogramConfiguration() { Name = "MyHistogramDefaultBound" })
.AddView("MyHistogram", new ExplicitBucketHistogramConfiguration() { Boundaries = boundaries })
.AddInMemoryExporter(exportedItems)
.Build();

Expand Down Expand Up @@ -409,7 +409,7 @@ public void ViewToProduceCustomHistogramBound()

Assert.Equal(40, histogramPoint.DoubleValue);
Assert.Equal(7, histogramPoint.LongValue);
Assert.Equal(bounds.Length + 1, histogramPoint.BucketCounts.Length);
Assert.Equal(boundaries.Length + 1, histogramPoint.BucketCounts.Length);
Assert.Equal(5, histogramPoint.BucketCounts[0]);
Assert.Equal(2, histogramPoint.BucketCounts[1]);
Assert.Equal(0, histogramPoint.BucketCounts[2]);
Expand Down