Skip to content

Add DisableTelemetry support using OpenTelemetry OTEL_SDK_DISABLED#3084

Merged
rajkumar-rangaraj merged 5 commits intomainfrom
rajrang/disablesdk
Jan 24, 2026
Merged

Add DisableTelemetry support using OpenTelemetry OTEL_SDK_DISABLED#3084
rajkumar-rangaraj merged 5 commits intomainfrom
rajrang/disablesdk

Conversation

@rajkumar-rangaraj
Copy link
Member

Summary

Adds support for disabling telemetry via TelemetryConfiguration.DisableTelemetry property by leveraging OpenTelemetry SDK 1.15.0's OTEL_SDK_DISABLED environment variable.

Changes

Core Implementation

  • TelemetryConfiguration.Build(): When DisableTelemetry is true, sets OTEL_SDK_DISABLED=true environment variable before creating the OpenTelemetry SDK, causing it to return no-op implementations

DI Support (AspNetCore/WorkerService)

  • Updated AddTelemetryConfigAndClient() to apply IConfigureOptions<TelemetryConfiguration> and IPostConfigureOptions<TelemetryConfiguration> callbacks, enabling services.Configure<TelemetryConfiguration>() pattern
  • Added TelemetryConfiguration injection in AzureMonitorExporterOptions.Configure to set OTEL_SDK_DISABLED in IConfiguration when DisableTelemetry is true

Tests

  • Added TelemetryConfigurationOtelSdkDisabledTests with 2 tests validating the feature for non-DI scenarios
  • Tests use [Collection("TelemetryClientTests")] to prevent parallel execution issues with environment variable manipulation

Documentation

  • Updated docs/concepts.md with usage examples for both direct and DI scenarios
  • Updated CHANGELOG.md

Usage

Non-DI:

var configuration = new TelemetryConfiguration
{
    ConnectionString = "...",
    DisableTelemetry = true
};
var tc = new TelemetryClient(configuration);

DI (ASP.NET Core / Worker Service):

// Must be called BEFORE AddApplicationInsightsTelemetry()
services.Configure<TelemetryConfiguration>(tc => tc.DisableTelemetry = true);
services.AddApplicationInsightsTelemetry();

Notes

  • Requires OpenTelemetry SDK 1.15.0+ which supports OTEL_SDK_DISABLED
  • For DI scenarios, DisableTelemetry must be configured before AddApplicationInsightsTelemetry() is called

Copilot AI review requested due to automatic review settings January 23, 2026 22:52
Copy link
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

This PR adds support for disabling telemetry collection via the TelemetryConfiguration.DisableTelemetry property by leveraging OpenTelemetry SDK 1.15.0's OTEL_SDK_DISABLED environment variable.

Changes:

  • Added DisableTelemetry property support in non-DI scenarios by setting OTEL_SDK_DISABLED environment variable in TelemetryConfiguration.Build()
  • Extended DI support to apply IConfigureOptions<TelemetryConfiguration> callbacks in the TelemetryConfiguration factory
  • Implemented DI telemetry disabling by setting OTEL_SDK_DISABLED in IConfiguration via AzureMonitorExporterOptions Configure callback
  • Added tests for non-DI scenarios and updated documentation

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
BASE/src/Microsoft.ApplicationInsights/Extensibility/TelemetryConfiguration.cs Sets OTEL_SDK_DISABLED environment variable when building SDK; removes CreateFromConfiguration method
BASE/src/Microsoft.ApplicationInsights/TelemetryClientExtensions.cs Changes StartOperation to return null instead of throwing ArgumentNullException when activity is null
NETCORE/src/Shared/Extensions/ApplicationInsightsExtensions.cs Updates TelemetryConfiguration factory to apply IConfigureOptions and IPostConfigureOptions callbacks
NETCORE/src/Microsoft.ApplicationInsights.WorkerService/ApplicationInsightsExtensions.cs Sets OTEL_SDK_DISABLED in IConfiguration when DisableTelemetry is true
NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs Sets OTEL_SDK_DISABLED in IConfiguration when DisableTelemetry is true
BASE/Test/Microsoft.ApplicationInsights.Test/Microsoft.ApplicationInsights.Tests/TelemetryConfigurationOtelSdkDisabledTests.cs Adds tests for DisableTelemetry in non-DI scenarios
docs/concepts.md Documents DisableTelemetry usage for both non-DI and DI scenarios
CHANGELOG.md Updates changelog with new feature entry
examples/AspNetCoreWebApp/Startup.cs Adds commented example showing DisableTelemetry usage in DI
.publicApi/Microsoft.ApplicationInsights.dll/Stable/PublicAPI.Unshipped.txt Removes CreateFromConfiguration method from public API

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@rajkumar-rangaraj rajkumar-rangaraj merged commit 10ad75f into main Jan 24, 2026
22 checks passed
@rajkumar-rangaraj rajkumar-rangaraj deleted the rajrang/disablesdk branch January 24, 2026 00:10
This was referenced Feb 1, 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.

2 participants