Skip to content

Commit

Permalink
add support for regex
Browse files Browse the repository at this point in the history
  • Loading branch information
reyang committed Aug 7, 2020
1 parent e43356a commit 190d3dd
Showing 1 changed file with 46 additions and 8 deletions.
54 changes: 46 additions & 8 deletions src/OpenTelemetry/Sdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using OpenTelemetry.Metrics;
using OpenTelemetry.Metrics.Export;
Expand Down Expand Up @@ -163,11 +165,29 @@ public static TracerProvider CreateTracerProvider(Action<TracerProviderBuilder>

public static TracerProvider CreateTracerProvider(IEnumerable<string> sources, Sampler sampler = null, Resource resource = null)
{
var activitySources = new Dictionary<string, bool>(StringComparer.OrdinalIgnoreCase);
if (sources == null)
{
throw new ArgumentNullException(nameof(sources));
}

if (!sources.Any())
{
throw new ArgumentException($"{nameof(sources)} collection is empty.");
}

var wildcardMode = false;

foreach (var name in sources)
{
activitySources[name] = true;
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentException($"{nameof(sources)} collection contains null or whitespace strings.");
}

if (name.Contains('*'))
{
wildcardMode = true;
}
}

var provider = new TracerProviderSdk
Expand All @@ -176,7 +196,7 @@ public static TracerProvider CreateTracerProvider(IEnumerable<string> sources, S
Sampler = sampler,
};

provider.ActivityListener = new ActivityListener
var listener = new ActivityListener
{
// Callback when Activity is started.
ActivityStarted = (activity) =>
Expand All @@ -195,10 +215,6 @@ public static TracerProvider CreateTracerProvider(IEnumerable<string> sources, S
provider.ActivityProcessor?.OnEnd(activity);
},

// Function which takes ActivitySource and returns true/false to indicate if it should be subscribed to
// or not.
ShouldListenTo = (activitySource) => activitySources.ContainsKey(activitySource.Name),

// Setting this to true means TraceId will be always
// available in sampling callbacks and will be the actual
// traceid used, if activity ends up getting created.
Expand All @@ -208,7 +224,29 @@ public static TracerProvider CreateTracerProvider(IEnumerable<string> sources, S
GetRequestedDataUsingContext = (ref ActivityCreationOptions<ActivityContext> options) => ComputeActivityDataRequest(options, sampler),
};

ActivitySource.AddActivityListener(provider.ActivityListener);
if (wildcardMode)
{
var pattern = "^(" + string.Join("|", from name in sources select '(' + Regex.Escape(name).Replace("\\*", ".*") + ')') + ")$";
var regex = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
listener.ShouldListenTo = (activitySource) => regex.IsMatch(activitySource.Name);
}
else
{
var activitySources = new Dictionary<string, bool>(StringComparer.OrdinalIgnoreCase);

foreach (var name in sources)
{
activitySources[name] = true;
}

// Function which takes ActivitySource and returns true/false to indicate if it should be subscribed to
// or not.
listener.ShouldListenTo = (activitySource) => activitySources.ContainsKey(activitySource.Name);
}

ActivitySource.AddActivityListener(listener);

provider.ActivityListener = listener;

return provider;
}
Expand Down

0 comments on commit 190d3dd

Please sign in to comment.