Skip to content

Commit

Permalink
Add tag filters to SentryLoggingOptions (#2360)
Browse files Browse the repository at this point in the history
  • Loading branch information
SeanFeldman committed May 10, 2023
1 parent 711d365 commit 050cfab
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Features

- Add tag filters to SentryLoggingOptions ([#2360](https://github.com/getsentry/sentry-dotnet/pull/2360))

## 3.31.0

### Features
Expand Down
16 changes: 14 additions & 2 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);
var @event = CreateEvent(logLevel, eventId, state, exception, message, CategoryName, _options.TagFilters);

_ = _hub.CaptureEvent(@event);
}
Expand Down Expand Up @@ -75,7 +75,14 @@ public bool IsEnabled(LogLevel logLevel)
}
}

internal static SentryEvent CreateEvent<TState>(LogLevel logLevel, EventId id, TState state, Exception? exception, string? message, string category)
internal static SentryEvent CreateEvent<TState>(
LogLevel logLevel,
EventId id,
TState state,
Exception? exception,
string? message,
string category,
ICollection<SubstringOrRegexPattern> tagFilters)
{
var @event = new SentryEvent(exception)
{
Expand All @@ -99,6 +106,11 @@ internal static SentryEvent CreateEvent<TState>(LogLevel logLevel, EventId id, T
continue;
}

if (tagFilters.Any(x => x.IsMatch(property.Key)))
{
continue;
}

switch (property.Value)
{
case string stringTagValue:
Expand Down
5 changes: 5 additions & 0 deletions src/Sentry.Extensions.Logging/SentryLoggingOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,9 @@ 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>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ 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,6 +41,7 @@ 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,6 +41,7 @@ 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,6 +41,7 @@ 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: 18 additions & 1 deletion 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");
sentryEvent = SentryLogger.CreateEvent(LogLevel.Debug, default, props, null, null, "category", Array.Empty<SubstringOrRegexPattern>());
}
finally
{
Expand Down Expand Up @@ -531,4 +531,21 @@ 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");
}
}

0 comments on commit 050cfab

Please sign in to comment.