diff --git a/CHANGELOG.md b/CHANGELOG.md index eb18126764..5bcd7a6601 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ ### Features -- Add tag filters to SentryLoggingOptions ([#2360](https://github.com/getsentry/sentry-dotnet/pull/2360)) +- Add tag filters to `SentryOptions` ([#2367](https://github.com/getsentry/sentry-dotnet/pull/2367)) ### Dependencies diff --git a/src/Sentry.Extensions.Logging/SentryLogger.cs b/src/Sentry.Extensions.Logging/SentryLogger.cs index 3d53caa037..284a5c371b 100644 --- a/src/Sentry.Extensions.Logging/SentryLogger.cs +++ b/src/Sentry.Extensions.Logging/SentryLogger.cs @@ -47,7 +47,7 @@ public bool IsEnabled(LogLevel logLevel) if (ShouldCaptureEvent(logLevel, eventId, exception)) { - var @event = CreateEvent(logLevel, eventId, state, exception, message, CategoryName, _options.TagFilters); + var @event = CreateEvent(logLevel, eventId, state, exception, message, CategoryName); _ = _hub.CaptureEvent(@event); } @@ -81,8 +81,7 @@ public bool IsEnabled(LogLevel logLevel) TState state, Exception? exception, string? message, - string category, - ICollection tagFilters) + string category) { var @event = new SentryEvent(exception) { @@ -106,11 +105,6 @@ public bool IsEnabled(LogLevel logLevel) continue; } - if (tagFilters.Any(x => x.IsMatch(property.Key))) - { - continue; - } - switch (property.Value) { case string stringTagValue: diff --git a/src/Sentry.Extensions.Logging/SentryLoggingOptions.cs b/src/Sentry.Extensions.Logging/SentryLoggingOptions.cs index 98bcc64162..52bd4a0260 100644 --- a/src/Sentry.Extensions.Logging/SentryLoggingOptions.cs +++ b/src/Sentry.Extensions.Logging/SentryLoggingOptions.cs @@ -48,9 +48,4 @@ public class SentryLoggingOptions : SentryOptions /// List of callbacks to be invoked when initializing the SDK /// internal Action[] ConfigureScopeCallbacks { get; set; } = Array.Empty>(); - - /// - /// List of substrings or regular expression patterns to filter out tags - /// - public ICollection TagFilters { get; set; } = new List(); } diff --git a/src/Sentry/Scope.cs b/src/Sentry/Scope.cs index bd4db02382..f8c053378c 100644 --- a/src/Sentry/Scope.cs +++ b/src/Sentry/Scope.cs @@ -290,6 +290,11 @@ public void SetExtra(string key, object? value) /// public void SetTag(string key, string value) { + if (Options.TagFilters.Any(x => x.IsMatch(key))) + { + return; + } + _tags[key] = value; if (Options.EnableScopeSync) { diff --git a/src/Sentry/SentryOptions.cs b/src/Sentry/SentryOptions.cs index e27763249e..ce3cdfe27d 100644 --- a/src/Sentry/SentryOptions.cs +++ b/src/Sentry/SentryOptions.cs @@ -131,6 +131,11 @@ public bool IsGlobalModeEnabled internal List? ExceptionFilters { get; set; } = new(); + /// + /// List of substrings or regular expression patterns to filter out tags + /// + public ICollection TagFilters { get; set; } = new List(); + /// /// The worker used by the client to pass envelopes. /// diff --git a/test/Sentry.Extensions.Logging.Tests/ApiApprovalTests.Run.Core3_1.verified.txt b/test/Sentry.Extensions.Logging.Tests/ApiApprovalTests.Run.Core3_1.verified.txt index 1b6cdb9272..b438b0af45 100644 --- a/test/Sentry.Extensions.Logging.Tests/ApiApprovalTests.Run.Core3_1.verified.txt +++ b/test/Sentry.Extensions.Logging.Tests/ApiApprovalTests.Run.Core3_1.verified.txt @@ -41,7 +41,6 @@ namespace Sentry.Extensions.Logging public bool InitializeSdk { get; set; } public Microsoft.Extensions.Logging.LogLevel MinimumBreadcrumbLevel { get; set; } public Microsoft.Extensions.Logging.LogLevel MinimumEventLevel { get; set; } - public System.Collections.Generic.ICollection TagFilters { get; set; } public void ConfigureScope(System.Action action) { } } public static class SentryLoggingOptionsExtensions diff --git a/test/Sentry.Extensions.Logging.Tests/ApiApprovalTests.Run.DotNet6_0.verified.txt b/test/Sentry.Extensions.Logging.Tests/ApiApprovalTests.Run.DotNet6_0.verified.txt index 1b6cdb9272..b438b0af45 100644 --- a/test/Sentry.Extensions.Logging.Tests/ApiApprovalTests.Run.DotNet6_0.verified.txt +++ b/test/Sentry.Extensions.Logging.Tests/ApiApprovalTests.Run.DotNet6_0.verified.txt @@ -41,7 +41,6 @@ namespace Sentry.Extensions.Logging public bool InitializeSdk { get; set; } public Microsoft.Extensions.Logging.LogLevel MinimumBreadcrumbLevel { get; set; } public Microsoft.Extensions.Logging.LogLevel MinimumEventLevel { get; set; } - public System.Collections.Generic.ICollection TagFilters { get; set; } public void ConfigureScope(System.Action action) { } } public static class SentryLoggingOptionsExtensions diff --git a/test/Sentry.Extensions.Logging.Tests/ApiApprovalTests.Run.DotNet7_0.verified.txt b/test/Sentry.Extensions.Logging.Tests/ApiApprovalTests.Run.DotNet7_0.verified.txt index 1b6cdb9272..b438b0af45 100644 --- a/test/Sentry.Extensions.Logging.Tests/ApiApprovalTests.Run.DotNet7_0.verified.txt +++ b/test/Sentry.Extensions.Logging.Tests/ApiApprovalTests.Run.DotNet7_0.verified.txt @@ -41,7 +41,6 @@ namespace Sentry.Extensions.Logging public bool InitializeSdk { get; set; } public Microsoft.Extensions.Logging.LogLevel MinimumBreadcrumbLevel { get; set; } public Microsoft.Extensions.Logging.LogLevel MinimumEventLevel { get; set; } - public System.Collections.Generic.ICollection TagFilters { get; set; } public void ConfigureScope(System.Action action) { } } public static class SentryLoggingOptionsExtensions diff --git a/test/Sentry.Extensions.Logging.Tests/ApiApprovalTests.Run.Net4_8.verified.txt b/test/Sentry.Extensions.Logging.Tests/ApiApprovalTests.Run.Net4_8.verified.txt index 1b6cdb9272..b438b0af45 100644 --- a/test/Sentry.Extensions.Logging.Tests/ApiApprovalTests.Run.Net4_8.verified.txt +++ b/test/Sentry.Extensions.Logging.Tests/ApiApprovalTests.Run.Net4_8.verified.txt @@ -41,7 +41,6 @@ namespace Sentry.Extensions.Logging public bool InitializeSdk { get; set; } public Microsoft.Extensions.Logging.LogLevel MinimumBreadcrumbLevel { get; set; } public Microsoft.Extensions.Logging.LogLevel MinimumEventLevel { get; set; } - public System.Collections.Generic.ICollection TagFilters { get; set; } public void ConfigureScope(System.Action action) { } } public static class SentryLoggingOptionsExtensions diff --git a/test/Sentry.Extensions.Logging.Tests/SentryLoggerTests.cs b/test/Sentry.Extensions.Logging.Tests/SentryLoggerTests.cs index 7d7c5ef4c2..7ada979126 100644 --- a/test/Sentry.Extensions.Logging.Tests/SentryLoggerTests.cs +++ b/test/Sentry.Extensions.Logging.Tests/SentryLoggerTests.cs @@ -112,7 +112,7 @@ public void Culture_does_not_effect_tags() try { Thread.CurrentThread.CurrentCulture = new("da-DK"); - sentryEvent = SentryLogger.CreateEvent(LogLevel.Debug, default, props, null, null, "category", Array.Empty()); + sentryEvent = SentryLogger.CreateEvent(LogLevel.Debug, default, props, null, null, "category"); } finally { @@ -531,21 +531,4 @@ public void BeginScope_Disposable_Scope() Assert.Same(actual, expected); } - - [Fact] - public void Filtered_tags_are_not_on_event() - { - var props = new List> - { - new("AzFunctions", "rule"), - new("AzureFunctions_FunctionName", "Func"), - new("AzureFunctions_InvocationId", "20a09c3b-e9dd-43fe-9a73-ebae1f90cab6"), - }; - - var tagFilters = new[] { new SubstringOrRegexPattern("AzureFunctions_") }; - - var sentryEvent = SentryLogger.CreateEvent(LogLevel.Debug, default, props, null, null, "category", tagFilters); - - sentryEvent.Tags.Should().OnlyContain(pair => pair.Key == "AzFunctions" && pair.Value == "rule"); - } } diff --git a/test/Sentry.Tests/ApiApprovalTests.Run.Core3_1.verified.txt b/test/Sentry.Tests/ApiApprovalTests.Run.Core3_1.verified.txt index bc4762d774..5c636a570f 100644 --- a/test/Sentry.Tests/ApiApprovalTests.Run.Core3_1.verified.txt +++ b/test/Sentry.Tests/ApiApprovalTests.Run.Core3_1.verified.txt @@ -613,6 +613,7 @@ namespace Sentry public string? ServerName { get; set; } public System.TimeSpan ShutdownTimeout { get; set; } public Sentry.StackTraceMode StackTraceMode { get; set; } + public System.Collections.Generic.ICollection TagFilters { get; set; } public System.Collections.Generic.IList TracePropagationTargets { get; set; } public double TracesSampleRate { get; set; } public System.Func? TracesSampler { get; set; } diff --git a/test/Sentry.Tests/ApiApprovalTests.Run.DotNet6_0.verified.txt b/test/Sentry.Tests/ApiApprovalTests.Run.DotNet6_0.verified.txt index 9816e4fab2..24eb68fe28 100644 --- a/test/Sentry.Tests/ApiApprovalTests.Run.DotNet6_0.verified.txt +++ b/test/Sentry.Tests/ApiApprovalTests.Run.DotNet6_0.verified.txt @@ -614,6 +614,7 @@ namespace Sentry public string? ServerName { get; set; } public System.TimeSpan ShutdownTimeout { get; set; } public Sentry.StackTraceMode StackTraceMode { get; set; } + public System.Collections.Generic.ICollection TagFilters { get; set; } public System.Collections.Generic.IList TracePropagationTargets { get; set; } public double TracesSampleRate { get; set; } public System.Func? TracesSampler { get; set; } diff --git a/test/Sentry.Tests/ApiApprovalTests.Run.DotNet7_0.verified.txt b/test/Sentry.Tests/ApiApprovalTests.Run.DotNet7_0.verified.txt index 9816e4fab2..24eb68fe28 100644 --- a/test/Sentry.Tests/ApiApprovalTests.Run.DotNet7_0.verified.txt +++ b/test/Sentry.Tests/ApiApprovalTests.Run.DotNet7_0.verified.txt @@ -614,6 +614,7 @@ namespace Sentry public string? ServerName { get; set; } public System.TimeSpan ShutdownTimeout { get; set; } public Sentry.StackTraceMode StackTraceMode { get; set; } + public System.Collections.Generic.ICollection TagFilters { get; set; } public System.Collections.Generic.IList TracePropagationTargets { get; set; } public double TracesSampleRate { get; set; } public System.Func? TracesSampler { get; set; } diff --git a/test/Sentry.Tests/ApiApprovalTests.Run.Net4_8.verified.txt b/test/Sentry.Tests/ApiApprovalTests.Run.Net4_8.verified.txt index fa1b223ac0..1ca12cb25c 100644 --- a/test/Sentry.Tests/ApiApprovalTests.Run.Net4_8.verified.txt +++ b/test/Sentry.Tests/ApiApprovalTests.Run.Net4_8.verified.txt @@ -612,6 +612,7 @@ namespace Sentry public string? ServerName { get; set; } public System.TimeSpan ShutdownTimeout { get; set; } public Sentry.StackTraceMode StackTraceMode { get; set; } + public System.Collections.Generic.ICollection TagFilters { get; set; } public System.Collections.Generic.IList TracePropagationTargets { get; set; } public double TracesSampleRate { get; set; } public System.Func? TracesSampler { get; set; } diff --git a/test/Sentry.Tests/ScopeTests.cs b/test/Sentry.Tests/ScopeTests.cs index d7de09a391..0d10fed586 100644 --- a/test/Sentry.Tests/ScopeTests.cs +++ b/test/Sentry.Tests/ScopeTests.cs @@ -462,6 +462,29 @@ public void AddBreadcrumb_ObserverExist_ObserverAddsBreadcrumbIfEnabled(bool obs // Assert observer.Received(expectedCount).AddBreadcrumb(Arg.Is(breadcrumb)); } + + [Fact] + public void Filtered_tags_are_not_set() + { + var tags = new List> + { + new("AzFunctions", "rule"), + new("AzureFunctions_FunctionName", "Func"), + new("AzureFunctions_InvocationId", "20a09c3b-e9dd-43fe-9a73-ebae1f90cab6"), + }; + + var scope = new Scope(new SentryOptions + { + TagFilters = new[] { new SubstringOrRegexPattern("AzureFunctions_") } + }); + + foreach (var (key, value) in tags) + { + scope.SetTag(key, value); + } + + scope.Tags.Should().OnlyContain(pair => pair.Key == "AzFunctions" && pair.Value == "rule"); + } } public static class ScopeTestExtensions