Fix DisableTelemetry not disabling metrics export#3156
Merged
Conversation
OTEL_SDK_DISABLED was set in IConfiguration too late (during AzureMonitorExporterOptions callback) after the OTel MeterProvider had already been constructed and checked the flag. Fix: Register a DisableTelemetryInitializerHostedService at position 0 in the service collection so it runs before OpenTelemetry's TelemetryHostedService, setting OTEL_SDK_DISABLED in IConfiguration before providers are built. Consolidated the registration in the shared AddTelemetryConfigAndClient method so both AspNetCore and WorkerService packages benefit from the fix.
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a startup timing issue where TelemetryConfiguration.DisableTelemetry = true did not reliably disable OpenTelemetry metrics export in DI scenarios, by setting OTEL_SDK_DISABLED early enough for the OTel SDK to honor it before provider construction.
Changes:
- Add a new hosted service that sets
OTEL_SDK_DISABLEDduring startup whenTelemetryConfiguration.DisableTelemetryis enabled. - Register the hosted service from the shared
AddTelemetryConfigAndClientpath so both AspNetCore and WorkerService benefit. - Remove the late
OTEL_SDK_DISABLEDassignment from the Azure Monitor exporter options configure callbacks.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| NETCORE/src/Shared/Shared.projitems | Includes the new shared hosted service in the shared compilation items. |
| NETCORE/src/Shared/Extensions/DisableTelemetryInitializerHostedService.cs | New startup hosted service that applies OTEL_SDK_DISABLED when telemetry is disabled. |
| NETCORE/src/Shared/Extensions/ApplicationInsightsExtensions.cs | Registers the hosted service early (index 0) to run before OpenTelemetry’s hosted service. |
| NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs | Removes late OTEL_SDK_DISABLED configuration from exporter options callback. |
| NETCORE/src/Microsoft.ApplicationInsights.WorkerService/ApplicationInsightsExtensions.cs | Removes late OTEL_SDK_DISABLED configuration from exporter options callback. |
| CHANGELOG.md | Adds an Unreleased entry describing the fix (but currently alters other Unreleased entries per diff). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
NETCORE/src/Shared/Extensions/DisableTelemetryInitializerHostedService.cs
Outdated
Show resolved
Hide resolved
… factory The hosted service approach did not work because MeterProvider is resolved during the DI configuration phase (via UseAzureMonitorExporter -> options resolution), before any hosted service runs. Move OTEL_SDK_DISABLED assignment into the TelemetryConfiguration singleton factory so it is set immediately after applying Configure callbacks. Since TelemetryConfiguration is always resolved before MeterProvider, this guarantees the flag is visible when the OTel SDK checks it. Remove DisableTelemetryInitializerHostedService (no longer needed).
harsimar
approved these changes
Mar 31, 2026
rajkumar-rangaraj
added a commit
that referenced
this pull request
Mar 31, 2026
PR #3156 accidentally removed the OTEL_SDK_DISABLED assignment from the AzureMonitorExporterOptions configure callbacks in both AspNetCore and WorkerService packages. This assignment is needed so the OTel SDK's TracerProvider sees the disabled flag when it checks IConfiguration. The TelemetryConfiguration factory (added in #3156) covers MeterProvider, but this callback is still required for TracerProvider which resolves through a different path.
harsimar
pushed a commit
that referenced
this pull request
Apr 1, 2026
…3157) * Restore OTEL_SDK_DISABLED in AzureMonitorExporterOptions callbacks PR #3156 accidentally removed the OTEL_SDK_DISABLED assignment from the AzureMonitorExporterOptions configure callbacks in both AspNetCore and WorkerService packages. This assignment is needed so the OTel SDK's TracerProvider sees the disabled flag when it checks IConfiguration. The TelemetryConfiguration factory (added in #3156) covers MeterProvider, but this callback is still required for TracerProvider which resolves through a different path. * Fix DisableTelemetry not disabling OTel LoggerProvider export Register a no-op ILoggerProvider at position 0 in the service collection that depends on TelemetryConfiguration. ILoggerFactory resolves all ILoggerProvider instances in registration order during host construction. By inserting this first, TelemetryConfiguration is resolved (which sets OTEL_SDK_DISABLED in IConfiguration) before OpenTelemetry's LoggerProvider factory checks the flag. Without this, LoggerProvider was constructed before OTEL_SDK_DISABLED was set, resulting in logs still being exported. * Update CHANGELOG entry for DisableTelemetry fix
This was referenced Apr 2, 2026
Open
Closed
Open
This was referenced Apr 3, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When using TelemetryConfiguration.DisableTelemetry = true (e.g., via services.Configure(tc => tc.DisableTelemetry = true)), metrics were still being exported. Setting OTEL_SDK_DISABLED=true directly in launchSettings.json worked correctly.
Root Cause
Timing issue: OTEL_SDK_DISABLED was being set in IConfiguration inside the AzureMonitorExporterOptions configure callback, which runs lazily after the OTel SDK has already constructed the MeterProvider. The MeterProviderBuilderBase checks OTEL_SDK_DISABLED at construction time and by then the flag wasn't set yet.
Fix
Changes
Testing