[OpenTelementry] Fix circular reference#7308
Conversation
Fix circular reference when `LoggerProvider` requires `ILogger`.
Add CHANGELOG entry.
Add PR number.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #7308 +/- ##
==========================================
- Coverage 89.81% 89.72% -0.10%
==========================================
Files 276 276
Lines 14822 14838 +16
==========================================
Hits 13313 13313
- Misses 1509 1525 +16
Flags with carried forward coverage won't be shown. Click here to find out more.
|
Resolve an ILogger to force resolution of dependencies.
Fix test that stopped throwing `NotSupportedException` to resolve the `ILoggerProvider` and `LoggerFactory` due to deferred creation.
There was a problem hiding this comment.
Pull request overview
This PR addresses a circular dependency in the logging DI integration when LoggerProvider construction requires ILogger/ILoggerFactory (e.g., via dependencies like IHttpClientFactory). It does so by deferring LoggerProvider resolution until the first logger is actually created, avoiding a re-entrant LoggerProvider build.
Changes:
- Update
OpenTelemetryLoggingExtensionsto pass aFunc<LoggerProvider>intoOpenTelemetryLoggerProvider, delayingLoggerProviderresolution toCreateLogger. - Make
OpenTelemetryLoggerProviderlazily initialize and cache theLoggerProviderfrom the provided factory (thread-safe). - Update affected tests (and changelog) to trigger the new lazy behavior by creating a logger.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
src/OpenTelemetry/Logs/ILogger/OpenTelemetryLoggingExtensions.cs |
Defers LoggerProvider resolution via a factory to avoid circular references during DI construction. |
src/OpenTelemetry/Logs/ILogger/OpenTelemetryLoggerProvider.cs |
Lazily resolves/caches LoggerProvider with synchronization to support deferred DI resolution. |
test/OpenTelemetry.Tests/Logs/OpenTelemetryLoggingExtensionsTests.cs |
Adjusts assertions and ensures circular-reference scenario is exercised by creating a logger. |
test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpLogExporterTests.cs |
Forces logger creation so configuration delegates are exercised under the new lazy-resolution behavior. |
test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpExporterHelperExtensionsTests.cs |
Updates the “throws on gRPC default factory” logging test to trigger failure via CreateLogger. |
src/OpenTelemetry/CHANGELOG.md |
Notes the circular reference fix. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Update test to assert that creating a logger from the logger provider throws `NotSupportedException`.
|
Asked Noah and Tarek to take a look at this PR. |
|
Thanks for digging this one out, nice find. The overall approach looks right to me: deferring the A few things worth considering before this goes in: 1. The deferred exception timing (your stated hesitation). I think this is the most important point to get explicit agreement on. Today a misconfiguration (like the OTLP gRPC default-factory 2. Memory model on the lazy field. The 3. Worth weighing 4. Does None of these block the direction, which I think is correct. Mostly want to make sure the exception-timing change is a conscious decision. Thanks again! |
|
I don't think I have anything to add to what @tarekgh already called out :) |
- Use `Volatile.Read/Write` to access provider. - Do not cache logging provider resolution failures. - Update CHANGELOG entry. - Add explanatory comments.
|
Comments should now be addressed. |
Found in #6899.
Changes
Fix circular reference when
LoggerProviderrequiresILogger.My only hesitation on this change as-is is the fact that I've had to update two tests outside of OpenTelemetry.Tests to account for this change and that the exception is only now thrown at the point
ILoggeris resolved and notILoggerFactory. Thoughts?Merge requirement checklist
CHANGELOG.mdfiles updated for non-trivial changesChanges in public API reviewed (if applicable)