Support question mark in wildcard (#2875)
This commit is contained in:
parent
782626920a
commit
1b5765bf43
|
|
@ -2,6 +2,10 @@
|
|||
|
||||
## Unreleased
|
||||
|
||||
* Improved wildcard support for `AddSource`, `AddMeter` to cover `?` (which
|
||||
matches exactly one character).
|
||||
([#2875](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2875))
|
||||
|
||||
## 1.2.0-rc2
|
||||
|
||||
Released 2022-Feb-02
|
||||
|
|
|
|||
|
|
@ -0,0 +1,47 @@
|
|||
// <copyright file="WildcardHelper.cs" company="OpenTelemetry Authors">
|
||||
// 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.
|
||||
// </copyright>
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace OpenTelemetry;
|
||||
|
||||
internal static class WildcardHelper
|
||||
{
|
||||
public static bool ContainsWildcard(string value)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return value.Contains('*') || value.Contains('?');
|
||||
}
|
||||
|
||||
public static Regex GetWildcardRegex(IEnumerable<string> patterns = default)
|
||||
{
|
||||
if (patterns == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var convertedPattern = string.Join(
|
||||
"|",
|
||||
from p in patterns select "(?:" + Regex.Escape(p).Replace("\\*", ".*").Replace("\\?", ".") + ')');
|
||||
return new Regex('^' + convertedPattern + '$', RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
||||
}
|
||||
}
|
||||
|
|
@ -19,7 +19,6 @@ using System.Collections.Generic;
|
|||
using System.Diagnostics;
|
||||
using System.Diagnostics.Metrics;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using OpenTelemetry.Internal;
|
||||
using OpenTelemetry.Resources;
|
||||
|
||||
|
|
@ -82,9 +81,9 @@ namespace OpenTelemetry.Metrics
|
|||
|
||||
// Setup Listener
|
||||
Func<Instrument, bool> shouldListenTo = instrument => false;
|
||||
if (meterSources.Any(s => s.Contains('*')))
|
||||
if (meterSources.Any(s => WildcardHelper.ContainsWildcard(s)))
|
||||
{
|
||||
var regex = GetWildcardRegex(meterSources);
|
||||
var regex = WildcardHelper.GetWildcardRegex(meterSources);
|
||||
shouldListenTo = instrument => regex.IsMatch(instrument.Meter.Name);
|
||||
}
|
||||
else if (meterSources.Any())
|
||||
|
|
@ -235,12 +234,6 @@ namespace OpenTelemetry.Metrics
|
|||
}
|
||||
|
||||
this.listener.Start();
|
||||
|
||||
static Regex GetWildcardRegex(IEnumerable<string> collection)
|
||||
{
|
||||
var pattern = '^' + string.Join("|", from name in collection select "(?:" + Regex.Escape(name).Replace("\\*", ".*") + ')') + '$';
|
||||
return new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
||||
}
|
||||
}
|
||||
|
||||
internal Resource Resource { get; }
|
||||
|
|
|
|||
|
|
@ -19,7 +19,6 @@ using System.Collections.Generic;
|
|||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text.RegularExpressions;
|
||||
using OpenTelemetry.Internal;
|
||||
using OpenTelemetry.Resources;
|
||||
|
||||
|
|
@ -50,13 +49,13 @@ namespace OpenTelemetry.Trace
|
|||
this.supportLegacyActivity = legacyActivityOperationNames.Count > 0;
|
||||
|
||||
bool legacyActivityWildcardMode = false;
|
||||
Regex legacyActivityWildcardModeRegex = null;
|
||||
var legacyActivityWildcardModeRegex = WildcardHelper.GetWildcardRegex();
|
||||
foreach (var legacyName in legacyActivityOperationNames)
|
||||
{
|
||||
if (legacyName.Contains('*'))
|
||||
if (WildcardHelper.ContainsWildcard(legacyName))
|
||||
{
|
||||
legacyActivityWildcardMode = true;
|
||||
legacyActivityWildcardModeRegex = GetWildcardRegex(legacyActivityOperationNames);
|
||||
legacyActivityWildcardModeRegex = WildcardHelper.GetWildcardRegex(legacyActivityOperationNames);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
@ -211,27 +210,15 @@ namespace OpenTelemetry.Trace
|
|||
this.getRequestedDataAction = this.RunGetRequestedDataOtherSampler;
|
||||
}
|
||||
|
||||
// Sources can be null. This happens when user
|
||||
// is only interested in InstrumentationLibraries
|
||||
// which do not depend on ActivitySources.
|
||||
if (sources.Any())
|
||||
{
|
||||
// Sources can be null. This happens when user
|
||||
// is only interested in InstrumentationLibraries
|
||||
// which do not depend on ActivitySources.
|
||||
|
||||
var wildcardMode = false;
|
||||
|
||||
// Validation of source name is already done in builder.
|
||||
foreach (var name in sources)
|
||||
if (sources.Any(s => WildcardHelper.ContainsWildcard(s)))
|
||||
{
|
||||
if (name.Contains('*'))
|
||||
{
|
||||
wildcardMode = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (wildcardMode)
|
||||
{
|
||||
var regex = GetWildcardRegex(sources);
|
||||
var regex = WildcardHelper.GetWildcardRegex(sources);
|
||||
|
||||
// Function which takes ActivitySource and returns true/false to indicate if it should be subscribed to
|
||||
// or not.
|
||||
|
|
@ -264,12 +251,6 @@ namespace OpenTelemetry.Trace
|
|||
|
||||
ActivitySource.AddActivityListener(listener);
|
||||
this.listener = listener;
|
||||
|
||||
Regex GetWildcardRegex(IEnumerable<string> collection)
|
||||
{
|
||||
var pattern = '^' + string.Join("|", from name in collection select "(?:" + Regex.Escape(name).Replace("\\*", ".*") + ')') + '$';
|
||||
return new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
||||
}
|
||||
}
|
||||
|
||||
internal Resource Resource { get; }
|
||||
|
|
|
|||
|
|
@ -249,7 +249,7 @@ namespace OpenTelemetry.Metrics.Tests
|
|||
|
||||
var exportedItems = new List<Metric>();
|
||||
var meterProviderBuilder = Sdk.CreateMeterProviderBuilder()
|
||||
.AddMeter("AbcCompany.XyzProduct.*")
|
||||
.AddMeter("AbcCompany.XyzProduct.Component?")
|
||||
.AddMeter("DefCompany.*.ComponentC")
|
||||
.AddMeter("GhiCompany.qweProduct.ComponentN") // Mixing of non-wildcard meter name and wildcard meter name.
|
||||
.AddInMemoryExporter(exportedItems);
|
||||
|
|
|
|||
|
|
@ -34,6 +34,86 @@ namespace OpenTelemetry.Trace.Tests
|
|||
Activity.DefaultIdFormat = ActivityIdFormat.W3C;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TracerProviderSdkAddSource()
|
||||
{
|
||||
using var source1 = new ActivitySource($"{Utils.GetCurrentMethodName()}.1");
|
||||
using var source2 = new ActivitySource($"{Utils.GetCurrentMethodName()}.2");
|
||||
|
||||
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
|
||||
.AddSource(source1.Name)
|
||||
.Build();
|
||||
|
||||
using (var activity = source1.StartActivity("test"))
|
||||
{
|
||||
Assert.NotNull(activity);
|
||||
}
|
||||
|
||||
using (var activity = source2.StartActivity("test"))
|
||||
{
|
||||
Assert.Null(activity);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TracerProviderSdkAddSourceWithWildcards()
|
||||
{
|
||||
using var source1 = new ActivitySource($"{Utils.GetCurrentMethodName()}.A");
|
||||
using var source2 = new ActivitySource($"{Utils.GetCurrentMethodName()}.Ab");
|
||||
using var source3 = new ActivitySource($"{Utils.GetCurrentMethodName()}.Abc");
|
||||
using var source4 = new ActivitySource($"{Utils.GetCurrentMethodName()}.B");
|
||||
|
||||
using (var tracerProvider = Sdk.CreateTracerProviderBuilder()
|
||||
.AddSource($"{Utils.GetCurrentMethodName()}.*")
|
||||
.Build())
|
||||
{
|
||||
using (var activity = source1.StartActivity("test"))
|
||||
{
|
||||
Assert.NotNull(activity);
|
||||
}
|
||||
|
||||
using (var activity = source2.StartActivity("test"))
|
||||
{
|
||||
Assert.NotNull(activity);
|
||||
}
|
||||
|
||||
using (var activity = source3.StartActivity("test"))
|
||||
{
|
||||
Assert.NotNull(activity);
|
||||
}
|
||||
|
||||
using (var activity = source4.StartActivity("test"))
|
||||
{
|
||||
Assert.NotNull(activity);
|
||||
}
|
||||
}
|
||||
|
||||
using (var tracerProvider = Sdk.CreateTracerProviderBuilder()
|
||||
.AddSource($"{Utils.GetCurrentMethodName()}.?")
|
||||
.Build())
|
||||
{
|
||||
using (var activity = source1.StartActivity("test"))
|
||||
{
|
||||
Assert.NotNull(activity);
|
||||
}
|
||||
|
||||
using (var activity = source2.StartActivity("test"))
|
||||
{
|
||||
Assert.Null(activity);
|
||||
}
|
||||
|
||||
using (var activity = source3.StartActivity("test"))
|
||||
{
|
||||
Assert.Null(activity);
|
||||
}
|
||||
|
||||
using (var activity = source4.StartActivity("test"))
|
||||
{
|
||||
Assert.NotNull(activity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TracerProviderSdkInvokesSamplingWithCorrectParameters()
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue