Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Sentry.Extensibility;
using Sentry.Internal.DataCollection;
using Sentry.Internal.Extensions;

namespace Sentry.Internal.DiagnosticSource;
Expand All @@ -11,7 +12,16 @@ internal EFCommandDiagnosticSourceHelper(IHub hub, SentryOptions options) : base

protected override string Operation => "db.query";

protected override string GetDescription(object? diagnosticSourceValue) => FilterNewLineValue(diagnosticSourceValue) ?? string.Empty;
protected override string GetDescription(object? diagnosticSourceValue)
Comment thread
jamescrosswell marked this conversation as resolved.
Resolved
{
// EF Core 10 redacts sensitive inlined values in LogCommandText only; older versions don't redact
var commandText = (diagnosticSourceValue?.GetStringProperty("LogCommandText", Options.DiagnosticLogger)
?? diagnosticSourceValue?.GetStringProperty("Command.CommandText", Options.DiagnosticLogger))?.TrimEnd()
?? FilterNewLineValue(diagnosticSourceValue) ?? string.Empty;
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
return Options.DataCollectionResolver.SanitizeDbStatements
? SqlSanitizer.Sanitize(commandText, GetDatabaseSystem(diagnosticSourceValue))
: commandText;
}

private Guid? GetCommandId(object? diagnosticSourceValue) => diagnosticSourceValue?.GetGuidProperty("CommandId", Options.DiagnosticLogger);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ internal EFQueryCompilerDiagnosticSourceHelper(IHub hub, SentryOptions options)

protected override string Operation => "db.query.compile";

protected override string GetDescription(object? diagnosticSourceValue) => FilterNewLineValue(diagnosticSourceValue) ?? string.Empty;
protected override string? GetDescription(object? diagnosticSourceValue) =>
Options.DataCollectionResolver.SanitizeDbStatements
? GetDatabaseSystem(diagnosticSourceValue)
: FilterNewLineValue(diagnosticSourceValue) ?? string.Empty;

/// <summary>
/// We don't have a correlation id for compiled query events. We just return the first unfinished query compile span.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#if !NETFRAMEWORK

using Sentry.Extensibility;
using Sentry.Internal.DataCollection;
using Sentry.Internal.Extensions;

namespace Sentry.Internal.DiagnosticSource;
Expand Down Expand Up @@ -210,7 +211,10 @@ private void FinishCommandSpan(object? value, SpanStatus spanStatus)
}
}

commandSpan.Description = value.GetStringProperty("Command.CommandText", _options.DiagnosticLogger);
var commandText = value.GetStringProperty("Command.CommandText", _options.DiagnosticLogger);
commandSpan.Description = _options.DataCollectionResolver.SanitizeDbStatements
? SqlSanitizer.Sanitize(commandText, "mssql")
: commandText;
commandSpan.Finish(spanStatus);
}

Expand Down
15 changes: 15 additions & 0 deletions src/Sentry.EntityFramework/DbCommandExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Sentry.EntityFramework;

internal static class DbCommandExtensions
{
internal static string? GetDbSystem(this DbCommand command) => DbSystemForNamespace(command.GetType().Namespace);

internal static string? DbSystemForNamespace(string? commandNamespace) => commandNamespace switch
{
"System.Data.SqlClient" or "Microsoft.Data.SqlClient" => "mssql",
"System.Data.SqlServerCe" => "mssqlcompact",
"MySql.Data.MySqlClient" or "MySqlConnector" or "Devart.Data.MySql" => "mysql",
"System.Data.SQLite" or "Microsoft.Data.Sqlite" or "Devart.Data.SQLite" => "sqlite",
_ => null
};
}
7 changes: 4 additions & 3 deletions src/Sentry.EntityFramework/SentryCommandInterceptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,14 @@ public virtual void Log<T>(DbCommand command, DbCommandInterceptionContext<T> in
return;
}

if (interceptionContext.Exception == null)
var level = interceptionContext.Exception == null ? BreadcrumbLevel.Debug : BreadcrumbLevel.Error;
if (_queryLogger is SentryQueryLogger sentryQueryLogger)
{
_queryLogger.Log(command.CommandText);
sentryQueryLogger.Log(command.CommandText, command.GetDbSystem(), level);
}
else
{
_queryLogger.Log(command.CommandText, BreadcrumbLevel.Error);
_queryLogger.Log(command.CommandText, level);
}
}
}
9 changes: 8 additions & 1 deletion src/Sentry.EntityFramework/SentryQueryLogger.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Sentry.Internal.DataCollection;

namespace Sentry.EntityFramework;

internal class SentryQueryLogger : IQueryLogger
Expand All @@ -7,8 +9,13 @@ internal class SentryQueryLogger : IQueryLogger
public SentryQueryLogger(IHub? hub = null) => _hub = hub ?? HubAdapter.Instance;

public void Log(string text, BreadcrumbLevel level = BreadcrumbLevel.Debug)
=> Log(text, null, level);

internal void Log(string text, string? dbSystem, BreadcrumbLevel level)
=> _hub.AddBreadcrumb(
message: text,
message: _hub.GetSentryOptions()?.DataCollectionResolver.SanitizeDbStatements == true
? SqlSanitizer.Sanitize(text, dbSystem)
: text,
category: "Entity Framework",
type: null,
data: null,
Expand Down
21 changes: 14 additions & 7 deletions src/Sentry.EntityFramework/SentryQueryPerformanceListener.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Sentry.Internal;
using Sentry.Internal.DataCollection;

namespace Sentry.EntityFramework;

Expand All @@ -21,31 +22,37 @@ internal SentryQueryPerformanceListener(IHub hub, SentryOptions options)
}

public void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
=> CreateSpan(DbReaderKey, command.CommandText, interceptionContext);
=> CreateSpan(DbReaderKey, command, interceptionContext);

public void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
=> Finish(DbReaderKey, interceptionContext);

public void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
=> CreateSpan(DbNonQueryKey, command.CommandText, interceptionContext);
=> CreateSpan(DbNonQueryKey, command, interceptionContext);

public void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
=> Finish(DbNonQueryKey, interceptionContext);

public void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
=> CreateSpan(DbScalarKey, command.CommandText, interceptionContext);
=> CreateSpan(DbScalarKey, command, interceptionContext);

public void ScalarExecuted(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
=> Finish(DbScalarKey, interceptionContext);

private void CreateSpan<T>(string key, string? command,
private void CreateSpan<T>(string key, DbCommand command,
DbCommandInterceptionContext<T> interceptionContext)
{
if (_hub.GetSpan()?.StartChild(key, command) is { } span)
if (_hub.GetSpan() is not { } parent)
{
span.SetOrigin(EntityFrameworkOrigin);
interceptionContext.AttachSpan(span);
return;
}

var description = _options.DataCollectionResolver.SanitizeDbStatements
? SqlSanitizer.Sanitize(command.CommandText, command.GetDbSystem())
: command.CommandText;
var span = parent.StartChild(key, description);
span.SetOrigin(EntityFrameworkOrigin);
interceptionContext.AttachSpan(span);
}

/// <summary>
Expand Down
Loading
Loading