Skip to content

Commit

Permalink
Reduced the logger noise from EF when not using Performance Monitoring (
Browse files Browse the repository at this point in the history
#1441)

* Reduced the logger noise from EF when not using Performance Monitoring.

Co-authored-by: Bruno Garcia <bruno@brunogarcia.com>
  • Loading branch information
lucas-zimerman and bruno-garcia authored Feb 1, 2022
1 parent 2324af6 commit ad6f005
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

### Fixes

- Reduced the logger noise from EF when not using Performance Monitoring ([#1441](https://github.com/getsentry/sentry-dotnet/pull/1441))
- Create CachingTransport directories in constructor to avoid DirectoryNotFoundException ([#1432](https://github.com/getsentry/sentry-dotnet/pull/1432))

## 3.13.0
Expand Down
25 changes: 21 additions & 4 deletions src/Sentry.EntityFramework/DbInterceptionIntegration.cs
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);
}
}
}
5 changes: 3 additions & 2 deletions src/Sentry.EntityFramework/SentryQueryPerformanceListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ private void Finish<T>(string key, DbCommandInterceptionContext<T> interceptionC
span.Finish(interceptionContext.Exception);
}
}
else
//Only log if there was a transaction on the Hub.
else if (_options.DiagnosticLevel == SentryLevel.Debug && _hub.GetSpan() is { })
{
_options.DiagnosticLogger?.LogWarning("Span with key {0} was not found on interceptionContext.", key);
_options.DiagnosticLogger?.LogDebug("Span with key {0} was not found on interceptionContext.", key);
}
}
}
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Data.Common;
using System.Data.Entity.Infrastructure.Interception;
using Effort.Provider;
using Sentry.Testing;

namespace Sentry.EntityFramework.Tests;

Expand Down Expand Up @@ -199,7 +200,7 @@ public void FirstOrDefault_FromDatabase_CapturesQuery()
{
// Arrange
var integration = new DbInterceptionIntegration();
integration.Register(_fixture.Hub, new SentryOptions());
integration.Register(_fixture.Hub, new SentryOptions() { TracesSampleRate = 1 });

// Act
_ = _fixture.DbContext.TestTable.FirstOrDefault();
Expand All @@ -221,4 +222,27 @@ public void FirstOrDefault_FromDatabase_CapturesQuery()
});
integration.Unregister();
}

[Fact]
public void Finish_NoActiveTransaction_LoggerNotCalled()
{
// Arrange
var hub = _fixture.Hub;
hub.GetSpan().Returns((_) => null);
var logger = Substitute.For<ITestOutputHelper>();

var options = new SentryOptions()
{
Debug = true,
DiagnosticLogger = new TestOutputDiagnosticLogger(logger, SentryLevel.Debug)
};

var listener = new SentryQueryPerformanceListener(hub, options);

// Act
listener.ScalarExecuted(Substitute.For<DbCommand>(), Substitute.For<DbCommandInterceptionContext<object>>());

// Assert
logger.Received(0).WriteLine(Arg.Any<string>());
}
}

0 comments on commit ad6f005

Please sign in to comment.