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

## Unreleased

* Fix issue where `ExplicitBucketHistogramConfiguration` could be used to
Comment thread
cijothomas marked this conversation as resolved.
configure metric streams for instruments that are not histograms.
([#3126](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3126))

## 1.2.0-rc4

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

if (metricStreamConfig is ExplicitBucketHistogramConfiguration
&& instrument.GetType().GetGenericTypeDefinition() != typeof(Histogram<>))
{
Comment thread
cijothomas marked this conversation as resolved.
metricStreamConfig = null;
Comment thread
cijothomas marked this conversation as resolved.
}
}
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