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 tag filters to SentryOptions #2367

Merged
merged 2 commits into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 2 additions & 8 deletions src/Sentry.Extensions.Logging/SentryLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -81,8 +81,7 @@ public bool IsEnabled(LogLevel logLevel)
TState state,
Exception? exception,
string? message,
string category,
ICollection<SubstringOrRegexPattern> tagFilters)
string category)
{
var @event = new SentryEvent(exception)
{
Expand All @@ -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:
Expand Down
5 changes: 0 additions & 5 deletions src/Sentry.Extensions.Logging/SentryLoggingOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,4 @@ public class SentryLoggingOptions : SentryOptions
/// List of callbacks to be invoked when initializing the SDK
/// </summary>
internal Action<Scope>[] ConfigureScopeCallbacks { get; set; } = Array.Empty<Action<Scope>>();

/// <summary>
/// List of substrings or regular expression patterns to filter out tags
/// </summary>
public ICollection<SubstringOrRegexPattern> TagFilters { get; set; } = new List<SubstringOrRegexPattern>();
}
5 changes: 5 additions & 0 deletions src/Sentry/Scope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,11 @@ public void SetExtra(string key, object? value)
/// <inheritdoc />
public void SetTag(string key, string value)
{
if (Options.TagFilters.Any(x => x.IsMatch(key)))
{
return;
}

_tags[key] = value;
if (Options.EnableScopeSync)
{
Expand Down
5 changes: 5 additions & 0 deletions src/Sentry/SentryOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ public bool IsGlobalModeEnabled

internal List<IExceptionFilter>? ExceptionFilters { get; set; } = new();

/// <summary>
/// List of substrings or regular expression patterns to filter out tags
/// </summary>
public ICollection<SubstringOrRegexPattern> TagFilters { get; set; } = new List<SubstringOrRegexPattern>();

/// <summary>
/// The worker used by the client to pass envelopes.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Sentry.SubstringOrRegexPattern> TagFilters { get; set; }
public void ConfigureScope(System.Action<Sentry.Scope> action) { }
}
public static class SentryLoggingOptionsExtensions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Sentry.SubstringOrRegexPattern> TagFilters { get; set; }
public void ConfigureScope(System.Action<Sentry.Scope> action) { }
}
public static class SentryLoggingOptionsExtensions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Sentry.SubstringOrRegexPattern> TagFilters { get; set; }
public void ConfigureScope(System.Action<Sentry.Scope> action) { }
}
public static class SentryLoggingOptionsExtensions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Sentry.SubstringOrRegexPattern> TagFilters { get; set; }
public void ConfigureScope(System.Action<Sentry.Scope> action) { }
}
public static class SentryLoggingOptionsExtensions
Expand Down
19 changes: 1 addition & 18 deletions test/Sentry.Extensions.Logging.Tests/SentryLoggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SubstringOrRegexPattern>());
sentryEvent = SentryLogger.CreateEvent(LogLevel.Debug, default, props, null, null, "category");
}
finally
{
Expand Down Expand Up @@ -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<KeyValuePair<string, object>>
{
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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Sentry.SubstringOrRegexPattern> TagFilters { get; set; }
public System.Collections.Generic.IList<Sentry.TracePropagationTarget> TracePropagationTargets { get; set; }
public double TracesSampleRate { get; set; }
public System.Func<Sentry.TransactionSamplingContext, double?>? TracesSampler { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Sentry.SubstringOrRegexPattern> TagFilters { get; set; }
public System.Collections.Generic.IList<Sentry.TracePropagationTarget> TracePropagationTargets { get; set; }
public double TracesSampleRate { get; set; }
public System.Func<Sentry.TransactionSamplingContext, double?>? TracesSampler { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Sentry.SubstringOrRegexPattern> TagFilters { get; set; }
public System.Collections.Generic.IList<Sentry.TracePropagationTarget> TracePropagationTargets { get; set; }
public double TracesSampleRate { get; set; }
public System.Func<Sentry.TransactionSamplingContext, double?>? TracesSampler { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Sentry.SubstringOrRegexPattern> TagFilters { get; set; }
public System.Collections.Generic.IList<Sentry.TracePropagationTarget> TracePropagationTargets { get; set; }
public double TracesSampleRate { get; set; }
public System.Func<Sentry.TransactionSamplingContext, double?>? TracesSampler { get; set; }
Expand Down
23 changes: 23 additions & 0 deletions test/Sentry.Tests/ScopeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<KeyValuePair<string, string>>
{
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
Expand Down