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

Add support for regex #1023

Closed
wants to merge 4 commits into from
Closed
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
8 changes: 6 additions & 2 deletions src/OpenTelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

## Unreleased

* Renaming `BroadcastActivityProcessor` to `FanOutActivityProcessor` (#1015)
* Introduce `SuppressInstrumentationScope` API (#988).
* Support wildcard pattern matching for ActivitySource
([#1023](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1023)).
* Renaming `BroadcastActivityProcessor` to `FanOutActivityProcessor`
([#1015](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1015)).
* Introduce `SuppressInstrumentationScope` API
([#988](https://github.com/open-telemetry/opentelemetry-dotnet/pull/988)).
* `ActivityProcessor` implements `IDisposable`.
* When `Dispose` occurs, it calls `ShutdownAsync`.
* If you want a custom behavior for dispose, you will have to override the
Expand Down
57 changes: 49 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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cijothomas Do we still need to listen to the blank source for the legacy Activity objects? Or did the ActivitySourceAdapter take care of that?

{
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,32 @@ 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);

// Function which takes ActivitySource and returns true/false to indicate if it should be subscribed to
// or not.
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