Skip to content
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
6 changes: 6 additions & 0 deletions src/OpenTelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ Notes](../../RELEASENOTES.md).

## Unreleased

* Fixed `NotSupportedException` thrown when building a `TracerProvider` or
`MeterProvider` with a large number of wildcard source/meter patterns on
`net8.0`, and a related `OutOfMemoryException` that could occur when many
such providers were built over the lifetime of a process.
([#7788](https://github.com/open-telemetry/opentelemetry-dotnet/pull/7788))

## 1.19.0

Released 2026-Sep-18
Expand Down
11 changes: 5 additions & 6 deletions src/OpenTelemetry/Internal/WildcardHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ namespace OpenTelemetry;

internal static class WildcardHelper
{
#if !NET
private static readonly TimeSpan RegexMatchTimeout = TimeSpan.FromSeconds(1);
#endif

public static bool ContainsWildcard(
[NotNullWhen(true)]
Expand Down Expand Up @@ -43,11 +41,12 @@ public static Regex GetWildcardRegex(IEnumerable<string> patterns)

var pattern = "^(?:" + convertedPattern + ")$";

#if NET
return new Regex(pattern, RegexOptions.NonBacktracking | RegexOptions.IgnoreCase);
#else
// RegexOptions.NonBacktracking is not used as it has a fixed automata-size limit that many
// source patterns can exceed and it retains a much larger automaton per Regex instance
// than a backtracking regex, which can lead to an OutOfMemoryException in applications.
// The match timeout bounds worst-case matching time to protect against catastrophic backtracking.
// See https://github.com/open-telemetry/opentelemetry-dotnet/issues/7787.
return new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase, RegexMatchTimeout);
#endif
}

public static bool IsMatch(Regex regex, string input)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,19 @@ public static MeterProviderBuilder AddView(this MeterProviderBuilder meterProvid
if (instrumentName.Contains('*'))
#endif
{
#if NET
var pattern = '^' + Regex.Escape(instrumentName).Replace("\\*", ".*", StringComparison.Ordinal);
var regex = new Regex(pattern, RegexOptions.NonBacktracking | RegexOptions.IgnoreCase);
#elif NETSTANDARD2_1_OR_GREATER
#if NET || NETSTANDARD2_1_OR_GREATER
var pattern = '^' + Regex.Escape(instrumentName).Replace("\\*", ".*", StringComparison.Ordinal);
var regex = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase, TimeSpan.FromSeconds(1));
#else
var pattern = '^' + Regex.Escape(instrumentName).Replace("\\*", ".*");
var regex = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase, TimeSpan.FromSeconds(1));
#endif

// RegexOptions.NonBacktracking is not used as it retains a much larger automaton
// per Regex instance than a backtracking regex, which can lead to an
// OutOfMemoryException in applications that build many providers with wildcard
// views over their lifetime. The match timeout bounds worst-case matching time
// to protect against catastrophic backtracking.
// See https://github.com/open-telemetry/opentelemetry-dotnet/issues/7787.
var regex = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase, TimeSpan.FromSeconds(1));
meterProviderBuilderSdk.AddView(instrument => WildcardHelper.IsMatch(regex, instrument.Name) ? metricStreamConfiguration : null);
}
else
Expand Down
29 changes: 29 additions & 0 deletions test/OpenTelemetry.Tests/Internal/WildcardHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,35 @@ public void GetWildcardRegex_DoesNotCatastrophicallyBacktrack()
Assert.True(sw.Elapsed < TimeSpan.FromSeconds(5), $"Non-matching input took {sw.Elapsed.TotalSeconds:F1}s.");
}

[Fact]
public void GetWildcardRegex_HandlesLargeNumberOfPatterns()
{
var patterns = Enumerable.Range(0, 300).Select(i => $"Some.Namespace.Source.{i}.*").ToArray();

var regex = WildcardHelper.GetWildcardRegex(patterns);

Assert.True(WildcardHelper.IsMatch(regex, "Some.Namespace.Source.42.Foo"));
Assert.False(WildcardHelper.IsMatch(regex, "Some.Other.Namespace"));
}

[Fact]
public void GetWildcardRegex_HandlesRealWorldSourcePatternCombination()
{
var patterns = new[]
{
"SomeApplication.App",
"AWSSDK.*",
"System.Net.Http",
"OpenTelemetry.Instrumentation.AWSLambda",
};

var regex = WildcardHelper.GetWildcardRegex(patterns);

Assert.True(WildcardHelper.IsMatch(regex, "SomeApplication.App"));
Assert.True(WildcardHelper.IsMatch(regex, "AWSSDK.DynamoDB"));
Assert.False(WildcardHelper.IsMatch(regex, "Unrelated.Source"));
}

[Theory]
[InlineData(new[] { "a" }, "a", true)]
[InlineData(new[] { "a.*" }, "a.b", true)]
Expand Down
14 changes: 14 additions & 0 deletions test/OpenTelemetry.Tests/Metrics/MetricViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,20 @@ public void AddViewWithNameInMetricStreamConfigurationThrowsInvalidArgumentExcep
.AddInMemoryExporter(exportedItems)));
}

[Fact]
public void AddViewWithLargeWildcardPatternDoesNotThrow()
{
var instrumentName = string.Concat(Enumerable.Repeat("a*", 1000));

var exportedItems = new List<Metric>();

using var meter = new Meter(Utils.GetCurrentMethodName());
using var container = BuildMeterProvider(out var meterProvider, builder => builder
.AddMeter(meter.Name)
.AddView(instrumentName, new MetricStreamConfiguration())
.AddInMemoryExporter(exportedItems));
}

[Fact]
public void AddViewWithExceptionInUserCallbackAppliedDefault()
{
Expand Down
Loading