Skip to content

Commit

Permalink
feat: Added DB attributes to spans (#2583)
Browse files Browse the repository at this point in the history
  • Loading branch information
bitsandfoxes authored Sep 6, 2023
1 parent c2a31b4 commit db270dc
Show file tree
Hide file tree
Showing 16 changed files with 184 additions and 41 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Features

- Added additional `DB` attributes to automatically generated spans like `name` and `provider` ([#2583](https://github.com/getsentry/sentry-dotnet/pull/2583))
- `Hints` now accept attachments provided as a file path via `AddAttachment` method ([#2585](https://github.com/getsentry/sentry-dotnet/pull/2585))

### Fixes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace Sentry.Internal.DiagnosticSource;
internal static class OTelKeys
{
internal const string DbName = "db.name";
internal const string DbSystem = "db.system";
internal const string DbServer = "db.server";
}

/// <summary>
Expand All @@ -27,3 +29,40 @@ internal static class EFKeys
internal const string DbConnectionId = "db.connection_id";
internal const string DbCommandId = "db.command_id";
}

/// <summary>
/// Mapping of Database Providers to known Open Telemetry db.system
/// https://learn.microsoft.com/en-us/ef/core/providers/?tabs=dotnet-core-cli
/// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/database.md#notes-and-well-known-identifiers-for-dbsystem
/// </summary>
internal static class DatabaseProviderSystems
{
public static readonly Dictionary<string, string> ProviderSystems = new()
{
{ "Microsoft.EntityFrameworkCore.SqlServer", "mssql" },
{ "Microsoft.EntityFrameworkCore.Sqlite", "sqlite" },
{ "Microsoft.EntityFrameworkCore.InMemory", "inmemory" },
{ "Microsoft.EntityFrameworkCore.Cosmos", "cosmosdb" },
{ "Npgsql.EntityFrameworkCore.PostgreSQL", "postgresql" },
{ "Pomelo.EntityFrameworkCore.MySql", "mysql" },
{ "MySql.EntityFrameworkCore", "mysql" },
{ "Oracle.EntityFrameworkCore", "oracle" },
{ "Devart.Data.MySql.EFCore", "mysql" },
{ "Devart.Data.Oracle.EFCore", "oracle" },
{ "Devart.Data.PostgreSql.EFCore", "postgres" },
{ "Devart.Data.SQLite.EFCore", "sqlite" },
{ "InterBase", "interbase" },
{ "FirebirdSql.EntityFrameworkCore.Firebird", "firebird" },
{ "IBM.EntityFrameworkCore", "db2" },
{ "IBM.EntityFrameworkCore-lnx", "db2" },
{ "IBM.EntityFrameworkCore-osx", "db2" },
{ "EntityFrameworkCore.Jet", "accessfiles" },
{ "Google.Cloud.EntityFrameworkCore.Spanner", "spanner" },
{ "Teradata.EntityFrameworkCore", "teradata" },
{ "FileContextCore", "datainfiles" },
{ "FileBaseContext", "tablesinfiles" },
{ "EntityFrameworkCore.SqlServerCompact35", "mssqlcompact" },
{ "EntityFrameworkCore.SqlServerCompact40", "mssqlcompact" },
{ "EntityFrameworkCore.OpenEdge", "progress" },
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ private static void SetCommandId(ISpan span, Guid? commandId)

protected override void SetSpanReference(ISpan span, object? diagnosticSourceValue)
{
if (GetDatabaseName(diagnosticSourceValue) is { } databaseName)
{
span.SetExtra(OTelKeys.DbName, databaseName);
}

if (GetCommandId(diagnosticSourceValue) is { } commandId)
{
SetCommandId(span, commandId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ internal EFConnectionDiagnosticSourceHelper(IHub hub, SentryOptions options) : b

protected override void SetSpanReference(ISpan span, object? diagnosticSourceValue)
{
if (GetDatabaseName(diagnosticSourceValue) is { } databaseName)
{
span.SetExtra(OTelKeys.DbName, databaseName);
}

if (GetConnectionId(diagnosticSourceValue) is { } connectionId)
{
SetConnectionId(span, connectionId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,25 @@ internal abstract class EFDiagnosticSourceHelper
protected static string? GetDatabaseName(object? diagnosticSourceValue) =>
diagnosticSourceValue?.GetStringProperty("Connection.Database");

protected static string? GetDatabaseSystem(object? diagnosticSourceValue)
{
var providerName = diagnosticSourceValue?.GetStringProperty("Context.Database.ProviderName");
if (providerName is null)
{
return null;
}

if (DatabaseProviderSystems.ProviderSystems.TryGetValue(providerName, out var dbSystem))
{
return dbSystem;
}

return null;
}

protected static string? GetDatabaseServerAddress(object? diagnosticSourceValue) =>
diagnosticSourceValue?.GetStringProperty("Connection.DataSource");

internal EFDiagnosticSourceHelper(IHub hub, SentryOptions options)
{
Options = options;
Expand Down Expand Up @@ -46,6 +65,7 @@ internal void AddSpan(object? diagnosticSourceValue)
var parent = Transaction.GetDbParentSpan();
var child = parent.StartChild(Operation, GetDescription(diagnosticSourceValue));

SetDbData(child, diagnosticSourceValue);
SetSpanReference(child, diagnosticSourceValue);
}

Expand All @@ -69,6 +89,24 @@ internal void FinishSpan(object? diagnosticSourceValue, SpanStatus status)
sourceSpan.Finish(status);
}

protected void SetDbData(ISpan span, object? diagnosticSourceValue)
{
if (GetDatabaseName(diagnosticSourceValue) is { } dataBaseName)
{
span.SetExtra(OTelKeys.DbName, dataBaseName);
}

if (GetDatabaseSystem(diagnosticSourceValue) is { } databaseProviderName)
{
span.SetExtra(OTelKeys.DbSystem, databaseProviderName);
}

if (GetDatabaseServerAddress(diagnosticSourceValue) is { } databaseServerAddress)
{
span.SetExtra(OTelKeys.DbServer, databaseServerAddress);
}
}

protected void LogTransactionSpans()
{
if (Transaction == null)
Expand Down Expand Up @@ -102,7 +140,7 @@ protected void LogTransactionSpans()
return str?[(str.IndexOf('\n') + 1)..];
}

protected abstract void SetSpanReference(ISpan span, object? diagnosticSourceValue);

protected abstract ISpan? GetSpanReference(ITransaction transaction, object? diagnosticSourceValue);

protected abstract void SetSpanReference(ISpan span, object? diagnosticSourceValue);
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ private static void SetDatabaseName(ISpan span, string databaseName)
span.SetExtra(OTelKeys.DbName, databaseName);
}

private static void SetDatabaseAddress(ISpan span, string databaseAddress)
{
Debug.Assert(databaseAddress != string.Empty);

span.SetExtra(OTelKeys.DbServer, databaseAddress);
}

private static void SetConnectionId(ISpan span, Guid? connectionId)
{
Debug.Assert(connectionId != Guid.Empty);
Expand Down Expand Up @@ -73,6 +80,7 @@ private void AddSpan(string operation, object? value)

var parent = transaction.GetDbParentSpan();
var span = parent.StartChild(operation);
span.SetExtra(OTelKeys.DbSystem, "sql");
SetOperationId(span, value?.GetGuidProperty("OperationId"));
SetConnectionId(span, value?.GetGuidProperty("ConnectionId"));
}
Expand Down Expand Up @@ -161,6 +169,11 @@ private void UpdateConnectionSpan(object? value)
connectionSpan.Description = dbName;
SetDatabaseName(connectionSpan, dbName);
}

if (value.GetStringProperty("Connection.DataSource") is { } dbSource)
{
SetDatabaseAddress(connectionSpan, dbSource);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public LocalDbFixture()
}

SqlInstance = new(
name: "SqlListenerTests" + Namer.RuntimeAndVersion,
name: "SqlListenerTests",
buildTemplate: TestDbBuilder.CreateTableAsync);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ WHERE @@ROWCOUNT = 1 AND [Id] = scope_identity();,
db.connection_id: Guid_1,
db.name: SqlListenerTests.verify_LoggingAsync,
db.operation_id: Guid_2,
db.server: (LocalDb)\SqlListenerTests,
db.system: sql,
rows_sent: 0
}
},
Expand All @@ -99,7 +101,8 @@ WHERE @@ROWCOUNT = 1 AND [Id] = scope_identity();
Extra: {
db.connection_id: Guid_1,
db.name: SqlListenerTests.verify_LoggingAsync,
db.operation_id: Guid_3
db.operation_id: Guid_3,
db.system: sql
}
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ WHERE @@ROWCOUNT = 1 AND [Id] = scope_identity();,
db.connection_id: Guid_1,
db.name: SqlListenerTests.verify_LoggingAsync,
db.operation_id: Guid_2,
db.server: (LocalDb)\SqlListenerTests,
db.system: sql,
rows_sent: 0
}
},
Expand All @@ -99,7 +101,8 @@ WHERE @@ROWCOUNT = 1 AND [Id] = scope_identity();
Extra: {
db.connection_id: Guid_1,
db.name: SqlListenerTests.verify_LoggingAsync,
db.operation_id: Guid_3
db.operation_id: Guid_3,
db.system: sql
}
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
db.connection_id: Guid_1,
db.name: SqlListenerTests.verify_RecordsEfAsync,
db.operation_id: Guid_2,
db.server: (LocalDb)\SqlListenerTests,
db.system: sql,
rows_sent: 1
}
},
Expand All @@ -76,7 +78,8 @@ WHERE @@ROWCOUNT = 1 AND [Id] = scope_identity();
Extra: {
db.connection_id: Guid_1,
db.name: SqlListenerTests.verify_RecordsEfAsync,
db.operation_id: Guid_3
db.operation_id: Guid_3,
db.system: sql
}
},
{
Expand All @@ -97,11 +100,12 @@ FROM [TestEntities] AS [t],
Extra: {
db.connection_id: Guid_1,
db.name: SqlListenerTests.verify_RecordsEfAsync,
db.operation_id: Guid_4
db.operation_id: Guid_4,
db.system: sql
}
}
],
IsFinished: true
}
}
]
]
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
db.connection_id: Guid_1,
db.name: SqlListenerTests.verify_RecordsEfAsync,
db.operation_id: Guid_2,
db.server: (LocalDb)\SqlListenerTests,
db.system: sql,
rows_sent: 1
}
},
Expand All @@ -76,7 +78,8 @@ WHERE @@ROWCOUNT = 1 AND [Id] = scope_identity();
Extra: {
db.connection_id: Guid_1,
db.name: SqlListenerTests.verify_RecordsEfAsync,
db.operation_id: Guid_3
db.operation_id: Guid_3,
db.system: sql
}
},
{
Expand All @@ -97,11 +100,12 @@ FROM [TestEntities] AS [t],
Extra: {
db.connection_id: Guid_1,
db.name: SqlListenerTests.verify_RecordsEfAsync,
db.operation_id: Guid_4
db.operation_id: Guid_4,
db.system: sql
}
}
],
IsFinished: true
}
}
]
]
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
db.connection_id: Guid_1,
db.name: SqlListenerTests.verify_RecordsEfAsync,
db.operation_id: Guid_2,
db.server: (LocalDb)\SqlListenerTests,
db.system: sql,
rows_sent: 1
}
},
Expand All @@ -76,7 +78,8 @@ WHERE @@ROWCOUNT = 1 AND [Id] = scope_identity();
Extra: {
db.connection_id: Guid_1,
db.name: SqlListenerTests.verify_RecordsEfAsync,
db.operation_id: Guid_3
db.operation_id: Guid_3,
db.system: sql
}
},
{
Expand All @@ -97,11 +100,12 @@ FROM [TestEntities] AS [t],
Extra: {
db.connection_id: Guid_1,
db.name: SqlListenerTests.verify_RecordsEfAsync,
db.operation_id: Guid_4
db.operation_id: Guid_4,
db.system: sql
}
}
],
IsFinished: true
}
}
]
]
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ WHERE @@ROWCOUNT = 1 AND [Id] = scope_identity();,
Extra: {
db.command_id: Guid_1,
db.connection_id: Guid_2,
db.name: SqlListenerTests.verify_RecordsEfAsync
db.name: SqlListenerTests.verify_RecordsEfAsync,
db.server: (LocalDb)\SqlListenerTests,
db.system: mssql
}
},
{
Expand All @@ -73,11 +75,13 @@ FROM [TestEntities] AS [t],
Extra: {
db.command_id: Guid_3,
db.connection_id: Guid_4,
db.name: SqlListenerTests.verify_RecordsEfAsync
db.name: SqlListenerTests.verify_RecordsEfAsync,
db.server: (LocalDb)\SqlListenerTests,
db.system: mssql
}
}
],
IsFinished: true
}
}
]
]
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
db.connection_id: Guid_1,
db.name: SqlListenerTests.verify_RecordsSqlAsync,
db.operation_id: Guid_2,
db.server: (LocalDb)\SqlListenerTests,
db.system: sql,
rows_sent: 1
}
},
Expand All @@ -68,7 +70,8 @@
Extra: {
db.connection_id: Guid_1,
db.name: SqlListenerTests.verify_RecordsSqlAsync,
db.operation_id: Guid_3
db.operation_id: Guid_3,
db.system: sql
}
},
{
Expand All @@ -80,11 +83,12 @@
Extra: {
db.connection_id: Guid_1,
db.name: SqlListenerTests.verify_RecordsSqlAsync,
db.operation_id: Guid_4
db.operation_id: Guid_4,
db.system: sql
}
}
],
IsFinished: true
}
}
]
]
Loading

0 comments on commit db270dc

Please sign in to comment.