Skip to content

Commit

Permalink
Fix/sql client db name (#2418)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescrosswell committed Jun 12, 2023
1 parent 204d4f9 commit 333163f
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- DB Connection spans presented poorly ([#2409](https://github.com/getsentry/sentry-dotnet/pull/2409))
- Populate scope's Cookies property ([#2411](https://github.com/getsentry/sentry-dotnet/pull/2411))
- Fix UWP GateKeeper errors ([#2415](https://github.com/getsentry/sentry-dotnet/pull/2415))
- Fix sql client db name ([#2418](https://github.com/getsentry/sentry-dotnet/pull/2418))

### Dependencies

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
7 changes: 2 additions & 5 deletions test/Sentry.DiagnosticSource.Tests/SentrySqlListenerTests.cs
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

0 comments on commit 333163f

Please sign in to comment.