Skip to content
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
Comment thread
cijothomas marked this conversation as 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<>))
{
Comment thread
cijothomas marked this conversation as resolved.
OpenTelemetrySdkEventSource.Log.MetricViewIgnored(
instrument.Name,
instrument.Meter.Name,
"Histogram metric stream configuration can not be used for instruments that are not histograms.",
Comment thread
cijothomas marked this conversation as resolved.
Outdated
"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