diff --git a/src/OpenTelemetry.Instrumentation.EntityFrameworkCore/CHANGELOG.md b/src/OpenTelemetry.Instrumentation.EntityFrameworkCore/CHANGELOG.md index 2c8fc2c1de..78eea158e9 100644 --- a/src/OpenTelemetry.Instrumentation.EntityFrameworkCore/CHANGELOG.md +++ b/src/OpenTelemetry.Instrumentation.EntityFrameworkCore/CHANGELOG.md @@ -24,6 +24,9 @@ [spans](https://github.com/open-telemetry/semantic-conventions/blob/v1.28.0/docs/database/database-spans.md). ([#2130](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/2130)) +* EF Core instrumentation support together with SqlClient instrumentation. + ([#2280](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/2280)) + ## 1.0.0-beta.12 Released 2024-Jun-18 diff --git a/src/OpenTelemetry.Instrumentation.EntityFrameworkCore/Implementation/EntityFrameworkDiagnosticListener.cs b/src/OpenTelemetry.Instrumentation.EntityFrameworkCore/Implementation/EntityFrameworkDiagnosticListener.cs index 322c35c587..e8094fc2a2 100644 --- a/src/OpenTelemetry.Instrumentation.EntityFrameworkCore/Implementation/EntityFrameworkDiagnosticListener.cs +++ b/src/OpenTelemetry.Instrumentation.EntityFrameworkCore/Implementation/EntityFrameworkDiagnosticListener.cs @@ -29,7 +29,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 commandFetcher = new("Command"); private readonly PropertyFetcher connectionFetcher = new("Connection"); @@ -60,7 +60,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. @@ -169,7 +169,7 @@ public override void OnEventWritten(string name, object? payload) return; } - if (activity.Source != SqlClientActivitySource) + if (activity.Source != EntityFrameworkActivitySource) { return; } @@ -266,12 +266,18 @@ public override void OnEventWritten(string name, object? payload) return; } - if (activity.Source != SqlClientActivitySource) + if (activity.Source == EntityFrameworkActivitySource) { - return; + activity.Stop(); } - activity.Stop(); + // From some reason this EF event comes before SQLClient SqlMicrosoftAfterExecuteCommand event + // EF span should not be parrent of any other span except SQLClient, because of that it can be closed safetly + // Can result in a slightly strange timeline where the EF span finishes before its child SQLClient but based on EventSources it is true + if (activity.Parent?.Source == EntityFrameworkActivitySource) + { + activity.Parent.Stop(); + } } break; @@ -284,7 +290,7 @@ public override void OnEventWritten(string name, object? payload) return; } - if (activity.Source != SqlClientActivitySource) + if (activity.Source != EntityFrameworkActivitySource) { return; }