From bc4f7883832d1928db056e11e759f97fd629ed55 Mon Sep 17 00:00:00 2001 From: Gabriel Lucaci Date: Fri, 15 Jul 2022 08:17:26 +0200 Subject: [PATCH] Fix exact match in wildcard (#3446) * Add support for exact match --- src/OpenTelemetry/CHANGELOG.md | 3 ++ src/OpenTelemetry/Internal/WildcardHelper.cs | 2 +- .../Internal/WildcardHelperTests.cs | 41 +++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 test/OpenTelemetry.Tests/Internal/WildcardHelperTests.cs diff --git a/src/OpenTelemetry/CHANGELOG.md b/src/OpenTelemetry/CHANGELOG.md index 3b5674e684a..e006a6427cd 100644 --- a/src/OpenTelemetry/CHANGELOG.md +++ b/src/OpenTelemetry/CHANGELOG.md @@ -28,6 +28,9 @@ * `LogRecord` instances are now reused to reduce memory pressure ([#3385](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3385)) +* Fix exact match of activity source name when `wildcard` is used. + ([#3446](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3446)) + ## 1.3.0 Released 2022-Jun-03 diff --git a/src/OpenTelemetry/Internal/WildcardHelper.cs b/src/OpenTelemetry/Internal/WildcardHelper.cs index 778d2d883b8..8432286f409 100644 --- a/src/OpenTelemetry/Internal/WildcardHelper.cs +++ b/src/OpenTelemetry/Internal/WildcardHelper.cs @@ -42,6 +42,6 @@ public static Regex GetWildcardRegex(IEnumerable patterns = default) var convertedPattern = string.Join( "|", from p in patterns select "(?:" + Regex.Escape(p).Replace("\\*", ".*").Replace("\\?", ".") + ')'); - return new Regex('^' + convertedPattern + '$', RegexOptions.Compiled | RegexOptions.IgnoreCase); + return new Regex("^(?:" + convertedPattern + ")$", RegexOptions.Compiled | RegexOptions.IgnoreCase); } } diff --git a/test/OpenTelemetry.Tests/Internal/WildcardHelperTests.cs b/test/OpenTelemetry.Tests/Internal/WildcardHelperTests.cs new file mode 100644 index 00000000000..7adf9e8a5bc --- /dev/null +++ b/test/OpenTelemetry.Tests/Internal/WildcardHelperTests.cs @@ -0,0 +1,41 @@ +// +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +using Xunit; + +namespace OpenTelemetry.Internal.Tests +{ + public class WildcardHelperTests + { + [Theory] + [InlineData(new[] { "a" }, "a", true)] + [InlineData(new[] { "a.*" }, "a.b", true)] + [InlineData(new[] { "a" }, "a.b", false)] + [InlineData(new[] { "a", "x.*" }, "x.y", true)] + [InlineData(new[] { "a", "x.*" }, "a.b", false)] + [InlineData(new[] { "a", "x", "y" }, "abbbt", false)] + [InlineData(new[] { "a", "x", "y" }, "ccxccc", false)] + [InlineData(new[] { "a", "x", "y" }, "wecgy", false)] + public void WildcardRegex_ShouldMatch(string[] patterns, string matchWith, bool isMatch) + { + var regex = WildcardHelper.GetWildcardRegex(patterns); + + var result = regex.IsMatch(matchWith); + + Assert.True(result == isMatch); + } + } +}