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

Fail-fast when using AddView with guaranteed conflict #2785

Merged
merged 4 commits into from
Jan 14, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions src/OpenTelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
* Make `MetricPoint` of `MetricPointAccessor` readonly.
([2736](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2736))

* Fail-fast when using AddView with guaranteed conflict.
([2751](https://github.com/open-telemetry/opentelemetry-dotnet/issues/2751))

## 1.2.0-rc1

Released 2021-Nov-29
Expand Down
18 changes: 18 additions & 0 deletions src/OpenTelemetry/Metrics/MeterProviderBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ public static MeterProviderBuilder AddView(this MeterProviderBuilder meterProvid
throw new ArgumentException($"Custom view name {name} is invalid.", nameof(name));
}

if (instrumentName.IndexOf('*') != -1)
{
throw new ArgumentException(
cijothomas marked this conversation as resolved.
Show resolved Hide resolved
$"Instrument selection criteria is invalid. Instrument name '{instrumentName}' " +
$"contains a wildcard character. This is not allowed when using a view to " +
$"rename a metric stream as it would lead to conflicting metric stream names.",
nameof(instrumentName));
}

if (meterProviderBuilder is MeterProviderBuilderBase meterProviderBuilderBase)
{
return meterProviderBuilderBase.AddView(instrumentName, name);
Expand Down Expand Up @@ -86,6 +95,15 @@ public static MeterProviderBuilder AddView(this MeterProviderBuilder meterProvid
throw new ArgumentException($"Custom view name {metricStreamConfiguration.Name} is invalid.", nameof(metricStreamConfiguration.Name));
}

if (metricStreamConfiguration.Name != null && instrumentName.IndexOf('*') != -1)
{
throw new ArgumentException(
$"Instrument selection criteria is invalid. Instrument name '{instrumentName}' " +
$"contains a wildcard character. This is not allowed when using a view to " +
$"rename a metric stream as it would lead to conflicting metric stream names.",
nameof(instrumentName));
}

if (metricStreamConfiguration is ExplicitBucketHistogramConfiguration histogramConfiguration)
{
// Validate histogram boundaries
Expand Down
55 changes: 28 additions & 27 deletions test/OpenTelemetry.Tests/Metrics/MetricViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,34 @@ public void AddViewWithNullMetricStreamConfigurationThrowsArgumentnullException(
.Build());
}

[Fact]
public void AddViewWithNameThrowsInvalidArgumentExceptionWhenConflict()
{
var exportedItems = new List<Metric>();

using var meter1 = new Meter("AddViewWithGuaranteedConflictThrowsInvalidArgumentException");

Assert.Throws<ArgumentException>(() => Sdk.CreateMeterProviderBuilder()
.AddMeter(meter1.Name)
.AddView("instrumenta.*", name: "newname")
.AddInMemoryExporter(exportedItems)
.Build());
}

[Fact]
public void AddViewWithNameInMetricStreamConfigurationThrowsInvalidArgumentExceptionWhenConflict()
{
var exportedItems = new List<Metric>();

using var meter1 = new Meter("AddViewWithGuaranteedConflictThrowsInvalidArgumentException");

Assert.Throws<ArgumentException>(() => Sdk.CreateMeterProviderBuilder()
.AddMeter(meter1.Name)
.AddView("instrumenta.*", new MetricStreamConfiguration() { Name = "newname" })
.AddInMemoryExporter(exportedItems)
.Build());
}

[Theory]
[MemberData(nameof(MetricTestData.InvalidHistogramBoundaries), MemberType = typeof(MetricTestData))]
public void AddViewWithInvalidHistogramBoundsThrowsArgumentException(double[] boundaries)
Expand Down Expand Up @@ -277,33 +305,6 @@ public void ViewWithNullCustomNameTakesInstrumentName()
Assert.Equal(counter1.Name, metric.Name);
}

[Fact]
public void ViewToRenameMetricWildCardMatch()
{
using var meter = new Meter(Utils.GetCurrentMethodName());
var exportedItems = new List<Metric>();
using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddMeter(meter.Name)
.AddView("counter*", "renamed")
.AddInMemoryExporter(exportedItems)
.Build();

// Expecting one metric stream.
var counter1 = meter.CreateCounter<long>("counterA");
counter1.Add(10);
var counter2 = meter.CreateCounter<long>("counterB");
counter2.Add(10);
var counter3 = meter.CreateCounter<long>("counterC");
counter3.Add(10);
meterProvider.ForceFlush(MaxTimeToAllowForFlush);

// counter* matches all 3 instruments which all
// becomes "renamed" and only 1st one is exported.
Assert.Single(exportedItems);
var metric = exportedItems[0];
Assert.Equal("renamed", metric.Name);
}

[Fact]
public void ViewToProduceMultipleStreamsFromInstrument()
{
Expand Down