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

[Instrumentation.EFCore] Support together with Instrumentation.SqlClient #2280

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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 @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<object> commandFetcher = new("Command");
private readonly PropertyFetcher<object> connectionFetcher = new("Connection");
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -169,7 +169,7 @@ public override void OnEventWritten(string name, object? payload)
return;
}

if (activity.Source != SqlClientActivitySource)
if (activity.Source != EntityFrameworkActivitySource)
{
return;
}
Expand Down Expand Up @@ -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;
Expand All @@ -284,7 +290,7 @@ public override void OnEventWritten(string name, object? payload)
return;
}

if (activity.Source != SqlClientActivitySource)
if (activity.Source != EntityFrameworkActivitySource)
{
return;
}
Expand Down
Loading