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

Do not apply ExplicitBucketHistogramConfiguration to non-histograms #3126

6 changes: 6 additions & 0 deletions src/OpenTelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## Unreleased

* Fix issue where `ExplicitBucketHistogramConfiguration` could be used to
cijothomas marked this conversation as resolved.
Show resolved Hide resolved
configure metric streams for instruments that are not histograms. Currently,
it is not possible to change the aggregation of an instrument with views. This
may be possible in the future.
([#3126](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3126))

## 1.2.0-rc4

Released 2022-Mar-30
Expand Down
10 changes: 10 additions & 0 deletions src/OpenTelemetry/Metrics/MeterProviderSdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,16 @@ internal MeterProviderSdk(
try
{
metricStreamConfig = viewConfig(instrument);

if (metricStreamConfig is ExplicitBucketHistogramConfiguration
&& instrument.GetType().GetGenericTypeDefinition() != typeof(Histogram<>))
{
cijothomas marked this conversation as resolved.
Show resolved Hide resolved
OpenTelemetrySdkEventSource.Log.MetricViewIgnored(
instrument.Name,
instrument.Meter.Name,
"Histogram metric stream configuration can not be used for instruments that are not histograms.",
cijothomas marked this conversation as resolved.
Show resolved Hide resolved
"Fix the view configuration.");
}
}
catch (Exception ex)
{
Expand Down
31 changes: 31 additions & 0 deletions test/OpenTelemetry.Tests/Metrics/MetricViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,37 @@ public void ViewToProduceMultipleStreamsWithDuplicatesFromInstrument()
Assert.Equal("renamedStream2", exportedItems[1].Name);
}

[Fact]
public void ViewWithHistogramConfiguraionIgnoredWhenAppliedToNonHistogram()
{
using var meter = new Meter(Utils.GetCurrentMethodName());
var exportedItems = new List<Metric>();
using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddMeter(meter.Name)
.AddView("NotAHistogram", new ExplicitBucketHistogramConfiguration() { Name = "ImAHistogram" })
.AddInMemoryExporter(exportedItems)
.Build();

var counter = meter.CreateCounter<long>("NotAHistogram");
counter.Add(10);
meterProvider.ForceFlush(MaxTimeToAllowForFlush);

Assert.Single(exportedItems);
var metric = exportedItems[0];

Assert.Equal("NotAHistogram", metric.Name);

List<MetricPoint> metricPoints = new List<MetricPoint>();
foreach (ref readonly var mp in metric.GetMetricPoints())
{
metricPoints.Add(mp);
}

Assert.Single(metricPoints);
var metricPoint = metricPoints[0];
Assert.Equal(10, metricPoint.GetSumLong());
}

[Fact]
public void ViewToProduceCustomHistogramBound()
{
Expand Down