Skip to content

Fix DisableTelemetry not disabling metrics export#3156

Merged
harsimar merged 4 commits intomainfrom
rajrang/disablesdkdibug
Mar 31, 2026
Merged

Fix DisableTelemetry not disabling metrics export#3156
harsimar merged 4 commits intomainfrom
rajrang/disablesdkdibug

Conversation

@rajkumar-rangaraj
Copy link
Copy Markdown
Member

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

  • Added a DisableTelemetryInitializerHostedService that is registered at position 0 in the service collection, ensuring it runs before OpenTelemetry's TelemetryHostedService.
  • On startup, it resolves TelemetryConfiguration, checks DisableTelemetry, and sets OTEL_SDK_DISABLED in IConfiguration before the OTel SDK constructs its providers.
  • Moved the registration into the shared AddTelemetryConfigAndClient method so both AspNetCore and WorkerService packages benefit from the fix (no duplication).
  • Removed the now-redundant OTEL_SDK_DISABLED assignment from the AzureMonitorExporterOptions callback in both packages.

Changes

File Change
\NETCORE/src/Shared/Extensions/DisableTelemetryInitializerHostedService.cs\ New hosted service class
\NETCORE/src/Shared/Extensions/ApplicationInsightsExtensions.cs\ Register hosted service in \AddTelemetryConfigAndClient\
\NETCORE/src/Shared/Shared.projitems\ Include new file in shared project
\NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs\ Remove late OTEL_SDK_DISABLED setting and duplicate registration
\NETCORE/src/Microsoft.ApplicationInsights.WorkerService/ApplicationInsightsExtensions.cs\ Remove late OTEL_SDK_DISABLED setting and duplicate registration

Testing

  • All existing unit tests pass (including \TelemetryConfigurationOtelSdkDisabledTests)
  • All integration tests pass (39/39 across net8.0, net9.0, net10.0)
  • Validated with AspNetCoreWebApp example

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.
Copilot AI review requested due to automatic review settings March 31, 2026 18:31
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_DISABLED during startup when TelemetryConfiguration.DisableTelemetry is enabled.
  • Register the hosted service from the shared AddTelemetryConfigAndClient path so both AspNetCore and WorkerService benefit.
  • Remove the late OTEL_SDK_DISABLED assignment 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.

- Replace IServiceProvider service locator with direct constructor injection
  of TelemetryConfiguration and IConfiguration in
  DisableTelemetryInitializerHostedService (both are singletons).
- Restore accidentally dropped CHANGELOG entries for #3153 and #3142.
… 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 harsimar merged commit 3f1c3bc into main Mar 31, 2026
18 checks passed
@harsimar harsimar deleted the rajrang/disablesdkdibug branch March 31, 2026 20:33
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
This was referenced Apr 3, 2026
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.

3 participants