From 35bb0e61ab3cc5e9cfc809a9a83c421780549e55 Mon Sep 17 00:00:00 2001 From: Sean Feldman Date: Fri, 12 May 2023 01:46:34 -0600 Subject: [PATCH 1/2] Move tags filtering from SentryLogger to Scope --- src/Sentry.Extensions.Logging/SentryLogger.cs | 10 ++------ .../SentryLoggingOptions.cs | 5 ---- src/Sentry/Scope.cs | 5 ++++ src/Sentry/SentryOptions.cs | 5 ++++ .../ApiApprovalTests.Run.Core3_1.verified.txt | 1 - ...piApprovalTests.Run.DotNet6_0.verified.txt | 1 - ...piApprovalTests.Run.DotNet7_0.verified.txt | 1 - .../ApiApprovalTests.Run.Net4_8.verified.txt | 1 - .../SentryLoggerTests.cs | 19 +-------------- .../ApiApprovalTests.Run.Core3_1.verified.txt | 1 + ...piApprovalTests.Run.DotNet6_0.verified.txt | 1 + ...piApprovalTests.Run.DotNet7_0.verified.txt | 1 + .../ApiApprovalTests.Run.Net4_8.verified.txt | 1 + test/Sentry.Tests/ScopeTests.cs | 23 +++++++++++++++++++ 14 files changed, 40 insertions(+), 35 deletions(-) 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 void Log( 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 @@ internal static SentryEvent CreateEvent( TState state, Exception? exception, string? message, - string category, - ICollection tagFilters) + string category) { var @event = new SentryEvent(exception) { @@ -106,11 +105,6 @@ internal static SentryEvent CreateEvent( 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 From 06ce931d25d2c660547245e62af2ddb2de7674be Mon Sep 17 00:00:00 2001 From: Sean Feldman Date: Fri, 12 May 2023 01:51:17 -0600 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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