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
Expand Up @@ -2,6 +2,10 @@

## Unreleased

* Support use with `SqlClient` instrumentation.
([#2280](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/2280),
[#2829](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/2829))

## 1.12.0-beta.1

Released 2025-May-05
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ internal sealed class EntityFrameworkDiagnosticListener : ListenerHandler
internal static readonly Assembly Assembly = typeof(EntityFrameworkDiagnosticListener).Assembly;
internal static readonly string ActivitySourceName = Assembly.GetName().Name;
internal static readonly string ActivityName = ActivitySourceName + ".Execute";
internal static readonly ActivitySource SqlClientActivitySource = new(ActivitySourceName, Assembly.GetPackageVersion());
internal static readonly ActivitySource EntityFrameworkActivitySource = new(ActivitySourceName, Assembly.GetPackageVersion());

private readonly PropertyFetcher<object> commandFetcher = new("Command");
private readonly PropertyFetcher<object> connectionFetcher = new("Connection");
Expand Down Expand Up @@ -59,7 +59,7 @@ public override void OnEventWritten(string name, object? payload)
{
case EntityFrameworkCoreCommandCreated:
{
activity = SqlClientActivitySource.StartActivity(ActivityName, ActivityKind.Client);
activity = EntityFrameworkActivitySource.StartActivity(ActivityName, ActivityKind.Client);
if (activity == null)
{
// There is no listener or it decided not to sample the current request.
Expand All @@ -80,38 +80,57 @@ public override void OnEventWritten(string name, object? payload)

if (activity.IsAllDataRequested)
{
var dbContext = this.dbContextFetcher.Fetch(payload);
var dbContextDatabase = this.dbContextDatabaseFetcher.Fetch(dbContext);
var providerName = this.providerNameFetcher.Fetch(dbContextDatabase);
string? providerOrCommandName = null;

switch (providerName)
if (this.dbContextFetcher.Fetch(payload) is { } dbContext)
{
var dbContextDatabase = this.dbContextDatabaseFetcher.Fetch(dbContext);
providerOrCommandName = this.providerNameFetcher.Fetch(dbContextDatabase);
}
else
{
// Try to infer the database name from the command
// type if the DbContext is not available.
providerOrCommandName = command.GetType().FullName;
}

switch (providerOrCommandName)
{
case "Microsoft.EntityFrameworkCore.SqlServer":
case "Microsoft.Data.SqlClient.SqlCommand":
activity.AddTag(AttributeDbSystem, "mssql");
break;
case "Microsoft.EntityFrameworkCore.Cosmos":
activity.AddTag(AttributeDbSystem, "cosmosdb");
break;
case "Microsoft.Data.Sqlite.SqliteCommand":
case "Microsoft.EntityFrameworkCore.Sqlite":
case "Devart.Data.SQLite.Entity.EFCore":
activity.AddTag(AttributeDbSystem, "sqlite");
break;
case "MySql.Data.EntityFrameworkCore":
case "MySql.Data.MySqlClient.MySqlCommand":
case "Pomelo.EntityFrameworkCore.MySql":
case "Devart.Data.MySql.Entity.EFCore":
case "Devart.Data.MySql.MySqlCommand":
activity.AddTag(AttributeDbSystem, "mysql");
break;
case "Npgsql.EntityFrameworkCore.PostgreSQL":
case "Npgsql.NpgsqlCommand":
case "Devart.Data.PostgreSql.Entity.EFCore":
case "Devart.Data.PostgreSql.PgSqlCommand":
activity.AddTag(AttributeDbSystem, "postgresql");
break;
case "Oracle.EntityFrameworkCore":
case "Oracle.ManagedDataAccess.Client.OracleCommand":
case "Devart.Data.Oracle.Entity.EFCore":
case "Devart.Data.Oracle.OracleCommand":
activity.AddTag(AttributeDbSystem, "oracle");
break;
case "Microsoft.EntityFrameworkCore.InMemory":
activity.AddTag(AttributeDbSystem, "efcoreinmemory");
break;
case "FirebirdSql.Data.FirebirdClient.FbCommand":
case "FirebirdSql.EntityFrameworkCore.Firebird":
activity.AddTag(AttributeDbSystem, "firebird");
break;
Expand All @@ -120,23 +139,27 @@ public override void OnEventWritten(string name, object? payload)
break;
case "EntityFrameworkCore.SqlServerCompact35":
case "EntityFrameworkCore.SqlServerCompact40":
case "System.Data.SqlServerCe.SqlCeCommand":
activity.AddTag(AttributeDbSystem, "mssqlcompact");
break;
case "EntityFrameworkCore.OpenEdge":
activity.AddTag(AttributeDbSystem, "openedge");
break;
case "EntityFrameworkCore.Jet":
case "EntityFrameworkCore.Jet.Data.JetCommand":
activity.AddTag(AttributeDbSystem, "jet");
break;
case "Google.Cloud.EntityFrameworkCore.Spanner":
case "Google.Cloud.Spanner.Data.SpannerCommand":
activity.AddTag(AttributeDbSystem, "spanner");
break;
case "Teradata.Client.Provider.TdCommand":
case "Teradata.EntityFrameworkCore":
activity.AddTag(AttributeDbSystem, "teradata");
break;
default:
activity.AddTag(AttributeDbSystem, "other_sql");
activity.AddTag("ef.provider", providerName);
activity.AddTag("ef.provider", providerOrCommandName);
break;
}

Expand Down Expand Up @@ -168,7 +191,7 @@ public override void OnEventWritten(string name, object? payload)
return;
}

if (activity.Source != SqlClientActivitySource)
if (activity.Source != EntityFrameworkActivitySource)
{
return;
}
Expand All @@ -179,9 +202,13 @@ public override void OnEventWritten(string name, object? payload)

try
{
var dbContext = this.dbContextFetcher.Fetch(payload);
var dbContextDatabase = this.dbContextDatabaseFetcher.Fetch(dbContext);
var providerName = this.providerNameFetcher.Fetch(dbContextDatabase);
string? providerName = null;

if (this.dbContextFetcher.Fetch(payload) is { } dbContext)
{
var dbContextDatabase = this.dbContextDatabaseFetcher.Fetch(dbContext);
providerName = this.providerNameFetcher.Fetch(dbContextDatabase);
}

if (command is IDbCommand typedCommand && this.options.Filter?.Invoke(providerName, typedCommand) == false)
{
Expand Down Expand Up @@ -267,12 +294,18 @@ public override void OnEventWritten(string name, object? payload)
return;
}

if (activity.Source != SqlClientActivitySource)
if (activity.Source == EntityFrameworkActivitySource)
{
return;
activity.Stop();
}

activity.Stop();
// For some reason this EF event comes before the SQLClient SqlMicrosoftAfterExecuteCommand event.
// EF span should not be parent of any other span except SQLClient, because of that it can be closed safely.
// Can result in a slightly strange timeline where the EF span finishes before its child SQLClient but based on EventSource's it is true.
if (activity.Parent?.Source == EntityFrameworkActivitySource)
{
activity.Parent.Stop();
}
}

break;
Expand All @@ -285,7 +318,7 @@ public override void OnEventWritten(string name, object? payload)
return;
}

if (activity.Source != SqlClientActivitySource)
if (activity.Source != EntityFrameworkActivitySource)
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="$(MicrosoftExtensionsConfigurationPkgVer)"/>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="$(MicrosoftExtensionsConfigurationPkgVer)" />
<PackageReference Include="Microsoft.Extensions.Options" Version="$(MicrosoftExtensionsOptionsPkgVer)" />
<PackageReference Include="OpenTelemetry.Api.ProviderBuilderExtensions" Version="$(OpenTelemetryCoreLatestVersion)" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,42 +299,14 @@ private void Seed()
context.Database.EnsureDeleted();
context.Database.EnsureCreated();

var one = new Item("ItemOne");
var one = new Item() { Name = "ItemOne" };

var two = new Item("ItemTwo");
var two = new Item() { Name = "ItemTwo" };

var three = new Item("ItemThree");
var three = new Item() { Name = "ItemThree" };

context.AddRange(one, two, three);

context.SaveChanges();
}

private class Item
{
public Item(string name)
{
this.Name = name;
}

public string Name { get; }
}

private class ItemsContext : DbContext
{
public ItemsContext(DbContextOptions options)
: base(options)
{
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Item>(
b =>
{
b.Property(e => e.Name);
b.HasKey("Name");
});
}
}
}
Loading
Loading