diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a1933499..cabed4055 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Changelog ## Unreleased +- [Fix `DisableTelemetry` not disabling metrics export: `OTEL_SDK_DISABLED` was set in IConfiguration too late (during options callback) after the OTel MeterProvider had already been constructed. Moved the check to a hosted service that runs before OpenTelemetry's TelemetryHostedService.](https://github.com/microsoft/ApplicationInsights-dotnet/pull/3156) - [Fix `TrackAvailability` ignoring user-specified `Timestamp` on `AvailabilityTelemetry`. The timestamp is now emitted as `microsoft.availability.testTimestamp` so downstream exporters can use the correct event time.](https://github.com/microsoft/ApplicationInsights-dotnet/pull/3153) - [Added `netstandard2.0` target framework to `Microsoft.ApplicationInsights` package to support customers with shared libraries targeting netstandard2.0.](https://github.com/microsoft/ApplicationInsights-dotnet/pull/3142) - [Fix `TelemetryClient.Context.Cloud.RoleName` set after construction now applies to all telemetry.](https://github.com/microsoft/ApplicationInsights-dotnet/pull/3129) diff --git a/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs b/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs index 1380c6942..41ec09716 100644 --- a/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs +++ b/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs @@ -205,12 +205,6 @@ internal static IOpenTelemetryBuilder UseApplicationInsightsTelemetry(this IOpen { var serviceOptions = aiOptions.Value; - // Set OTEL_SDK_DISABLED in configuration if DisableTelemetry is true - if (telemetryConfig.DisableTelemetry) - { - config["OTEL_SDK_DISABLED"] = "true"; - } - if (!string.IsNullOrEmpty(telemetryConfig.StorageDirectory)) { exporterOptions.StorageDirectory = telemetryConfig.StorageDirectory; diff --git a/NETCORE/src/Microsoft.ApplicationInsights.WorkerService/ApplicationInsightsExtensions.cs b/NETCORE/src/Microsoft.ApplicationInsights.WorkerService/ApplicationInsightsExtensions.cs index 34a3c0506..d8cdfd494 100644 --- a/NETCORE/src/Microsoft.ApplicationInsights.WorkerService/ApplicationInsightsExtensions.cs +++ b/NETCORE/src/Microsoft.ApplicationInsights.WorkerService/ApplicationInsightsExtensions.cs @@ -200,12 +200,6 @@ internal static IOpenTelemetryBuilder UseApplicationInsightsTelemetry(this IOpen { var serviceOptions = aiOptions.Value; - // Set OTEL_SDK_DISABLED in configuration if DisableTelemetry is true - if (telemetryConfig.DisableTelemetry) - { - config["OTEL_SDK_DISABLED"] = "true"; - } - if (!string.IsNullOrEmpty(telemetryConfig.StorageDirectory)) { exporterOptions.StorageDirectory = telemetryConfig.StorageDirectory; diff --git a/NETCORE/src/Shared/Extensions/ApplicationInsightsExtensions.cs b/NETCORE/src/Shared/Extensions/ApplicationInsightsExtensions.cs index c494d4231..919df48a8 100644 --- a/NETCORE/src/Shared/Extensions/ApplicationInsightsExtensions.cs +++ b/NETCORE/src/Shared/Extensions/ApplicationInsightsExtensions.cs @@ -91,7 +91,7 @@ private static bool IsApplicationInsightsAdded(IServiceCollection services) private static void AddTelemetryConfigAndClient(IServiceCollection services, string extensionVersion) { services.AddOptions(); - + // Register TelemetryConfiguration singleton with factory that creates it for DI scenarios // We use a factory to ensure skipDefaultBuilderConfiguration: true is passed services.AddSingleton(provider => @@ -121,7 +121,18 @@ private static void AddTelemetryConfigAndClient(IServiceCollection services, str { postConfigure.PostConfigure(Options.DefaultName, configuration); } - + + // Set OTEL_SDK_DISABLED in IConfiguration so the OTel SDK's MeterProvider/TracerProvider + // factories see it when they check IsOtelSdkDisabled(). This must happen here (during + // TelemetryConfiguration resolution) rather than in a hosted service, because the + // MeterProvider singleton factory can be triggered during the DI build phase + // (e.g., via UseAzureMonitorExporter -> options resolution) before any hosted service runs. + if (configuration.DisableTelemetry) + { + var iconfig = provider.GetRequiredService(); + iconfig["OTEL_SDK_DISABLED"] = "true"; + } + return configuration; });