Skip to content

Avoid invalid traces when EF Core and Npgsql instrumentations run together#4855

Open
aviralgarg05 wants to merge 5 commits intoopen-telemetry:mainfrom
aviralgarg05:fix/efcore-npgsql-conflict
Open

Avoid invalid traces when EF Core and Npgsql instrumentations run together#4855
aviralgarg05 wants to merge 5 commits intoopen-telemetry:mainfrom
aviralgarg05:fix/efcore-npgsql-conflict

Conversation

@aviralgarg05
Copy link
Contributor

Why

Fixes #4252 – prevents invalid trace recursion when Entity Framework Core and Npgsql trace instrumentations are enabled together by removing the conflicting Npgsql instrumentation automatically.

What

  • Added FilterConflictingInstrumentations inside TracerSettings so that whenever EF Core tracing is enabled the Npgsql trace instrumentation is filtered out.
  • Hooked the filter into both the environment-variable and file-based configuration paths and emitted a warning to explain the automatic disabling.

Tests

  • dotnet test test/OpenTelemetry.AutoInstrumentation.Tests/OpenTelemetry.AutoInstrumentation.Tests.csproj -c Release (fails because dotnet is not available in this environment; please rerun locally once the SDK is installed.)

Checklist

  • CHANGELOG.md is updated.
  • Documentation is updated.
  • New features are covered by tests.

@aviralgarg05 aviralgarg05 requested a review from a team as a code owner February 17, 2026 09:34
@nrcventura
Copy link
Member

This approach, even though it works, has some tradeoff that may not be worth it. It seems to work by filtering the "enabled" instrumentation list. However, if someone is just using the default settings, and not using file based configuration to only opt-into the instrumentation that they care about, they will only get postgresql instrumentation if they are using EF Core to access postgres. The failing integration tests reflect that behavior.

Ideally I think we need to detect if an application is using both the EF Core and Posgres libraries and then disable one of them. Doing that is more complicated than just filtering things at configuration time, especially when there are no guaranties that both of the DB libraries will be loaded into memory at configuration time.

Maybe there's something that can be done via byte code injection into the method(s) used to register the postgres library with EF Core, and when that call runs we can update unsubscribe from the postgres ActivitySource.

@nrcventura
Copy link
Member

Can an integration test be added to help with preventing any regressions in this fix?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Invalid trace when Npsql and EFCore instrumentation are enabled

2 participants

Comments