-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to disable ActivitySources
This allows for users to pass in a comma separated list of Activity Source names (glob-pattern supported) that should not be listened to by the Datadog Tracer.
- Loading branch information
Showing
9 changed files
with
179 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
tracer/src/Datadog.Trace/Activity/Handlers/DisableActivityHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// <copyright file="DisableActivityHandler.cs" company="Datadog"> | ||
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc. | ||
// </copyright> | ||
|
||
#nullable enable | ||
|
||
using System.Collections.Generic; | ||
using System.Text.RegularExpressions; | ||
using Datadog.Trace.Activity.DuckTypes; | ||
using Datadog.Trace.Sampling; | ||
|
||
namespace Datadog.Trace.Activity.Handlers | ||
{ | ||
internal class DisableActivityHandler : IActivityHandler | ||
{ | ||
private List<Regex>? _disabledSourceNameGlobs = null; | ||
private bool _disableAll = false; | ||
|
||
public void ActivityStarted<T>(string sourceName, T activity) | ||
where T : IActivity | ||
{ | ||
// do nothing; this should not be called | ||
} | ||
|
||
public void ActivityStopped<T>(string sourceName, T activity) | ||
where T : IActivity | ||
{ | ||
// do nothing; this should not be called | ||
} | ||
|
||
/// <summary> | ||
/// Determines whether <see cref="DisableActivityHandler"/> will "listen" to <paramref name="sourceName"/>. | ||
/// <para> | ||
/// Note that "listen" in this case means that the created ActivityListener will not subscribe to the ActivitySource. | ||
/// </para> | ||
/// </summary> | ||
/// <returns><see langword="true"/> when the Tracer will disable the ActivitySource; otherwise <see langword="false"/></returns> | ||
public bool ShouldListenTo(string sourceName, string? version) | ||
{ | ||
if (_disableAll) | ||
{ | ||
return true; // "*" was specified as a pattern, short circuit to disable all | ||
} | ||
|
||
_disabledSourceNameGlobs ??= PopulateGlobs(); | ||
if (_disabledSourceNameGlobs.Count == 0) | ||
{ | ||
return false; // no glob patterns specified, sourceName will not be disabled | ||
} | ||
|
||
foreach (var regex in _disabledSourceNameGlobs) | ||
{ | ||
if (regex.IsMatch(sourceName)) | ||
{ | ||
return true; // disable ActivitySource of "sourceName" from being listened to by the tracer | ||
} | ||
} | ||
|
||
var toDisable = Tracer.Instance.Settings.DisabledActivitySources; | ||
if (toDisable is null || toDisable.Length == 0) | ||
{ | ||
return false; | ||
} | ||
|
||
// sources were specified to be disabled, but this sourceName didn't match any of them | ||
return false; // sourceName will _not_ be disabled | ||
} | ||
|
||
private List<Regex> PopulateGlobs() | ||
{ | ||
var globs = new List<Regex>(); | ||
var toDisable = Tracer.Instance.Settings.DisabledActivitySources; | ||
if (toDisable is null || toDisable.Length == 0) | ||
{ | ||
return globs; | ||
} | ||
|
||
foreach (var disabledSourceNameGlob in toDisable) | ||
{ | ||
// HACK: using RegexBuilder here even though it isn't _really_ for this | ||
var globRegex = RegexBuilder.Build(disabledSourceNameGlob, SamplingRulesFormat.Glob, RegexBuilder.DefaultTimeout); | ||
// handle special case where a "*" pattern will be null | ||
if (globRegex is null) | ||
{ | ||
_disableAll = true; | ||
return []; | ||
} | ||
|
||
globs.Add(globRegex); | ||
} | ||
|
||
return globs; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters