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

Fix/sql client db name #2418

Merged
merged 38 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from 37 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
a1fb23f
Refactored Add/Finish spans logic into Helper classes
jamescrosswell May 29, 2023
80d24bb
Implemented logic to correlate Add/Finish spans using Diagnostic Conn…
jamescrosswell May 29, 2023
54fe8a2
Replaced AsyncLocl<WeakReference> with Correlation IDs extracted from…
jamescrosswell Jun 1, 2023
086a8be
Refactored to use Extra instead of TraceData, for consistency with Se…
jamescrosswell Jun 1, 2023
589fab1
Merge branch 'main' into fix/db-spans-not-finishing
jamescrosswell Jun 1, 2023
fe73cbf
Update CHANGELOG.md
jamescrosswell Jun 1, 2023
f1e9785
Update SqlListenerTests.RecordsEf.Net4_8.verified.txt
jamescrosswell Jun 1, 2023
2ac37af
Undo csproj whitespace
mattjohnsonpint Jun 1, 2023
6b731f0
Apply suggestions from code review
jamescrosswell Jun 1, 2023
852e160
Refactored SentryEFCoreListener so DiagnosticSourceHelpers are create…
jamescrosswell Jun 1, 2023
64732f5
Merge branch 'fix/db-spans-not-finishing' of https://github.com/getse…
jamescrosswell Jun 2, 2023
5dc7010
Implemented fix for DB Connection spans presented poorly #2144
jamescrosswell Jun 2, 2023
f869c51
Merge branch 'main' into fix/db-spans-presentation
jamescrosswell Jun 2, 2023
945393c
Update CHANGELOG.md
jamescrosswell Jun 2, 2023
fa21697
Fixed timing in OnNext_Same_Connections_Consolidated so we can reliab…
jamescrosswell Jun 2, 2023
ec44c6b
Moved db.query spans back under the associated db.connection
jamescrosswell Jun 5, 2023
fa0f431
EFConnectionDiagnosticSourceHelper now uses the "Connection.Database"…
jamescrosswell Jun 6, 2023
a9010d9
Fixed errors with unit tests
jamescrosswell Jun 6, 2023
37cc1bf
Merge branch 'main' of git@github.com:getsentry/sentry-dotnet.git int…
jamescrosswell Jun 6, 2023
f278a58
Merge branch 'main' into fix/db-spans-presentation
mattjohnsonpint Jun 7, 2023
f54f332
Update CHANGELOG.md
mattjohnsonpint Jun 7, 2023
8448f47
fix merge conflict
mattjohnsonpint Jun 7, 2023
232ceaf
nits
mattjohnsonpint Jun 7, 2023
93dff1b
Update extension method improvements to apply to all property types
mattjohnsonpint Jun 7, 2023
551a20b
Restore test changes (bad merge)
mattjohnsonpint Jun 7, 2023
9eec767
Fixed whitespace error in Verify tests
jamescrosswell Jun 7, 2023
028c7b6
Flattened DB Connection and Query spans produced by the Sentry DB Dia…
jamescrosswell Jun 8, 2023
f7decd0
Renamed verify tests to conform with async naming conventions
jamescrosswell Jun 8, 2023
553e69a
SqlClient diagnostics now uses the Databasename to describe db.connec…
jamescrosswell Jun 8, 2023
8525585
nits
mattjohnsonpint Jun 8, 2023
1b100f2
Fixed typos
jamescrosswell Jun 8, 2023
5186953
EFCommand operations now record the db.name in the Span.Extra properties
jamescrosswell Jun 8, 2023
e22365c
Merge remote-tracking branch 'getsentry/main' into fix/sql-client-db-…
jamescrosswell Jun 9, 2023
ab02ef9
SqlClient diagnostics listeners now record the db.name
jamescrosswell Jun 9, 2023
4439728
Added db.name to Query spans in the SentrySqlListener
jamescrosswell Jun 9, 2023
b8a24ae
Update CHANGELOG.md
jamescrosswell Jun 9, 2023
21af27c
nits
mattjohnsonpint Jun 12, 2023
006c404
Update CHANGELOG.md
mattjohnsonpint Jun 12, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- SentryHttpMessageHandler added when AddHttpClient is before UseSentry ([#2390](https://github.com/getsentry/sentry-dotnet/pull/2390))
- Set the native sdk name for Android ([#2389](https://github.com/getsentry/sentry-dotnet/pull/2389))
- Fix db connection spans not finishing ([#2398](https://github.com/getsentry/sentry-dotnet/pull/2398))
- Fix sql client db name ([#2418](https://github.com/getsentry/sentry-dotnet/pull/2418))
- Various .NET MAUI fixes / improvements ([#2403](https://github.com/getsentry/sentry-dotnet/pull/2403))
- The battery level was being reported incorrectly due to percentage multiplier.
- The device architecture (x64, arm64, etc.) is now reported
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,13 @@ private void AddSpan(string operation, object? value)
return;
}

var operationId = value?.GetGuidProperty("OperationId");
var connectionId = value?.GetGuidProperty("ConnectionId");
var parent = transaction.GetDbParentSpan();
var span = parent.StartChild(operation);
SetOperationId(span, operationId);
SetConnectionId(span, connectionId);
SetOperationId(span, value?.GetGuidProperty("OperationId"));
SetConnectionId(span, value?.GetGuidProperty("ConnectionId"));
}

private ISpan? GetSpan(SentrySqlSpanType type, KeyValuePair<string, object?> kvp)
private ISpan? GetSpan(SentrySqlSpanType type, object? value)
{
var transaction = _hub.GetTransactionIfSampled();
if (transaction == null)
Expand All @@ -90,7 +88,7 @@ private void AddSpan(string operation, object? value)
switch (type)
{
case SentrySqlSpanType.Execution:
var operationId = kvp.Value?.GetGuidProperty("OperationId");
var operationId = value?.GetGuidProperty("OperationId");
if (TryGetQuerySpan(transaction, operationId) is { } querySpan)
{
return querySpan;
Expand All @@ -102,7 +100,7 @@ private void AddSpan(string operation, object? value)
return null;

case SentrySqlSpanType.Connection:
var connectionId = kvp.Value?.GetGuidProperty("ConnectionId");
var connectionId = value?.GetGuidProperty("ConnectionId");
if (TryGetConnectionSpan(transaction, connectionId) is { } connectionSpan)
{
return connectionSpan;
Expand Down Expand Up @@ -166,6 +164,36 @@ private void UpdateConnectionSpan(object? value)
}
}

private void FinishCommandSpan(object? value, SpanStatus spanStatus)
{
if (value == null)
{
return;
}


if (GetSpan(SentrySqlSpanType.Execution, value) is not { } commandSpan)
{
return;
}

// Try to lookup the associated connection span so that we can store the db.name in
// the command span as well. This will be easier for users to read/identify than the
// ConnectionId (which is a Guid)
var connectionId = value.GetGuidProperty("ConnectionId");
var transaction = _hub.GetTransactionIfSampled();
if (TryGetConnectionSpan(transaction!, connectionId) is { } connectionSpan)
{
if (connectionSpan.Extra.TryGetValue<string, string>(OTelKeys.DbName) is { } dbName)
{
SetDatabaseName(commandSpan, dbName);
}
}

commandSpan.Description = value.GetStringProperty("Command.CommandText");
commandSpan.Finish(spanStatus);
}

public void OnCompleted() { }

public void OnError(Exception error) { }
Expand All @@ -180,15 +208,11 @@ public void OnNext(KeyValuePair<string, object?> kvp)
case SqlMicrosoftBeforeExecuteCommand or SqlDataBeforeExecuteCommand:
AddSpan("db.query", kvp.Value);
return;
case SqlMicrosoftAfterExecuteCommand or SqlDataAfterExecuteCommand
when GetSpan(SentrySqlSpanType.Execution, kvp) is { } commandSpan:
commandSpan.Description = kvp.Value?.GetStringProperty("Command.CommandText");
commandSpan.Finish(SpanStatus.Ok);
case SqlMicrosoftAfterExecuteCommand or SqlDataAfterExecuteCommand:
FinishCommandSpan(kvp.Value, SpanStatus.Ok);
return;
case SqlMicrosoftWriteCommandError or SqlDataWriteCommandError
when GetSpan(SentrySqlSpanType.Execution, kvp) is { } errorSpan:
errorSpan.Description = kvp.Value?.GetStringProperty("Command.CommandText");
errorSpan.Finish(SpanStatus.InternalError);
case SqlMicrosoftWriteCommandError or SqlDataWriteCommandError:
FinishCommandSpan(kvp.Value, SpanStatus.InternalError);
return;

// Connection
Expand All @@ -199,7 +223,7 @@ when GetSpan(SentrySqlSpanType.Execution, kvp) is { } errorSpan:
UpdateConnectionSpan(kvp.Value);
return;
case SqlMicrosoftWriteConnectionCloseAfterCommand or SqlDataWriteConnectionCloseAfterCommand
when GetSpan(SentrySqlSpanType.Connection, kvp) is { } closeSpan:
when GetSpan(SentrySqlSpanType.Connection, kvp.Value) is { } closeSpan:
TrySetConnectionStatistics(closeSpan, kvp.Value);
closeSpan.Finish(SpanStatus.Ok);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ WHERE @@ROWCOUNT = 1 AND [Id] = scope_identity();
IsSampled: true,
Extra: {
db.connection_id: Guid_1,
db.name: SqlListenerTests.verify_LoggingAsync,
db.operation_id: Guid_3
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ WHERE @@ROWCOUNT = 1 AND [Id] = scope_identity();
IsSampled: true,
Extra: {
db.connection_id: Guid_1,
db.name: SqlListenerTests.verify_LoggingAsync,
db.operation_id: Guid_3
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ WHERE @@ROWCOUNT = 1 AND [Id] = scope_identity();
IsSampled: true,
Extra: {
db.connection_id: Guid_1,
db.name: SqlListenerTests.verify_RecordsEfAsync,
db.operation_id: Guid_3
}
},
Expand All @@ -95,6 +96,7 @@ FROM [TestEntities] AS [t],
IsSampled: true,
Extra: {
db.connection_id: Guid_1,
db.name: SqlListenerTests.verify_RecordsEfAsync,
db.operation_id: Guid_4
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ WHERE @@ROWCOUNT = 1 AND [Id] = scope_identity();
IsSampled: true,
Extra: {
db.connection_id: Guid_1,
db.name: SqlListenerTests.verify_RecordsEfAsync,
db.operation_id: Guid_3
}
},
Expand All @@ -95,6 +96,7 @@ FROM [TestEntities] AS [t],
IsSampled: true,
Extra: {
db.connection_id: Guid_1,
db.name: SqlListenerTests.verify_RecordsEfAsync,
db.operation_id: Guid_4
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ WHERE @@ROWCOUNT = 1 AND [Id] = scope_identity();
IsSampled: true,
Extra: {
db.connection_id: Guid_1,
db.name: SqlListenerTests.verify_RecordsEfAsync,
db.operation_id: Guid_3
}
},
Expand All @@ -95,6 +96,7 @@ FROM [TestEntities] AS [t],
IsSampled: true,
Extra: {
db.connection_id: Guid_1,
db.name: SqlListenerTests.verify_RecordsEfAsync,
db.operation_id: Guid_4
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
IsSampled: true,
Extra: {
db.connection_id: Guid_1,
db.name: SqlListenerTests.verify_RecordsSqlAsync,
db.operation_id: Guid_3
}
},
Expand All @@ -78,6 +79,7 @@
IsSampled: true,
Extra: {
db.connection_id: Guid_1,
db.name: SqlListenerTests.verify_RecordsSqlAsync,
db.operation_id: Guid_4
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,7 @@ public void OnNext_HappyPaths_IsValid(string connectionOpenKey, string connectio
new
{
OperationId = connectionOperationIdClosed,
ConnectionId = connectionId,
Connection = new
{
Database = dbName
}
ConnectionId = connectionId
}));

// Assert
Expand All @@ -245,6 +241,7 @@ public void OnNext_HappyPaths_IsValid(string connectionOpenKey, string connectio
Assert.Equal(query, commandSpan.Description);
Assert.Equal(queryOperationId, commandSpan.Extra.TryGetValue<string, Guid>(SqlKeys.DbOperationId));
Assert.Equal(connectionId, commandSpan.Extra.TryGetValue<string, Guid>(SqlKeys.DbConnectionId));
Assert.Equal(dbName, commandSpan.Extra.TryGetValue<string, string>(OTelKeys.DbName));

Assert.Equal(dbName, connectionSpan.Description);
Assert.Equal(connectionOperationId, connectionSpan.Extra.TryGetValue<string, Guid>(SqlKeys.DbOperationId));
Expand Down