Skip to content

Commit

Permalink
Add DisableActivityHandler for disabling ActivitySources
Browse files Browse the repository at this point in the history
This allows for end users to completely disable
certain ActivitySource names by using the new
configuration key DisabledOpenTelemetryIntegrations
and DD_DISABLED_OPENTELEMETRY_INTEGRATIONS
which accepts a semi-colon separated list of
ActivitySource names to disable.

Note that the names must be an exact match.
  • Loading branch information
bouwkast committed Jul 19, 2024
1 parent 578567f commit 87fbe4e
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 6 deletions.
32 changes: 26 additions & 6 deletions tracer/src/Datadog.Trace/Activity/ActivityListenerHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,34 @@ public static bool OnShouldListenTo<T>(T source)
{
if (source is IActivitySource activitySource)
{
if (handler.ShouldListenTo(sName, activitySource.Version) && HandlerBySource.TryAdd(sName, handler))
if (handler.ShouldListenTo(sName, activitySource.Version))
{
Log.Debug("ActivityListenerHandler: {SourceName} will be handled by {Handler}.", sName, handler);
return true;
if (handler is DisableActivityHandler)
{
Log.Information("ActivityListenerHandler: {SourceName} will be disabled for the .NET Tracer.", sName);
return false;
}

if (HandlerBySource.TryAdd(sName, handler))
{
Log.Debug("ActivityListenerHandler: {SourceName} will be handled by {Handler}.", sName, handler);
return true;
}
}
}
else if (handler.ShouldListenTo(sName, null) && HandlerBySource.TryAdd(sName, handler))
else if (handler.ShouldListenTo(sName, null))
{
Log.Debug("ActivityListenerHandler: {SourceName} will be handled by {Handler}.", sName, handler);
return true;
if (handler is DisableActivityHandler)
{
Log.Information("ActivityListenerHandler: {SourceName} will be disabled for the .NET Tracer.", sName);
return false;
}

if (HandlerBySource.TryAdd(sName, handler))
{
Log.Debug("ActivityListenerHandler: {SourceName} will be handled by {Handler}.", sName, handler);
return true;
}
}
}

Expand All @@ -71,6 +89,7 @@ public static void OnActivityWithSourceStarted<T>(string sourceName, T activity)
var sName = sourceName ?? "(null)";
if (HandlerBySource.TryGetValue(sName, out var handler))
{
Log.Debug("ActivityListenerHandler: ActivityStarted event handled by. [Source={SourceName}]", sName);
handler.ActivityStarted(sName, activity);
}
else
Expand All @@ -86,6 +105,7 @@ public static void OnActivityWithSourceStopped<T>(string sourceName, T activity)
if (HandlerBySource.TryGetValue(sName, out var handler))
{
handler.ActivityStopped(sName, activity);
Log.Debug("ActivityListenerHandler: Stopped event handled by. [Source={SourceName}]", sName);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ namespace Datadog.Trace.Activity.Handlers
{
internal static class ActivityHandlersRegister
{
// Disable Activity Handler does not listen to the activity source at all.
internal static readonly DisableActivityHandler DisableHandler = new();

// Ignore Activity Handler catches existing integrations that also emits activities.
internal static readonly IgnoreActivityHandler IgnoreHandler = new();

// Activity handlers in order, the first handler where ShouldListenTo returns true will always handle that source.
internal static readonly IActivityHandler[] Handlers =
{
DisableHandler,

IgnoreHandler,

// Azure Service Bus handlers
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// <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;
using Datadog.Trace.Activity.DuckTypes;

namespace Datadog.Trace.Activity.Handlers
{
internal class DisableActivityHandler : IActivityHandler
{
public void ActivityStarted<T>(string sourceName, T activity)
where T : IActivity
{
// do nothing
throw new InvalidOperationException("Should not happen");
}

public void ActivityStopped<T>(string sourceName, T activity)
where T : IActivity
{
// do nothing
throw new InvalidOperationException("Should not happen");
}

public bool ShouldListenTo(string sourceName, string? version)
{
var toDisable = Tracer.Instance.Settings.DisabledOpenTelemetryIntegrations;
if (toDisable is null || string.IsNullOrWhiteSpace(toDisable))
{
return false;
}

var toDisableSplit = toDisable.Split(';');
foreach (var disabledSourceName in toDisableSplit)
{
if (sourceName == disabledSourceName)
{
return true;
}
}

return false;
}
}
}
7 changes: 7 additions & 0 deletions tracer/src/Datadog.Trace/Configuration/ConfigurationKeys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ internal static partial class ConfigurationKeys
/// <seealso cref="TracerSettings.DisabledIntegrationNames"/>
public const string DisabledIntegrations = "DD_DISABLED_INTEGRATIONS";

/// <summary>
/// Configuration key for a list of OpenTelemetry integrations where their ActivitySource's will not be listened to.
/// Default is empty (all OpenTelemetry integrations will be listened to).
/// Supports multiple values separated with semi-colons.
/// </summary>
public const string DisabledOpenTelemetryIntegrations = "DD_DISABLED_OPENTELEMETRY_INTEGRATIONS";

/// <summary>
/// Configuration key for enabling or disabling default Analytics.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ internal ImmutableTracerSettings(TracerSettings settings, bool unusedParamNotToU
SpanSamplingRules = settings.SpanSamplingRules;
_globalSamplingRate = settings.GlobalSamplingRateInternal;
IntegrationsInternal = new ImmutableIntegrationSettingsCollection(settings.IntegrationsInternal, settings.DisabledIntegrationNamesInternal);
DisabledOpenTelemetryIntegrations = settings.DisabledOpenTelemetryIntegrationNamesInternal;
_headerTags = new ReadOnlyDictionary<string, string>(settings.HeaderTagsInternal);
HeaderTagsNormalizationFixEnabled = settings.HeaderTagsNormalizationFixEnabled;
GrpcTagsInternal = new ReadOnlyDictionary<string, string>(settings.GrpcTagsInternal);
Expand Down Expand Up @@ -337,6 +338,12 @@ internal ImmutableTracerSettings(TracerSettings settings, bool unusedParamNotToU
[GeneratePublicApi(PublicApiUsage.ImmutableTracerSettings_Integrations_Get)]
internal ImmutableIntegrationSettingsCollection IntegrationsInternal { get; }

/// <summary>
/// Gets the semi-colon separated string of disabled Activity Source names that will not be handled at all.
/// </summary>
/// <seealso cref="ConfigurationKeys.DisabledOpenTelemetryIntegrations"/>
internal string? DisabledOpenTelemetryIntegrations { get; }

/// <summary>
/// Gets the global tags, which are applied to all <see cref="Span"/>s.
/// </summary>
Expand Down
14 changes: 14 additions & 0 deletions tracer/src/Datadog.Trace/Configuration/TracerSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,13 @@ _ when x.ToBoolean() is { } boolean => boolean,

DisabledIntegrationNamesInternal = new HashSet<string>(disabledIntegrationNames, StringComparer.OrdinalIgnoreCase);

var disabledOpenTelemetryIntegrationNames = config.WithKeys(ConfigurationKeys.DisabledOpenTelemetryIntegrations)
.AsString()
?.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries) ??
Enumerable.Empty<string>();

DisabledOpenTelemetryIntegrationNamesInternal = config.WithKeys(ConfigurationKeys.DisabledOpenTelemetryIntegrations).AsString();

IntegrationsInternal = new IntegrationSettingsCollection(source, unusedParamNotToUsePublicApi: false);

ExporterInternal = new ExporterSettings(source, _telemetry);
Expand Down Expand Up @@ -593,6 +600,13 @@ _ when x.ToBoolean() is { } boolean => boolean,
[ConfigKey(ConfigurationKeys.DisabledIntegrations)]
internal HashSet<string> DisabledIntegrationNamesInternal { get; private set; }

/// <summary>
/// Gets or sets the names of disabled OpenTelemetry integrations.
/// </summary>
/// <seealso cref="ConfigurationKeys.DisabledOpenTelemetryIntegrations"/>
[ConfigKey(ConfigurationKeys.DisabledOpenTelemetryIntegrations)]
internal string? DisabledOpenTelemetryIntegrationNamesInternal { get; private set; }

/// <summary>
/// Gets or sets the transport settings that dictate how the tracer connects to the agent.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ internal TracerSettingsSnapshot(Datadog.Trace.Configuration.TracerSettings setti
ServiceVersionInternal = settings.ServiceVersionInternal;
TraceEnabledInternal = settings.TraceEnabledInternal;
DisabledIntegrationNamesInternal = GetHashSet(settings.DisabledIntegrationNamesInternal);
DisabledOpenTelemetryIntegrationNamesInternal = settings.DisabledOpenTelemetryIntegrationNamesInternal;
AnalyticsEnabledInternal = settings.AnalyticsEnabledInternal;
MaxTracesSubmittedPerSecondInternal = settings.MaxTracesSubmittedPerSecondInternal;
CustomSamplingRulesInternal = settings.CustomSamplingRulesInternal;
Expand All @@ -35,6 +36,7 @@ internal TracerSettingsSnapshot(Datadog.Trace.Configuration.TracerSettings setti
private string? ServiceVersionInternal { get; }
private bool TraceEnabledInternal { get; }
private System.Collections.Generic.HashSet<string>? DisabledIntegrationNamesInternal { get; }
private string? DisabledOpenTelemetryIntegrationNamesInternal { get; }
private bool AnalyticsEnabledInternal { get; }
private int MaxTracesSubmittedPerSecondInternal { get; }
private string? CustomSamplingRulesInternal { get; }
Expand All @@ -54,6 +56,7 @@ internal void RecordChanges(Datadog.Trace.Configuration.TracerSettings settings,
RecordIfChanged(telemetry, "DD_VERSION", ServiceVersionInternal, settings.ServiceVersionInternal);
RecordIfChanged(telemetry, "DD_TRACE_ENABLED", TraceEnabledInternal, settings.TraceEnabledInternal);
RecordIfChanged(telemetry, "DD_DISABLED_INTEGRATIONS", DisabledIntegrationNamesInternal, GetHashSet(settings.DisabledIntegrationNamesInternal));
RecordIfChanged(telemetry, "DD_DISABLED_OPENTELEMETRY_INTEGRATIONS", DisabledOpenTelemetryIntegrationNamesInternal, settings.DisabledOpenTelemetryIntegrationNamesInternal);
RecordIfChanged(telemetry, "DD_TRACE_ANALYTICS_ENABLED", AnalyticsEnabledInternal, settings.AnalyticsEnabledInternal);
RecordIfChanged(telemetry, "DD_TRACE_RATE_LIMIT", MaxTracesSubmittedPerSecondInternal, settings.MaxTracesSubmittedPerSecondInternal);
RecordIfChanged(telemetry, "DD_TRACE_SAMPLING_RULES", CustomSamplingRulesInternal, settings.CustomSamplingRulesInternal);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ internal TracerSettingsSnapshot(Datadog.Trace.Configuration.TracerSettings setti
ServiceVersionInternal = settings.ServiceVersionInternal;
TraceEnabledInternal = settings.TraceEnabledInternal;
DisabledIntegrationNamesInternal = GetHashSet(settings.DisabledIntegrationNamesInternal);
DisabledOpenTelemetryIntegrationNamesInternal = settings.DisabledOpenTelemetryIntegrationNamesInternal;
AnalyticsEnabledInternal = settings.AnalyticsEnabledInternal;
MaxTracesSubmittedPerSecondInternal = settings.MaxTracesSubmittedPerSecondInternal;
CustomSamplingRulesInternal = settings.CustomSamplingRulesInternal;
Expand All @@ -35,6 +36,7 @@ internal TracerSettingsSnapshot(Datadog.Trace.Configuration.TracerSettings setti
private string? ServiceVersionInternal { get; }
private bool TraceEnabledInternal { get; }
private System.Collections.Generic.HashSet<string>? DisabledIntegrationNamesInternal { get; }
private string? DisabledOpenTelemetryIntegrationNamesInternal { get; }
private bool AnalyticsEnabledInternal { get; }
private int MaxTracesSubmittedPerSecondInternal { get; }
private string? CustomSamplingRulesInternal { get; }
Expand All @@ -54,6 +56,7 @@ internal void RecordChanges(Datadog.Trace.Configuration.TracerSettings settings,
RecordIfChanged(telemetry, "DD_VERSION", ServiceVersionInternal, settings.ServiceVersionInternal);
RecordIfChanged(telemetry, "DD_TRACE_ENABLED", TraceEnabledInternal, settings.TraceEnabledInternal);
RecordIfChanged(telemetry, "DD_DISABLED_INTEGRATIONS", DisabledIntegrationNamesInternal, GetHashSet(settings.DisabledIntegrationNamesInternal));
RecordIfChanged(telemetry, "DD_DISABLED_OPENTELEMETRY_INTEGRATIONS", DisabledOpenTelemetryIntegrationNamesInternal, settings.DisabledOpenTelemetryIntegrationNamesInternal);
RecordIfChanged(telemetry, "DD_TRACE_ANALYTICS_ENABLED", AnalyticsEnabledInternal, settings.AnalyticsEnabledInternal);
RecordIfChanged(telemetry, "DD_TRACE_RATE_LIMIT", MaxTracesSubmittedPerSecondInternal, settings.MaxTracesSubmittedPerSecondInternal);
RecordIfChanged(telemetry, "DD_TRACE_SAMPLING_RULES", CustomSamplingRulesInternal, settings.CustomSamplingRulesInternal);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ internal TracerSettingsSnapshot(Datadog.Trace.Configuration.TracerSettings setti
ServiceVersionInternal = settings.ServiceVersionInternal;
TraceEnabledInternal = settings.TraceEnabledInternal;
DisabledIntegrationNamesInternal = GetHashSet(settings.DisabledIntegrationNamesInternal);
DisabledOpenTelemetryIntegrationNamesInternal = settings.DisabledOpenTelemetryIntegrationNamesInternal;
AnalyticsEnabledInternal = settings.AnalyticsEnabledInternal;
MaxTracesSubmittedPerSecondInternal = settings.MaxTracesSubmittedPerSecondInternal;
CustomSamplingRulesInternal = settings.CustomSamplingRulesInternal;
Expand All @@ -35,6 +36,7 @@ internal TracerSettingsSnapshot(Datadog.Trace.Configuration.TracerSettings setti
private string? ServiceVersionInternal { get; }
private bool TraceEnabledInternal { get; }
private System.Collections.Generic.HashSet<string>? DisabledIntegrationNamesInternal { get; }
private string? DisabledOpenTelemetryIntegrationNamesInternal { get; }
private bool AnalyticsEnabledInternal { get; }
private int MaxTracesSubmittedPerSecondInternal { get; }
private string? CustomSamplingRulesInternal { get; }
Expand All @@ -54,6 +56,7 @@ internal void RecordChanges(Datadog.Trace.Configuration.TracerSettings settings,
RecordIfChanged(telemetry, "DD_VERSION", ServiceVersionInternal, settings.ServiceVersionInternal);
RecordIfChanged(telemetry, "DD_TRACE_ENABLED", TraceEnabledInternal, settings.TraceEnabledInternal);
RecordIfChanged(telemetry, "DD_DISABLED_INTEGRATIONS", DisabledIntegrationNamesInternal, GetHashSet(settings.DisabledIntegrationNamesInternal));
RecordIfChanged(telemetry, "DD_DISABLED_OPENTELEMETRY_INTEGRATIONS", DisabledOpenTelemetryIntegrationNamesInternal, settings.DisabledOpenTelemetryIntegrationNamesInternal);
RecordIfChanged(telemetry, "DD_TRACE_ANALYTICS_ENABLED", AnalyticsEnabledInternal, settings.AnalyticsEnabledInternal);
RecordIfChanged(telemetry, "DD_TRACE_RATE_LIMIT", MaxTracesSubmittedPerSecondInternal, settings.MaxTracesSubmittedPerSecondInternal);
RecordIfChanged(telemetry, "DD_TRACE_SAMPLING_RULES", CustomSamplingRulesInternal, settings.CustomSamplingRulesInternal);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ internal TracerSettingsSnapshot(Datadog.Trace.Configuration.TracerSettings setti
ServiceVersionInternal = settings.ServiceVersionInternal;
TraceEnabledInternal = settings.TraceEnabledInternal;
DisabledIntegrationNamesInternal = GetHashSet(settings.DisabledIntegrationNamesInternal);
DisabledOpenTelemetryIntegrationNamesInternal = settings.DisabledOpenTelemetryIntegrationNamesInternal;
AnalyticsEnabledInternal = settings.AnalyticsEnabledInternal;
MaxTracesSubmittedPerSecondInternal = settings.MaxTracesSubmittedPerSecondInternal;
CustomSamplingRulesInternal = settings.CustomSamplingRulesInternal;
Expand All @@ -35,6 +36,7 @@ internal TracerSettingsSnapshot(Datadog.Trace.Configuration.TracerSettings setti
private string? ServiceVersionInternal { get; }
private bool TraceEnabledInternal { get; }
private System.Collections.Generic.HashSet<string>? DisabledIntegrationNamesInternal { get; }
private string? DisabledOpenTelemetryIntegrationNamesInternal { get; }
private bool AnalyticsEnabledInternal { get; }
private int MaxTracesSubmittedPerSecondInternal { get; }
private string? CustomSamplingRulesInternal { get; }
Expand All @@ -54,6 +56,7 @@ internal void RecordChanges(Datadog.Trace.Configuration.TracerSettings settings,
RecordIfChanged(telemetry, "DD_VERSION", ServiceVersionInternal, settings.ServiceVersionInternal);
RecordIfChanged(telemetry, "DD_TRACE_ENABLED", TraceEnabledInternal, settings.TraceEnabledInternal);
RecordIfChanged(telemetry, "DD_DISABLED_INTEGRATIONS", DisabledIntegrationNamesInternal, GetHashSet(settings.DisabledIntegrationNamesInternal));
RecordIfChanged(telemetry, "DD_DISABLED_OPENTELEMETRY_INTEGRATIONS", DisabledOpenTelemetryIntegrationNamesInternal, settings.DisabledOpenTelemetryIntegrationNamesInternal);
RecordIfChanged(telemetry, "DD_TRACE_ANALYTICS_ENABLED", AnalyticsEnabledInternal, settings.AnalyticsEnabledInternal);
RecordIfChanged(telemetry, "DD_TRACE_RATE_LIMIT", MaxTracesSubmittedPerSecondInternal, settings.MaxTracesSubmittedPerSecondInternal);
RecordIfChanged(telemetry, "DD_TRACE_SAMPLING_RULES", CustomSamplingRulesInternal, settings.CustomSamplingRulesInternal);
Expand Down

0 comments on commit 87fbe4e

Please sign in to comment.