-
-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduced the logger noise from EF when not using Performance Monitoring (
#1441) * Reduced the logger noise from EF when not using Performance Monitoring. Co-authored-by: Bruno Garcia <bruno@brunogarcia.com>
- Loading branch information
1 parent
2324af6
commit ad6f005
Showing
5 changed files
with
78 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,34 @@ | ||
using System.Data.Entity.Infrastructure.Interception; | ||
using Sentry.Extensibility; | ||
using Sentry.Integrations; | ||
|
||
namespace Sentry.EntityFramework; | ||
|
||
internal class DbInterceptionIntegration : ISdkIntegration | ||
{ | ||
private IDbInterceptor? _sqlInterceptor { get; set; } | ||
// Internal for testing. | ||
internal IDbInterceptor? SqlInterceptor { get; private set; } | ||
|
||
internal const string SampleRateDisabledMessage = "EF performance won't be collected because TracesSampleRate is set to 0."; | ||
|
||
public void Register(IHub hub, SentryOptions options) | ||
{ | ||
_sqlInterceptor = new SentryQueryPerformanceListener(hub, options); | ||
DbInterception.Add(_sqlInterceptor); | ||
if (options.TracesSampleRate == 0) | ||
{ | ||
options.DiagnosticLogger?.LogInfo(SampleRateDisabledMessage); | ||
} | ||
else | ||
{ | ||
SqlInterceptor = new SentryQueryPerformanceListener(hub, options); | ||
DbInterception.Add(SqlInterceptor); | ||
} | ||
} | ||
|
||
public void Unregister() => DbInterception.Remove(_sqlInterceptor); | ||
public void Unregister() | ||
{ | ||
if (SqlInterceptor is { } interceptor) | ||
{ | ||
DbInterception.Remove(interceptor); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
test/Sentry.EntityFramework.Tests/DbInterceptionIntegrationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using Sentry.Testing; | ||
|
||
namespace Sentry.EntityFramework.Tests | ||
{ | ||
public class DbInterceptionIntegrationTests | ||
{ | ||
[Fact] | ||
public void Register_TraceSAmpleRateZero_IntegrationNotRegistered() | ||
{ | ||
// Arrange | ||
var logger = Substitute.For<ITestOutputHelper>(); | ||
var options = new SentryOptions() | ||
{ | ||
Debug = true, | ||
DiagnosticLogger = new TestOutputDiagnosticLogger(logger, SentryLevel.Debug), | ||
TracesSampleRate = 0 | ||
}; | ||
var integration = new DbInterceptionIntegration(); | ||
|
||
// Act | ||
integration.Register(Substitute.For<IHub>(), options); | ||
|
||
// Assert | ||
logger.Received(1).WriteLine(Arg.Is<string>(message => message.Contains(DbInterceptionIntegration.SampleRateDisabledMessage))); | ||
Assert.Null(integration.SqlInterceptor); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters