diff --git a/CHANGELOG.md b/CHANGELOG.md index cabed4055..c0a95523f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +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 `OTEL_SDK_DISABLED` not being honored when `TelemetryConfiguration.DisableTelemetry` is set in DI scenarios.](https://github.com/microsoft/ApplicationInsights-dotnet/pull/3156), [#3157](https://github.com/microsoft/ApplicationInsights-dotnet/pull/3157) - [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 41ec09716..1380c6942 100644 --- a/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs +++ b/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs @@ -205,6 +205,12 @@ 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 d8cdfd494..34a3c0506 100644 --- a/NETCORE/src/Microsoft.ApplicationInsights.WorkerService/ApplicationInsightsExtensions.cs +++ b/NETCORE/src/Microsoft.ApplicationInsights.WorkerService/ApplicationInsightsExtensions.cs @@ -200,6 +200,12 @@ 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 919df48a8..b623e6fc1 100644 --- a/NETCORE/src/Shared/Extensions/ApplicationInsightsExtensions.cs +++ b/NETCORE/src/Shared/Extensions/ApplicationInsightsExtensions.cs @@ -20,6 +20,7 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; + using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Options; /// @@ -92,6 +93,17 @@ private static void AddTelemetryConfigAndClient(IServiceCollection services, str { services.AddOptions(); + // Register a no-op ILoggerProvider at position 0 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 runs. + // Without this, LoggerProvider checks IConfiguration before OTEL_SDK_DISABLED is set. + services.Insert(0, ServiceDescriptor.Singleton(sp => + { + _ = sp.GetRequiredService(); + return NullLoggerProvider.Instance; + })); + // Register TelemetryConfiguration singleton with factory that creates it for DI scenarios // We use a factory to ensure skipDefaultBuilderConfiguration: true is passed services.AddSingleton(provider =>