From a4b725494d378bd50c4279441586f4df59b3dc4b Mon Sep 17 00:00:00 2001 From: Harsimar Kaur Date: Fri, 23 Jan 2026 16:31:11 -0800 Subject: [PATCH 1/6] initial code changes, need to run tests --- .../Stable/PublicAPI.Unshipped.txt | 2 - .../Stable/PublicAPI.Unshipped.txt | 2 - BreakingChanges.md | 6 +- NETCORE/Readme.md | 19 ----- NETCORE/WorkerService.md | 1 - .../ApplicationInsightsExtensions.cs | 6 -- .../ApplicationInsightsExtensions.cs | 6 -- .../ApplicationInsightsServiceOptions.cs | 9 --- .../CustomWebApplicationFactory.cs | 1 - .../content/config-all-default.json | 1 - .../content/config-all-settings-false.json | 1 - .../content/config-all-settings-true.json | 1 - .../AddApplicationInsightsTelemetryTests.cs | 69 ------------------- .../content/config-all-default.json | 1 - .../content/config-all-settings-false.json | 1 - .../content/config-all-settings-true.json | 1 - .../ExtensionsTest.cs | 44 ------------ .../content/config-all-default.json | 1 - .../content/config-all-settings-false.json | 1 - .../content/config-all-settings-true.json | 1 - NETCORE/test/Shared/ConfigurationTests.cs | 65 +---------------- .../WorkerHostFixture.cs | 1 - 22 files changed, 5 insertions(+), 235 deletions(-) diff --git a/.publicApi/Microsoft.ApplicationInsights.AspNetCore.dll/Stable/PublicAPI.Unshipped.txt b/.publicApi/Microsoft.ApplicationInsights.AspNetCore.dll/Stable/PublicAPI.Unshipped.txt index 59c2efd7e..612c6c31c 100644 --- a/.publicApi/Microsoft.ApplicationInsights.AspNetCore.dll/Stable/PublicAPI.Unshipped.txt +++ b/.publicApi/Microsoft.ApplicationInsights.AspNetCore.dll/Stable/PublicAPI.Unshipped.txt @@ -8,8 +8,6 @@ Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOp Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.ConnectionString.set -> void Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.Credential.get -> Azure.Core.TokenCredential Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.Credential.set -> void -Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.EnableAdaptiveSampling.get -> bool -Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.EnableAdaptiveSampling.set -> void Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.EnableAuthenticationTrackingJavaScript.get -> bool Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.EnableAuthenticationTrackingJavaScript.set -> void Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.EnableDependencyTrackingTelemetryModule.get -> bool diff --git a/.publicApi/Microsoft.ApplicationInsights.WorkerService.dll/Stable/PublicAPI.Unshipped.txt b/.publicApi/Microsoft.ApplicationInsights.WorkerService.dll/Stable/PublicAPI.Unshipped.txt index 1e82d9d1f..e7852d609 100644 --- a/.publicApi/Microsoft.ApplicationInsights.WorkerService.dll/Stable/PublicAPI.Unshipped.txt +++ b/.publicApi/Microsoft.ApplicationInsights.WorkerService.dll/Stable/PublicAPI.Unshipped.txt @@ -8,8 +8,6 @@ Microsoft.ApplicationInsights.WorkerService.ApplicationInsightsServiceOptions.Co Microsoft.ApplicationInsights.WorkerService.ApplicationInsightsServiceOptions.ConnectionString.set -> void Microsoft.ApplicationInsights.WorkerService.ApplicationInsightsServiceOptions.Credential.get -> Azure.Core.TokenCredential Microsoft.ApplicationInsights.WorkerService.ApplicationInsightsServiceOptions.Credential.set -> void -Microsoft.ApplicationInsights.WorkerService.ApplicationInsightsServiceOptions.EnableAdaptiveSampling.get -> bool -Microsoft.ApplicationInsights.WorkerService.ApplicationInsightsServiceOptions.EnableAdaptiveSampling.set -> void Microsoft.ApplicationInsights.WorkerService.ApplicationInsightsServiceOptions.EnableDependencyTrackingTelemetryModule.get -> bool Microsoft.ApplicationInsights.WorkerService.ApplicationInsightsServiceOptions.EnableDependencyTrackingTelemetryModule.set -> void Microsoft.ApplicationInsights.WorkerService.ApplicationInsightsServiceOptions.EnablePerformanceCounterCollectionModule.get -> bool diff --git a/BreakingChanges.md b/BreakingChanges.md index 18d169e57..350852ae7 100644 --- a/BreakingChanges.md +++ b/BreakingChanges.md @@ -217,12 +217,10 @@ The following extension methods remain with identical signatures: - ❌ **`EnableHeartbeat`** - Heartbeat configuration removed - ❌ **`RequestCollectionOptions`** - Removed (non-functional, use OpenTelemetry instrumentation options) - ❌ **`DependencyCollectionOptions`** - Removed (non-functional, use OpenTelemetry instrumentation options) +- ❌ `EnableAdaptiveSampling`** - Removed, rate limited sampling is now the default. ### Properties Retained - ✅ **`ConnectionString`** - Primary configuration method -- ✅ **`EnableAdaptiveSampling`** - Still configurable (but behavior changed) - - **2.x**: Controls traditional adaptive sampling - - **3.x**: Maps to rate-limit based sampling in Azure Monitor Exporter, set at 5 OpenTelemetry traces per second by default - ✅ **`ApplicationVersion`** - ✅ **`AddAutoCollectedMetricExtractor`** - ✅ **`EnableQuickPulseMetricStream`** @@ -285,11 +283,11 @@ The following extension methods remain with identical signatures: - ❌ **`DeveloperMode`** - No longer configurable - ❌ **`EndpointAddress`** - No longer configurable (`ConnectionString` contains endpoints) - ❌ **`DependencyCollectionOptions`** - Removed (non-functional, use OpenTelemetry instrumentation options) +- ❌ **`EnableAdaptiveSampling`** - Removed, rate limited sampling is now the default. ### Properties Retained - ✅ **`ConnectionString`** - Primary configuration method (maps to `AzureMonitorExporterOptions.ConnectionString`) - ✅ **`ApplicationVersion`** - Still configurable -- ✅ **`EnableAdaptiveSampling`** - When true the rate limited sampler is used at 5 traces per second. - ✅ **`EnableDependencyTrackingTelemetryModule`** - Still configurable - ✅ **`EnablePerformanceCounterCollectionModule`** - Still configurable - ✅ **`EnableQuickPulseMetricStream`** - Maps to `AzureMonitorExporterOptions.EnableLiveMetrics` diff --git a/NETCORE/Readme.md b/NETCORE/Readme.md index 65fb9d0e0..d6d0693fb 100644 --- a/NETCORE/Readme.md +++ b/NETCORE/Readme.md @@ -159,11 +159,6 @@ builder.Services.AddApplicationInsightsTelemetry(options => // Enable or disable Live Metrics (default: true) options.EnableQuickPulseMetricStream = true; - - // Enable or disable adaptive sampling (default: true) - // When true, enables rate-limit based sampling in Azure Monitor Exporter - // When false, sampling is disabled (100% collection) - options.EnableAdaptiveSampling = true; }); ``` @@ -367,17 +362,6 @@ builder.Services.AddApplicationInsightsTelemetry(options => }); ``` -#### Disabling Adaptive Sampling - -To collect 100% of telemetry (disable sampling): - -```csharp -builder.Services.AddApplicationInsightsTelemetry(options => -{ - options.EnableAdaptiveSampling = false; -}); -``` - #### Dropping Specific Metrics Instruments To exclude specific instruments from being collected: @@ -464,8 +448,6 @@ The following features are **no longer available** in version 3.x: 3. **Configuration** - Connection strings are now configured via `ApplicationInsightsServiceOptions.ConnectionString` or `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable. -4. **Sampling** - Sampling is now controlled by the `EnableAdaptiveSampling` option, which configures the Azure Monitor Exporter's sampling behavior. - 5. **Live Metrics** - Still supported via the `EnableQuickPulseMetricStream` option. ### Migration Steps @@ -898,7 +880,6 @@ After migrating, thoroughly test your application to ensure: - [ ] Remove all ITelemetryProcessor implementations - [ ] Remove all custom ITelemetryModule implementations - [ ] Replace RequestCollectionOptions with AspNetCoreTraceInstrumentationOptions -- [ ] Update sampling configuration (use EnableAdaptiveSampling) - [ ] Test all custom telemetry collection - [ ] Verify Live Metrics is working (if enabled) - [ ] Verify all expected telemetry appears in Azure Monitor diff --git a/NETCORE/WorkerService.md b/NETCORE/WorkerService.md index c30142b6e..b30474787 100644 --- a/NETCORE/WorkerService.md +++ b/NETCORE/WorkerService.md @@ -119,7 +119,6 @@ builder.Services.AddApplicationInsightsTelemetryWorkerService(options => { options.ConnectionString = "InstrumentationKey=..."; options.EnableQuickPulseMetricStream = true; - options.EnableAdaptiveSampling = true; }); ``` diff --git a/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs b/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs index fe737ad05..6dd9d2de3 100644 --- a/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs +++ b/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs @@ -203,12 +203,6 @@ internal static IOpenTelemetryBuilder UseApplicationInsightsTelemetry(this IOpen { exporterOptions.Credential = serviceOptions.Credential; } - - if (!serviceOptions.EnableAdaptiveSampling) - { - exporterOptions.SamplingRatio = 1.0F; - exporterOptions.TracesPerSecond = null; - } if (serviceOptions.EnableQuickPulseMetricStream) { diff --git a/NETCORE/src/Microsoft.ApplicationInsights.WorkerService/ApplicationInsightsExtensions.cs b/NETCORE/src/Microsoft.ApplicationInsights.WorkerService/ApplicationInsightsExtensions.cs index 2455cb978..c8c457182 100644 --- a/NETCORE/src/Microsoft.ApplicationInsights.WorkerService/ApplicationInsightsExtensions.cs +++ b/NETCORE/src/Microsoft.ApplicationInsights.WorkerService/ApplicationInsightsExtensions.cs @@ -199,12 +199,6 @@ internal static IOpenTelemetryBuilder UseApplicationInsightsTelemetry(this IOpen exporterOptions.Credential = serviceOptions.Credential; } - if (!serviceOptions.EnableAdaptiveSampling) - { - exporterOptions.SamplingRatio = 1.0F; - exporterOptions.TracesPerSecond = null; - } - exporterOptions.EnableLiveMetrics = serviceOptions.EnableQuickPulseMetricStream; // Configure standard metrics and performance counter collection using reflection diff --git a/NETCORE/src/Shared/Extensions/ApplicationInsightsServiceOptions.cs b/NETCORE/src/Shared/Extensions/ApplicationInsightsServiceOptions.cs index b7e8b5cc8..df2827336 100644 --- a/NETCORE/src/Shared/Extensions/ApplicationInsightsServiceOptions.cs +++ b/NETCORE/src/Shared/Extensions/ApplicationInsightsServiceOptions.cs @@ -30,14 +30,6 @@ public class ApplicationInsightsServiceOptions /// public bool EnableDependencyTrackingTelemetryModule { get; set; } = true; - /// - /// Gets or sets a value indicating whether telemetry processor that controls sampling is added to the service. - /// Setting EnableAdaptiveSampling to false, will disable sampling entirely. - /// When true (default), enables rate-limit based sampling in Azure Monitor Exporter. - /// Defaults to true. - /// - public bool EnableAdaptiveSampling { get; set; } = true; - /// /// Gets or sets the connection string for the application. /// @@ -92,7 +84,6 @@ internal void CopyPropertiesTo(ApplicationInsightsServiceOptions target) } target.ApplicationVersion = this.ApplicationVersion; - target.EnableAdaptiveSampling = this.EnableAdaptiveSampling; target.EnableQuickPulseMetricStream = this.EnableQuickPulseMetricStream; target.AddAutoCollectedMetricExtractor = this.AddAutoCollectedMetricExtractor; target.EnablePerformanceCounterCollectionModule = this.EnablePerformanceCounterCollectionModule; diff --git a/NETCORE/test/IntegrationTests.Tests/CustomWebApplicationFactory.cs b/NETCORE/test/IntegrationTests.Tests/CustomWebApplicationFactory.cs index 382d9897d..5f673d8b5 100644 --- a/NETCORE/test/IntegrationTests.Tests/CustomWebApplicationFactory.cs +++ b/NETCORE/test/IntegrationTests.Tests/CustomWebApplicationFactory.cs @@ -43,7 +43,6 @@ protected override void ConfigureWebHost(IWebHostBuilder builder) { options.AddAutoCollectedMetricExtractor = false; options.EnableQuickPulseMetricStream = false; - options.EnableAdaptiveSampling = false; options.ConnectionString = "InstrumentationKey=ikey"; }); }); diff --git a/NETCORE/test/IntegrationTests.Tests/content/config-all-default.json b/NETCORE/test/IntegrationTests.Tests/content/config-all-default.json index 2506a4083..a6e2e18d0 100644 --- a/NETCORE/test/IntegrationTests.Tests/content/config-all-default.json +++ b/NETCORE/test/IntegrationTests.Tests/content/config-all-default.json @@ -1,7 +1,6 @@ { "ApplicationInsights": { "ConnectionString": "InstrumentationKey=11111111-2222-3333-4444-555555555555;IngestionEndpoint=http://testendpoint", - "EnableAdaptiveSampling": false, "EnableQuickPulseMetricStream": true, "ApplicationVersion": "Version" } diff --git a/NETCORE/test/IntegrationTests.Tests/content/config-all-settings-false.json b/NETCORE/test/IntegrationTests.Tests/content/config-all-settings-false.json index c7a4d1e5e..2821b2414 100644 --- a/NETCORE/test/IntegrationTests.Tests/content/config-all-settings-false.json +++ b/NETCORE/test/IntegrationTests.Tests/content/config-all-settings-false.json @@ -1,7 +1,6 @@ { "ApplicationInsights": { "ConnectionString": "InstrumentationKey=22222222-2222-3333-4444-555555555555", - "EnableAdaptiveSampling": false, "EnableQuickPulseMetricStream": false } } diff --git a/NETCORE/test/IntegrationTests.Tests/content/config-all-settings-true.json b/NETCORE/test/IntegrationTests.Tests/content/config-all-settings-true.json index 47ca8f1da..225bf5f26 100644 --- a/NETCORE/test/IntegrationTests.Tests/content/config-all-settings-true.json +++ b/NETCORE/test/IntegrationTests.Tests/content/config-all-settings-true.json @@ -1,7 +1,6 @@ { "ApplicationInsights": { "ConnectionString": "InstrumentationKey=22222222-2222-3333-4444-555555555555", - "EnableAdaptiveSampling": true, "EnableQuickPulseMetricStream": true, "ApplicationVersion": "1.0.0" } diff --git a/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Extensions/ApplicationInsightsExtensionsTests/AddApplicationInsightsTelemetryTests.cs b/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Extensions/ApplicationInsightsExtensionsTests/AddApplicationInsightsTelemetryTests.cs index 18ecc062b..0720e269d 100644 --- a/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Extensions/ApplicationInsightsExtensionsTests/AddApplicationInsightsTelemetryTests.cs +++ b/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Extensions/ApplicationInsightsExtensionsTests/AddApplicationInsightsTelemetryTests.cs @@ -595,7 +595,6 @@ public static void AppApplicationInsightsTelemetryFromApplicationInsightsService { ApplicationVersion = "test", DeveloperMode = true, - EnableAdaptiveSampling = false, EnableAuthenticationTrackingJavaScript = false, EnableDebugLogger = true, EnableQuickPulseMetricStream = false, @@ -1062,71 +1061,6 @@ public static void ConfigureApplicationInsightsTelemetryModuleDoesNotThrowIfModu Assert.Null(testTelemetryModule); } - [Fact] - public static void AddsAddaptiveSamplingServiceToTheConfigurationByDefault() - { - var services = CreateServicesAndAddApplicationinsightsTelemetry(null, "http://localhost:1234/v2/track/", null, false); - IServiceProvider serviceProvider = services.BuildServiceProvider(); - var telemetryConfiguration = serviceProvider.GetTelemetryConfiguration(); - var adaptiveSamplingProcessorCount = GetTelemetryProcessorsCountInConfigurationDefaultSink(telemetryConfiguration); - - // There will be 2 separate SamplingTelemetryProcessors - one for Events, and other for everything else. - Assert.Equal(2, adaptiveSamplingProcessorCount); - } - - /// - /// User could enable or disable sampling by setting EnableAdaptiveSampling. - /// This configuration can be read from a JSON file by the configuration factory or through code by passing ApplicationInsightsServiceOptions. - /// - /// - /// DefaultConfiguration - calls services.AddApplicationInsightsTelemetry() which reads IConfiguration from user application automatically. - /// SuppliedConfiguration - invokes services.AddApplicationInsightsTelemetry(configuration) where IConfiguration object is supplied by caller. - /// Code - Caller creates an instance of ApplicationInsightsServiceOptions and passes it. This option overrides all configuration being used in JSON file. - /// There is a special case where NULL values in these properties - InstrumentationKey, ConnectionString, EndpointAddress and DeveloperMode are overwritten. We check IConfiguration object to see if these properties have values, if values are present then we override it. - /// - /// Sets the value for property EnableAdaptiveSampling. - [Theory] - [InlineData("DefaultConfiguration", true)] - [InlineData("DefaultConfiguration", false)] - [InlineData("SuppliedConfiguration", true)] - [InlineData("SuppliedConfiguration", false)] - [InlineData("Code", true)] - [InlineData("Code", false)] - public static void DoesNotAddSamplingToConfigurationIfExplicitlyControlledThroughParameter(string configType, bool isEnable) - { - // ARRANGE - Action serviceOptions = null; - var filePath = Path.Combine("content", "config-all-settings-" + isEnable.ToString().ToLower() + ".json"); - - if (configType == "Code") - { - serviceOptions = o => { o.EnableAdaptiveSampling = isEnable; }; - filePath = null; - } - - // ACT - var services = CreateServicesAndAddApplicationinsightsTelemetry(filePath, null, serviceOptions, true, configType == "DefaultConfiguration" ? true : false); - - // VALIDATE - IServiceProvider serviceProvider = services.BuildServiceProvider(); - var telemetryConfiguration = serviceProvider.GetTelemetryConfiguration(); - var qpProcessorCount = GetTelemetryProcessorsCountInConfigurationDefaultSink(telemetryConfiguration); - // There will be 2 separate SamplingTelemetryProcessors - one for Events, and other for everything else. - Assert.Equal(isEnable ? 2 : 0, qpProcessorCount); - } - - [Fact] - public static void AddsAddaptiveSamplingServiceToTheConfigurationWithServiceOptions() - { - Action serviceOptions = options => options.EnableAdaptiveSampling = true; - var services = CreateServicesAndAddApplicationinsightsTelemetry(null, "http://localhost:1234/v2/track/", serviceOptions, false); - IServiceProvider serviceProvider = services.BuildServiceProvider(); - var telemetryConfiguration = serviceProvider.GetTelemetryConfiguration(); - var adaptiveSamplingProcessorCount = GetTelemetryProcessorsCountInConfigurationDefaultSink(telemetryConfiguration); - // There will be 2 separate SamplingTelemetryProcessors - one for Events, and other for everything else. - Assert.Equal(2, adaptiveSamplingProcessorCount); - } - [Fact] public static void AddsServerTelemetryChannelByDefault() { @@ -1257,9 +1191,6 @@ public static void ValidatesThatOnlyPassThroughProcessorIsAddedToCommonPipeline( var metricExtractorProcessorCount = GetTelemetryProcessorsCountInConfiguration(telemetryConfiguration); Assert.Equal(0, metricExtractorProcessorCount); - var samplingProcessorCount = GetTelemetryProcessorsCountInConfiguration(telemetryConfiguration); - Assert.Equal(0, samplingProcessorCount); - var passThroughProcessorCount = telemetryConfiguration.TelemetryProcessors.Count; Assert.Equal(1, passThroughProcessorCount); diff --git a/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/content/config-all-default.json b/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/content/config-all-default.json index 5d8346e32..1309ab919 100644 --- a/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/content/config-all-default.json +++ b/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/content/config-all-default.json @@ -2,7 +2,6 @@ "ApplicationInsights": { "InstrumentationKey": "11111111-2222-3333-4444-555555555555", "ConnectionString": "InstrumentationKey=11111111-2222-3333-4444-555555555555;IngestionEndpoint=http://testendpoint", - "EnableAdaptiveSampling": false, "EnablePerformanceCounterCollectionModule": true, "EnableAzureInstanceMetadataTelemetryModule": true, "EnableRequestTrackingTelemetryModule": true, diff --git a/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/content/config-all-settings-false.json b/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/content/config-all-settings-false.json index 705591032..c0716fd14 100644 --- a/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/content/config-all-settings-false.json +++ b/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/content/config-all-settings-false.json @@ -2,7 +2,6 @@ "ApplicationInsights": { "InstrumentationKey": "22222222-2222-3333-4444-555555555555", "ConnectionString": "InstrumentationKey=22222222-2222-3333-4444-555555555555;IngestionEndpoint=http://testendpoint", - "EnableAdaptiveSampling": false, "EnablePerformanceCounterCollectionModule": false, "EnableAzureInstanceMetadataTelemetryModule": false, "EnableRequestTrackingTelemetryModule": false, diff --git a/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/content/config-all-settings-true.json b/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/content/config-all-settings-true.json index 69672bbc9..4b20494b8 100644 --- a/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/content/config-all-settings-true.json +++ b/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/content/config-all-settings-true.json @@ -2,7 +2,6 @@ "ApplicationInsights": { "InstrumentationKey": "22222222-2222-3333-4444-555555555555", "ConnectionString": "InstrumentationKey=22222222-2222-3333-4444-555555555555;IngestionEndpoint=http://testendpoint", - "EnableAdaptiveSampling": true, "EnablePerformanceCounterCollectionModule": true, "EnableAzureInstanceMetadataTelemetryModule": true, "EnableRequestTrackingTelemetryModule": true, diff --git a/NETCORE/test/Microsoft.ApplicationInsights.WorkerService.Tests/ExtensionsTest.cs b/NETCORE/test/Microsoft.ApplicationInsights.WorkerService.Tests/ExtensionsTest.cs index 8bca10679..3930669f2 100644 --- a/NETCORE/test/Microsoft.ApplicationInsights.WorkerService.Tests/ExtensionsTest.cs +++ b/NETCORE/test/Microsoft.ApplicationInsights.WorkerService.Tests/ExtensionsTest.cs @@ -404,7 +404,6 @@ public void VerifyAddAIWorkerServiceSetsUpDefaultConfigurationAndModules() // TelemetryProcessors Assert.Contains(telemetryConfiguration.DefaultTelemetrySink.TelemetryProcessors, proc => proc.GetType().Name.Contains("AutocollectedMetricsExtractor")); - Assert.Contains(telemetryConfiguration.DefaultTelemetrySink.TelemetryProcessors, proc => proc.GetType().Name.Contains("AdaptiveSamplingTelemetryProcessor")); Assert.Contains(telemetryConfiguration.DefaultTelemetrySink.TelemetryProcessors, proc => proc.GetType().Name.Contains("QuickPulseTelemetryProcessor")); // TelemetryInitializers @@ -483,7 +482,6 @@ public void VerifyAddAIWorkerServiceRespectsAIOptions() // ACT var aiOptions = new ApplicationInsightsServiceOptions(); aiOptions.AddAutoCollectedMetricExtractor = false; - aiOptions.EnableAdaptiveSampling = false; aiOptions.EnableQuickPulseMetricStream = false; aiOptions.InstrumentationKey = "keyfromaioption"; services.AddApplicationInsightsTelemetryWorkerService(aiOptions); @@ -493,7 +491,6 @@ public void VerifyAddAIWorkerServiceRespectsAIOptions() var telemetryConfiguration = serviceProvider.GetRequiredService(); Assert.Equal("keyfromaioption", telemetryConfiguration.InstrumentationKey); Assert.DoesNotContain(telemetryConfiguration.DefaultTelemetrySink.TelemetryProcessors, proc => proc.GetType().Name.Contains("AutocollectedMetricsExtractor")); - Assert.DoesNotContain(telemetryConfiguration.DefaultTelemetrySink.TelemetryProcessors, proc => proc.GetType().Name.Contains("AdaptiveSamplingTelemetryProcessor")); Assert.DoesNotContain(telemetryConfiguration.DefaultTelemetrySink.TelemetryProcessors, proc => proc.GetType().Name.Contains("QuickPulseTelemetryProcessor")); } @@ -719,47 +716,6 @@ public static void UserCanEnableAndDisableHeartbeatFeature(string configType, bo Assert.Equal(isEnable, module.IsHeartbeatEnabled); } - /// - /// User could enable or disable sampling by setting EnableAdaptiveSampling. - /// This configuration can be read from a JSON file by the configuration factory or through code by passing ApplicationInsightsServiceOptions. - /// - /// - /// DefaultConfiguration - calls services.AddApplicationInsightsTelemetryWorkerService() which reads IConfiguration from user application automatically. - /// SuppliedConfiguration - invokes services.AddApplicationInsightsTelemetryWorkerService(configuration) where IConfiguration object is supplied by caller. - /// Code - Caller creates an instance of ApplicationInsightsServiceOptions and passes it. This option overrides all configuration being used in JSON file. - /// There is a special case where NULL values in these properties - InstrumentationKey, ConnectionString, EndpointAddress and DeveloperMode are overwritten. We check IConfiguration object to see if these properties have values, if values are present then we override it. - /// - /// Sets the value for property EnableAdaptiveSampling. - [Theory] - [InlineData("DefaultConfiguration", true)] - [InlineData("DefaultConfiguration", false)] - [InlineData("SuppliedConfiguration", true)] - [InlineData("SuppliedConfiguration", false)] - [InlineData("Code", true)] - [InlineData("Code", false)] - public static void DoesNotAddSamplingToConfigurationIfExplicitlyControlledThroughParameter(string configType, bool isEnable) - { - // ARRANGE - Action serviceOptions = null; - var filePath = Path.Combine("content", "config-all-settings-" + isEnable.ToString().ToLower() + ".json"); - - if (configType == "Code") - { - serviceOptions = o => { o.EnableAdaptiveSampling = isEnable; }; - filePath = null; - } - - // ACT - var services = CreateServicesAndAddApplicationinsightsWorker(filePath, serviceOptions, configType == "DefaultConfiguration" ? true : false); - - // VALIDATE - IServiceProvider serviceProvider = services.BuildServiceProvider(); - var telemetryConfiguration = serviceProvider.GetRequiredService>().Value; - var qpProcessorCount = GetTelemetryProcessorsCountInConfigurationDefaultSink(telemetryConfiguration); - // There will be 2 separate SamplingTelemetryProcessors - one for Events, and other for everything else. - Assert.Equal(isEnable ? 2 : 0, qpProcessorCount); - } - /// /// User could enable or disable auto collected metrics by setting AddAutoCollectedMetricExtractor. /// This configuration can be read from a JSON file by the configuration factory or through code by passing ApplicationInsightsServiceOptions. diff --git a/NETCORE/test/Microsoft.ApplicationInsights.WorkerService.Tests/content/config-all-default.json b/NETCORE/test/Microsoft.ApplicationInsights.WorkerService.Tests/content/config-all-default.json index 5d8346e32..1309ab919 100644 --- a/NETCORE/test/Microsoft.ApplicationInsights.WorkerService.Tests/content/config-all-default.json +++ b/NETCORE/test/Microsoft.ApplicationInsights.WorkerService.Tests/content/config-all-default.json @@ -2,7 +2,6 @@ "ApplicationInsights": { "InstrumentationKey": "11111111-2222-3333-4444-555555555555", "ConnectionString": "InstrumentationKey=11111111-2222-3333-4444-555555555555;IngestionEndpoint=http://testendpoint", - "EnableAdaptiveSampling": false, "EnablePerformanceCounterCollectionModule": true, "EnableAzureInstanceMetadataTelemetryModule": true, "EnableRequestTrackingTelemetryModule": true, diff --git a/NETCORE/test/Microsoft.ApplicationInsights.WorkerService.Tests/content/config-all-settings-false.json b/NETCORE/test/Microsoft.ApplicationInsights.WorkerService.Tests/content/config-all-settings-false.json index 705591032..c0716fd14 100644 --- a/NETCORE/test/Microsoft.ApplicationInsights.WorkerService.Tests/content/config-all-settings-false.json +++ b/NETCORE/test/Microsoft.ApplicationInsights.WorkerService.Tests/content/config-all-settings-false.json @@ -2,7 +2,6 @@ "ApplicationInsights": { "InstrumentationKey": "22222222-2222-3333-4444-555555555555", "ConnectionString": "InstrumentationKey=22222222-2222-3333-4444-555555555555;IngestionEndpoint=http://testendpoint", - "EnableAdaptiveSampling": false, "EnablePerformanceCounterCollectionModule": false, "EnableAzureInstanceMetadataTelemetryModule": false, "EnableRequestTrackingTelemetryModule": false, diff --git a/NETCORE/test/Microsoft.ApplicationInsights.WorkerService.Tests/content/config-all-settings-true.json b/NETCORE/test/Microsoft.ApplicationInsights.WorkerService.Tests/content/config-all-settings-true.json index 69672bbc9..4b20494b8 100644 --- a/NETCORE/test/Microsoft.ApplicationInsights.WorkerService.Tests/content/config-all-settings-true.json +++ b/NETCORE/test/Microsoft.ApplicationInsights.WorkerService.Tests/content/config-all-settings-true.json @@ -2,7 +2,6 @@ "ApplicationInsights": { "InstrumentationKey": "22222222-2222-3333-4444-555555555555", "ConnectionString": "InstrumentationKey=22222222-2222-3333-4444-555555555555;IngestionEndpoint=http://testendpoint", - "EnableAdaptiveSampling": true, "EnablePerformanceCounterCollectionModule": true, "EnableAzureInstanceMetadataTelemetryModule": true, "EnableRequestTrackingTelemetryModule": true, diff --git a/NETCORE/test/Shared/ConfigurationTests.cs b/NETCORE/test/Shared/ConfigurationTests.cs index 9833139b8..5681cdff7 100644 --- a/NETCORE/test/Shared/ConfigurationTests.cs +++ b/NETCORE/test/Shared/ConfigurationTests.cs @@ -14,6 +14,7 @@ namespace Microsoft.ApplicationInsights.AspNetCore.Tests #else namespace Microsoft.ApplicationInsights.WorkerService.Tests { + using System.Reflection; using Microsoft.ApplicationInsights.WorkerService; #endif @@ -51,66 +52,6 @@ public void ReadsConnectionStringFromApplicationInsightsSectionInConfig() Assert.Equal(TestConnectionString, options.ConnectionString); } - [Fact] - public void ReadsEnableAdaptiveSamplingFromApplicationInsightsSectionInConfig() - { - // ARRANGE - var jsonFullPath = Path.Combine(Directory.GetCurrentDirectory(), "content", "config-all-settings-false.json"); - this.output.WriteLine("json:" + jsonFullPath); - var config = new ConfigurationBuilder().AddJsonFile(jsonFullPath).Build(); - - var services = new ServiceCollection(); - services.AddSingleton(config); - - // ACT -#if AI_ASPNETCORE_WEB - services.AddApplicationInsightsTelemetry(); -#else - services.AddApplicationInsightsTelemetryWorkerService(); -#endif - - // VALIDATE - IServiceProvider serviceProvider = services.BuildServiceProvider(); - var options = serviceProvider.GetRequiredService>().Value; - Assert.False(options.EnableAdaptiveSampling); - - // Verify underlying AzureMonitorExporterOptions - var exporterOptions = serviceProvider.GetRequiredService>().Value; - Assert.Equal(1.0F, exporterOptions.SamplingRatio); - Assert.Null(exporterOptions.TracesPerSecond); - } - - [Fact] - public void DefaultEnableAdaptiveSampling_ExporterHasDefaultSamplingValues() - { - // ARRANGE - use config that doesn't specify EnableAdaptiveSampling - var jsonFullPath = Path.Combine(Directory.GetCurrentDirectory(), "content", "config-connection-string.json"); - this.output.WriteLine("json:" + jsonFullPath); - var config = new ConfigurationBuilder().AddJsonFile(jsonFullPath).Build(); - - var services = new ServiceCollection(); - services.AddSingleton(config); - - // ACT - #if AI_ASPNETCORE_WEB - services.AddApplicationInsightsTelemetry(); - #else - services.AddApplicationInsightsTelemetryWorkerService(); - #endif - - // VALIDATE - IServiceProvider serviceProvider = services.BuildServiceProvider(); - - // Verify ApplicationInsightsServiceOptions has default EnableAdaptiveSampling = true - var options = serviceProvider.GetRequiredService>().Value; - Assert.True(options.EnableAdaptiveSampling); - - // Verify underlying AzureMonitorExporterOptions for adaptive sampling - var exporterOptions = serviceProvider.GetRequiredService>().Value; - Assert.Equal(1.0F, exporterOptions.SamplingRatio); - Assert.Equal(5.0, exporterOptions.TracesPerSecond); - } - [Fact] public void ReadsEnableQuickPulseMetricStreamFromApplicationInsightsSectionInConfig() { @@ -183,13 +124,13 @@ public void ConfigurationFlowsFromApplicationInsightsSectionToAzureMonitorExport // Verify ApplicationInsightsServiceOptions var aiOptions = serviceProvider.GetRequiredService>().Value; Assert.Equal("InstrumentationKey=22222222-2222-3333-4444-555555555555", aiOptions.ConnectionString); - Assert.False(aiOptions.EnableAdaptiveSampling); Assert.False(aiOptions.EnableQuickPulseMetricStream); // Verify AzureMonitorExporterOptions gets the values var exporterOptions = serviceProvider.GetRequiredService>().Value; Assert.Equal("InstrumentationKey=22222222-2222-3333-4444-555555555555", exporterOptions.ConnectionString); - Assert.Equal(1.0F, exporterOptions.SamplingRatio); // No sampling when EnableAdaptiveSampling is false + Assert.Equal(1.0F, exporterOptions.SamplingRatio); + Assert.Equal(5, exporterOptions.TracesPerSecond); Assert.False(exporterOptions.EnableLiveMetrics); } diff --git a/NETCORE/test/WorkerIntegrationTests.Tests/WorkerHostFixture.cs b/NETCORE/test/WorkerIntegrationTests.Tests/WorkerHostFixture.cs index bb0360453..63b546a08 100644 --- a/NETCORE/test/WorkerIntegrationTests.Tests/WorkerHostFixture.cs +++ b/NETCORE/test/WorkerIntegrationTests.Tests/WorkerHostFixture.cs @@ -57,7 +57,6 @@ public async Task InitializeAsync() services.Configure(options => { options.ConnectionString = TestConnectionString; - options.EnableAdaptiveSampling = false; options.EnableQuickPulseMetricStream = false; options.AddAutoCollectedMetricExtractor = false; }); From b7f2337290332c6f01eccd1fc020baf9b2b5c62f Mon Sep 17 00:00:00 2001 From: Harsimar Kaur Date: Mon, 26 Jan 2026 09:52:10 -0800 Subject: [PATCH 2/6] add sampling to service options, fix tests --- .../Stable/PublicAPI.Unshipped.txt | 4 + .../Stable/PublicAPI.Unshipped.txt | 4 + .../ApplicationInsightsExtensions.cs | 16 +++ .../ApplicationInsightsExtensions.cs | 14 +++ .../ApplicationInsightsServiceOptions.cs | 13 +++ .../CustomWebApplicationFactory.cs | 1 + .../config-sampling-tracespersecond.json | 6 + .../content/config-samplingratio.json | 6 + NETCORE/test/Shared/ConfigurationTests.cs | 110 ++++++++++++++++++ .../WorkerHostFixture.cs | 1 + 10 files changed, 175 insertions(+) create mode 100644 NETCORE/test/IntegrationTests.Tests/content/config-sampling-tracespersecond.json create mode 100644 NETCORE/test/IntegrationTests.Tests/content/config-samplingratio.json diff --git a/.publicApi/Microsoft.ApplicationInsights.AspNetCore.dll/Stable/PublicAPI.Unshipped.txt b/.publicApi/Microsoft.ApplicationInsights.AspNetCore.dll/Stable/PublicAPI.Unshipped.txt index 612c6c31c..ff8a867fb 100644 --- a/.publicApi/Microsoft.ApplicationInsights.AspNetCore.dll/Stable/PublicAPI.Unshipped.txt +++ b/.publicApi/Microsoft.ApplicationInsights.AspNetCore.dll/Stable/PublicAPI.Unshipped.txt @@ -16,6 +16,10 @@ Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOp Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.EnablePerformanceCounterCollectionModule.set -> void Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.EnableQuickPulseMetricStream.get -> bool Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.EnableQuickPulseMetricStream.set -> void +Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.SamplingRatio.get -> float? +Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.SamplingRatio.set -> void +Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.TracesPerSecond.get -> double? +Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.TracesPerSecond.set -> void Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.EnableRequestTrackingTelemetryModule.get -> bool Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.EnableRequestTrackingTelemetryModule.set -> void Microsoft.ApplicationInsights.AspNetCore.IJavaScriptSnippet diff --git a/.publicApi/Microsoft.ApplicationInsights.WorkerService.dll/Stable/PublicAPI.Unshipped.txt b/.publicApi/Microsoft.ApplicationInsights.WorkerService.dll/Stable/PublicAPI.Unshipped.txt index e7852d609..6321c5830 100644 --- a/.publicApi/Microsoft.ApplicationInsights.WorkerService.dll/Stable/PublicAPI.Unshipped.txt +++ b/.publicApi/Microsoft.ApplicationInsights.WorkerService.dll/Stable/PublicAPI.Unshipped.txt @@ -14,6 +14,10 @@ Microsoft.ApplicationInsights.WorkerService.ApplicationInsightsServiceOptions.En Microsoft.ApplicationInsights.WorkerService.ApplicationInsightsServiceOptions.EnablePerformanceCounterCollectionModule.set -> void Microsoft.ApplicationInsights.WorkerService.ApplicationInsightsServiceOptions.EnableQuickPulseMetricStream.get -> bool Microsoft.ApplicationInsights.WorkerService.ApplicationInsightsServiceOptions.EnableQuickPulseMetricStream.set -> void +Microsoft.ApplicationInsights.WorkerService.ApplicationInsightsServiceOptions.SamplingRatio.get -> float? +Microsoft.ApplicationInsights.WorkerService.ApplicationInsightsServiceOptions.SamplingRatio.set -> void +Microsoft.ApplicationInsights.WorkerService.ApplicationInsightsServiceOptions.TracesPerSecond.get -> double? +Microsoft.ApplicationInsights.WorkerService.ApplicationInsightsServiceOptions.TracesPerSecond.set -> void Microsoft.Extensions.DependencyInjection.ApplicationInsightsExtensions static Microsoft.Extensions.DependencyInjection.ApplicationInsightsExtensions.AddApplicationInsightsTelemetryWorkerService(this Microsoft.Extensions.DependencyInjection.IServiceCollection services) -> Microsoft.Extensions.DependencyInjection.IServiceCollection static Microsoft.Extensions.DependencyInjection.ApplicationInsightsExtensions.AddApplicationInsightsTelemetryWorkerService(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, Microsoft.ApplicationInsights.WorkerService.ApplicationInsightsServiceOptions options) -> Microsoft.Extensions.DependencyInjection.IServiceCollection diff --git a/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs b/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs index 6dd9d2de3..3988da8d9 100644 --- a/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs +++ b/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs @@ -213,6 +213,22 @@ internal static IOpenTelemetryBuilder UseApplicationInsightsTelemetry(this IOpen exporterOptions.EnableLiveMetrics = false; } + if (serviceOptions.TracesPerSecond.HasValue && serviceOptions.TracesPerSecond.Value > 0) + { + exporterOptions.TracesPerSecond = serviceOptions.TracesPerSecond.Value; + } + + if (serviceOptions.SamplingRatio.HasValue && serviceOptions.SamplingRatio.Value >= 0.0f && serviceOptions.SamplingRatio.Value <= 1.0f) + { + exporterOptions.SamplingRatio = serviceOptions.SamplingRatio.Value; + if (!serviceOptions.TracesPerSecond.HasValue) + { + exporterOptions.TracesPerSecond = null; + } + } + + + // Configure standard metrics and performance counter collection using reflection // Only set when false since the default is true if (!serviceOptions.AddAutoCollectedMetricExtractor) diff --git a/NETCORE/src/Microsoft.ApplicationInsights.WorkerService/ApplicationInsightsExtensions.cs b/NETCORE/src/Microsoft.ApplicationInsights.WorkerService/ApplicationInsightsExtensions.cs index c8c457182..79465ac21 100644 --- a/NETCORE/src/Microsoft.ApplicationInsights.WorkerService/ApplicationInsightsExtensions.cs +++ b/NETCORE/src/Microsoft.ApplicationInsights.WorkerService/ApplicationInsightsExtensions.cs @@ -201,6 +201,20 @@ internal static IOpenTelemetryBuilder UseApplicationInsightsTelemetry(this IOpen exporterOptions.EnableLiveMetrics = serviceOptions.EnableQuickPulseMetricStream; + if (serviceOptions.TracesPerSecond.HasValue && serviceOptions.TracesPerSecond.Value > 0) + { + exporterOptions.TracesPerSecond = serviceOptions.TracesPerSecond.Value; + } + + if (serviceOptions.SamplingRatio.HasValue && serviceOptions.SamplingRatio.Value >= 0.0f && serviceOptions.SamplingRatio.Value <= 1.0f) + { + exporterOptions.SamplingRatio = serviceOptions.SamplingRatio.Value; + if (!serviceOptions.TracesPerSecond.HasValue) + { + exporterOptions.TracesPerSecond = null; + } + } + // Configure standard metrics and performance counter collection using reflection // Only set when false since the default is true if (!serviceOptions.AddAutoCollectedMetricExtractor) diff --git a/NETCORE/src/Shared/Extensions/ApplicationInsightsServiceOptions.cs b/NETCORE/src/Shared/Extensions/ApplicationInsightsServiceOptions.cs index df2827336..fdc3e7814 100644 --- a/NETCORE/src/Shared/Extensions/ApplicationInsightsServiceOptions.cs +++ b/NETCORE/src/Shared/Extensions/ApplicationInsightsServiceOptions.cs @@ -53,6 +53,17 @@ public class ApplicationInsightsServiceOptions /// public bool AddAutoCollectedMetricExtractor { get; set; } = true; + /// + /// Gets or sets the target number of traces per second to be collected. + /// + public double? TracesPerSecond { get; set; } + + /// + /// Gets or sets the sampling ratio for telemetry. + /// Value must be between 0.0 and 1.0, where 1.0 means all telemetry is collected (no sampling). + /// + public float? SamplingRatio { get; set; } + #if AI_ASPNETCORE_WEB /// /// Gets or sets a value indicating whether RequestTrackingTelemetryModule should be enabled. @@ -88,6 +99,8 @@ internal void CopyPropertiesTo(ApplicationInsightsServiceOptions target) target.AddAutoCollectedMetricExtractor = this.AddAutoCollectedMetricExtractor; target.EnablePerformanceCounterCollectionModule = this.EnablePerformanceCounterCollectionModule; target.EnableDependencyTrackingTelemetryModule = this.EnableDependencyTrackingTelemetryModule; + target.TracesPerSecond = this.TracesPerSecond; + target.SamplingRatio = this.SamplingRatio; #if AI_ASPNETCORE_WEB target.EnableAuthenticationTrackingJavaScript = this.EnableAuthenticationTrackingJavaScript; diff --git a/NETCORE/test/IntegrationTests.Tests/CustomWebApplicationFactory.cs b/NETCORE/test/IntegrationTests.Tests/CustomWebApplicationFactory.cs index 5f673d8b5..d860ea834 100644 --- a/NETCORE/test/IntegrationTests.Tests/CustomWebApplicationFactory.cs +++ b/NETCORE/test/IntegrationTests.Tests/CustomWebApplicationFactory.cs @@ -44,6 +44,7 @@ protected override void ConfigureWebHost(IWebHostBuilder builder) options.AddAutoCollectedMetricExtractor = false; options.EnableQuickPulseMetricStream = false; options.ConnectionString = "InstrumentationKey=ikey"; + options.SamplingRatio = 1.0f; // 100% telemetry flow in tests }); }); } diff --git a/NETCORE/test/IntegrationTests.Tests/content/config-sampling-tracespersecond.json b/NETCORE/test/IntegrationTests.Tests/content/config-sampling-tracespersecond.json new file mode 100644 index 000000000..0d46df2e4 --- /dev/null +++ b/NETCORE/test/IntegrationTests.Tests/content/config-sampling-tracespersecond.json @@ -0,0 +1,6 @@ +{ + "ApplicationInsights": { + "ConnectionString": "InstrumentationKey=33333333-4444-5555-6666-777777777777", + "TracesPerSecond": 10.0 + } +} diff --git a/NETCORE/test/IntegrationTests.Tests/content/config-samplingratio.json b/NETCORE/test/IntegrationTests.Tests/content/config-samplingratio.json new file mode 100644 index 000000000..70f4fee1e --- /dev/null +++ b/NETCORE/test/IntegrationTests.Tests/content/config-samplingratio.json @@ -0,0 +1,6 @@ +{ + "ApplicationInsights": { + "ConnectionString": "InstrumentationKey=44444444-5555-6666-7777-888888888888", + "SamplingRatio": 0.5 + } +} diff --git a/NETCORE/test/Shared/ConfigurationTests.cs b/NETCORE/test/Shared/ConfigurationTests.cs index 5681cdff7..6e936be98 100644 --- a/NETCORE/test/Shared/ConfigurationTests.cs +++ b/NETCORE/test/Shared/ConfigurationTests.cs @@ -134,6 +134,116 @@ public void ConfigurationFlowsFromApplicationInsightsSectionToAzureMonitorExport Assert.False(exporterOptions.EnableLiveMetrics); } + [Fact] + public void ReadsTracesPerSecondFromApplicationInsightsSectionInConfig() + { + // ARRANGE + var jsonFullPath = Path.Combine(Directory.GetCurrentDirectory(), "content", "config-sampling-tracespersecond.json"); + this.output.WriteLine("json:" + jsonFullPath); + var config = new ConfigurationBuilder().AddJsonFile(jsonFullPath).Build(); + + var services = new ServiceCollection(); + services.AddSingleton(config); + + // ACT +#if AI_ASPNETCORE_WEB + services.AddApplicationInsightsTelemetry(); +#else + services.AddApplicationInsightsTelemetryWorkerService(); +#endif + + // VALIDATE + IServiceProvider serviceProvider = services.BuildServiceProvider(); + var options = serviceProvider.GetRequiredService>().Value; + Assert.Equal(10.0, options.TracesPerSecond); + + // Verify it flows to exporter options + var exporterOptions = serviceProvider.GetRequiredService>().Value; + Assert.Equal(10.0, exporterOptions.TracesPerSecond); + } + + [Fact] + public void ReadsSamplingRatioFromApplicationInsightsSectionInConfig() + { + // ARRANGE + var jsonFullPath = Path.Combine(Directory.GetCurrentDirectory(), "content", "config-samplingratio.json"); + this.output.WriteLine("json:" + jsonFullPath); + var config = new ConfigurationBuilder().AddJsonFile(jsonFullPath).Build(); + + var services = new ServiceCollection(); + services.AddSingleton(config); + + // ACT +#if AI_ASPNETCORE_WEB + services.AddApplicationInsightsTelemetry(); +#else + services.AddApplicationInsightsTelemetryWorkerService(); +#endif + + // VALIDATE + IServiceProvider serviceProvider = services.BuildServiceProvider(); + var options = serviceProvider.GetRequiredService>().Value; + Assert.Equal(0.5f, options.SamplingRatio); + + // Verify it flows to exporter options + var exporterOptions = serviceProvider.GetRequiredService>().Value; + Assert.Equal(0.5f, exporterOptions.SamplingRatio); + } + + [Fact] + public void TracesPerSecondIgnoresNonPositiveValues() + { + // ARRANGE + var services = new ServiceCollection(); + var config = new ConfigurationBuilder().AddInMemoryCollection().Build(); + services.AddSingleton(config); + + // ACT +#if AI_ASPNETCORE_WEB + services.AddApplicationInsightsTelemetry(options => +#else + services.AddApplicationInsightsTelemetryWorkerService(options => +#endif + { + options.ConnectionString = "InstrumentationKey=11111111-2222-3333-4444-555555555555"; + options.TracesPerSecond = -1.0; // Invalid value + }); + + // VALIDATE + IServiceProvider serviceProvider = services.BuildServiceProvider(); + var exporterOptions = serviceProvider.GetRequiredService>().Value; + + // TracesPerSecond should not be set to a negative value - it should remain at default + Assert.Equal(5.0, exporterOptions.TracesPerSecond); + } + + [Fact] + public void SamplingRatioIgnoresInvalidValues() + { + // ARRANGE + var services = new ServiceCollection(); + var config = new ConfigurationBuilder().AddInMemoryCollection().Build(); + services.AddSingleton(config); + + // ACT +#if AI_ASPNETCORE_WEB + services.AddApplicationInsightsTelemetry(options => +#else + services.AddApplicationInsightsTelemetryWorkerService(options => +#endif + { + options.ConnectionString = "InstrumentationKey=11111111-2222-3333-4444-555555555555"; + options.SamplingRatio = 1.5f; // Invalid value (> 1.0) + }); + + // VALIDATE + IServiceProvider serviceProvider = services.BuildServiceProvider(); + var exporterOptions = serviceProvider.GetRequiredService>().Value; + + // SamplingRatio should not be set to an invalid value + Assert.NotEqual(1.5f, exporterOptions.SamplingRatio); + } + [Fact] public void EnvironmentVariablesTakePrecedenceOverAppSettings() { diff --git a/NETCORE/test/WorkerIntegrationTests.Tests/WorkerHostFixture.cs b/NETCORE/test/WorkerIntegrationTests.Tests/WorkerHostFixture.cs index 63b546a08..932a30ef3 100644 --- a/NETCORE/test/WorkerIntegrationTests.Tests/WorkerHostFixture.cs +++ b/NETCORE/test/WorkerIntegrationTests.Tests/WorkerHostFixture.cs @@ -59,6 +59,7 @@ public async Task InitializeAsync() options.ConnectionString = TestConnectionString; options.EnableQuickPulseMetricStream = false; options.AddAutoCollectedMetricExtractor = false; + options.SamplingRatio = 1.0f; // 100% telemetry flow in tests }); services.ConfigureOpenTelemetryTracerProvider(tracer => From 5515112353d83da9bf5d1cbdcc29c96bed243ad1 Mon Sep 17 00:00:00 2001 From: Harsimar Kaur Date: Mon, 26 Jan 2026 10:51:50 -0800 Subject: [PATCH 3/6] fix ci error --- .../Extensions/ApplicationInsightsExtensions.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs b/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs index 3988da8d9..fdc224856 100644 --- a/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs +++ b/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs @@ -227,8 +227,6 @@ internal static IOpenTelemetryBuilder UseApplicationInsightsTelemetry(this IOpen } } - - // Configure standard metrics and performance counter collection using reflection // Only set when false since the default is true if (!serviceOptions.AddAutoCollectedMetricExtractor) From 1473481c094e4d7706f622e591830449d53a9626 Mon Sep 17 00:00:00 2001 From: Harsimar Kaur Date: Mon, 26 Jan 2026 11:54:06 -0800 Subject: [PATCH 4/6] merge from master, change log --- BreakingChanges.md | 7 +++++++ CHANGELOG.md | 1 + 2 files changed, 8 insertions(+) diff --git a/BreakingChanges.md b/BreakingChanges.md index 350852ae7..1b12178d7 100644 --- a/BreakingChanges.md +++ b/BreakingChanges.md @@ -232,6 +232,8 @@ The following extension methods remain with identical signatures: ### New Properties Added in 3.x - ✅ **`Credential`** (Azure.Core.TokenCredential) - Enables Azure Active Directory (AAD) authentication +- ✅ **`TracesPerSecond`** (double?) - Gets or sets the number of traces per second for rate-limited sampling (default sampling mode). Replaces `EnableAdaptiveSampling`. +- ✅ **`SamplingRatio`** (float?) - Gets or sets the sampling ratio for traces (0.0 to 1.0). A value of 1.0 means all telemetry is sent. Replaces `EnableAdaptiveSampling`. ### JavaScriptSnippet Constructor Change **2.x:** @@ -294,6 +296,11 @@ The following extension methods remain with identical signatures: - ✅ **`EnableDebugLogger`** - Still configurable though has no effect - ✅ **`AddAutoCollectedMetricExtractor`** - Still configurable +### New Properties Added in 3.x +- ✅ **`Credential`** (Azure.Core.TokenCredential) - Enables Azure Active Directory (AAD) authentication +- ✅ **`TracesPerSecond`** (double?) - Gets or sets the number of traces per second for rate-limited sampling (default sampling mode). Replaces `EnableAdaptiveSampling`. +- ✅ **`SamplingRatio`** (float?) - Gets or sets the sampling ratio for traces (0.0 to 1.0). A value of 1.0 means all telemetry is sent. Replaces `EnableAdaptiveSampling`. + ## Migration Impact - Any code depending on `InstrumentationKey` must migrate to `ConnectionString` - Code checking or configuring the removed options (`DeveloperMode`, `EndpointAddress`, etc.) will break diff --git a/CHANGELOG.md b/CHANGELOG.md index 2bf75e1e4..87a98433c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ - OpenTelemetry.Instrumentation.SqlClient 1.15.0-rc.1 - OpenTelemetry.Resources.Azure 1.15.0-beta.1 - [Added support to disable telemetry via TelemetryConfiguration.DisableTelemetry](https://github.com/microsoft/ApplicationInsights-dotnet/pull/3084) +- [Removed EnabledAdaptiveSampling and replace with TracesPerSecond and SamplingRatio](https://github.com/microsoft/ApplicationInsights-dotnet/pull/3085) ## Version 3.0.0-beta2 ### Added From 8e24b1c03cbbb5eec44b906033914b2ae3e4d093 Mon Sep 17 00:00:00 2001 From: Harsimar Kaur Date: Mon, 26 Jan 2026 13:39:40 -0800 Subject: [PATCH 5/6] pr comments --- CHANGELOG.md | 2 +- .../ApplicationInsightsExtensions.cs | 26 ++++++++++++++----- .../ApplicationInsightsExtensions.cs | 26 ++++++++++++++----- 3 files changed, 41 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 87a98433c..1bfb00185 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,7 +12,7 @@ - OpenTelemetry.Instrumentation.SqlClient 1.15.0-rc.1 - OpenTelemetry.Resources.Azure 1.15.0-beta.1 - [Added support to disable telemetry via TelemetryConfiguration.DisableTelemetry](https://github.com/microsoft/ApplicationInsights-dotnet/pull/3084) -- [Removed EnabledAdaptiveSampling and replace with TracesPerSecond and SamplingRatio](https://github.com/microsoft/ApplicationInsights-dotnet/pull/3085) +- [Removed `EnabledAdaptiveSampling` and replaced with `TracesPerSecond` and `SamplingRatio`](https://github.com/microsoft/ApplicationInsights-dotnet/pull/3085) ## Version 3.0.0-beta2 ### Added diff --git a/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs b/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs index 0063bd413..181130fe2 100644 --- a/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs +++ b/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs @@ -220,17 +220,31 @@ internal static IOpenTelemetryBuilder UseApplicationInsightsTelemetry(this IOpen exporterOptions.EnableLiveMetrics = false; } - if (serviceOptions.TracesPerSecond.HasValue && serviceOptions.TracesPerSecond.Value > 0) + if (serviceOptions.TracesPerSecond.HasValue) { - exporterOptions.TracesPerSecond = serviceOptions.TracesPerSecond.Value; + if (serviceOptions.TracesPerSecond.Value >= 0) + { + exporterOptions.TracesPerSecond = serviceOptions.TracesPerSecond.Value; + } + else + { + AspNetCoreEventSource.Instance.LogError($"Invalid TracesPerSecond value '{serviceOptions.TracesPerSecond.Value}'. Value must be at least 0. Using default value."); + } + } - if (serviceOptions.SamplingRatio.HasValue && serviceOptions.SamplingRatio.Value >= 0.0f && serviceOptions.SamplingRatio.Value <= 1.0f) + if (serviceOptions.SamplingRatio.HasValue) { - exporterOptions.SamplingRatio = serviceOptions.SamplingRatio.Value; - if (!serviceOptions.TracesPerSecond.HasValue) + if (serviceOptions.SamplingRatio.Value >= 0.0f && serviceOptions.SamplingRatio.Value <= 1.0f) { + exporterOptions.SamplingRatio = serviceOptions.SamplingRatio.Value; + if (!serviceOptions.TracesPerSecond.HasValue) + { + exporterOptions.TracesPerSecond = null; + } + } + else { - exporterOptions.TracesPerSecond = null; + AspNetCoreEventSource.Instance.LogError($"Invalid SamplingRatio value '{serviceOptions.SamplingRatio.Value}'. Value must be between 0.0 and 1.0. Using default value."); } } diff --git a/NETCORE/src/Microsoft.ApplicationInsights.WorkerService/ApplicationInsightsExtensions.cs b/NETCORE/src/Microsoft.ApplicationInsights.WorkerService/ApplicationInsightsExtensions.cs index a9e55d1cf..ad5d6a9b3 100644 --- a/NETCORE/src/Microsoft.ApplicationInsights.WorkerService/ApplicationInsightsExtensions.cs +++ b/NETCORE/src/Microsoft.ApplicationInsights.WorkerService/ApplicationInsightsExtensions.cs @@ -208,17 +208,31 @@ internal static IOpenTelemetryBuilder UseApplicationInsightsTelemetry(this IOpen exporterOptions.EnableLiveMetrics = serviceOptions.EnableQuickPulseMetricStream; - if (serviceOptions.TracesPerSecond.HasValue && serviceOptions.TracesPerSecond.Value > 0) + if (serviceOptions.TracesPerSecond.HasValue) { - exporterOptions.TracesPerSecond = serviceOptions.TracesPerSecond.Value; + if (serviceOptions.TracesPerSecond.Value >= 0) + { + exporterOptions.TracesPerSecond = serviceOptions.TracesPerSecond.Value; + } + else + { + WorkerServiceEventSource.Instance.LogError($"Invalid TracesPerSecond value '{serviceOptions.TracesPerSecond.Value}'. Value must be at least 0. Using default value."); + } + } - if (serviceOptions.SamplingRatio.HasValue && serviceOptions.SamplingRatio.Value >= 0.0f && serviceOptions.SamplingRatio.Value <= 1.0f) + if (serviceOptions.SamplingRatio.HasValue) { - exporterOptions.SamplingRatio = serviceOptions.SamplingRatio.Value; - if (!serviceOptions.TracesPerSecond.HasValue) + if (serviceOptions.SamplingRatio.Value >= 0.0f && serviceOptions.SamplingRatio.Value <= 1.0f) { + exporterOptions.SamplingRatio = serviceOptions.SamplingRatio.Value; + if (!serviceOptions.TracesPerSecond.HasValue) + { + exporterOptions.TracesPerSecond = null; + } + } + else { - exporterOptions.TracesPerSecond = null; + WorkerServiceEventSource.Instance.LogError($"Invalid SamplingRatio value '{serviceOptions.SamplingRatio.Value}'. Value must be between 0.0 and 1.0. Using default value."); } } From 8efaec20a148b5185545ef3f2fa7572b9b25cdd8 Mon Sep 17 00:00:00 2001 From: Harsimar Kaur Date: Mon, 26 Jan 2026 13:46:33 -0800 Subject: [PATCH 6/6] ci --- .../Extensions/ApplicationInsightsExtensions.cs | 4 ++-- .../ApplicationInsightsExtensions.cs | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs b/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs index 181130fe2..245ad4c9b 100644 --- a/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs +++ b/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs @@ -230,12 +230,12 @@ internal static IOpenTelemetryBuilder UseApplicationInsightsTelemetry(this IOpen { AspNetCoreEventSource.Instance.LogError($"Invalid TracesPerSecond value '{serviceOptions.TracesPerSecond.Value}'. Value must be at least 0. Using default value."); } - } if (serviceOptions.SamplingRatio.HasValue) { - if (serviceOptions.SamplingRatio.Value >= 0.0f && serviceOptions.SamplingRatio.Value <= 1.0f) { + if (serviceOptions.SamplingRatio.Value >= 0.0f && serviceOptions.SamplingRatio.Value <= 1.0f) + { exporterOptions.SamplingRatio = serviceOptions.SamplingRatio.Value; if (!serviceOptions.TracesPerSecond.HasValue) { diff --git a/NETCORE/src/Microsoft.ApplicationInsights.WorkerService/ApplicationInsightsExtensions.cs b/NETCORE/src/Microsoft.ApplicationInsights.WorkerService/ApplicationInsightsExtensions.cs index ad5d6a9b3..990f89847 100644 --- a/NETCORE/src/Microsoft.ApplicationInsights.WorkerService/ApplicationInsightsExtensions.cs +++ b/NETCORE/src/Microsoft.ApplicationInsights.WorkerService/ApplicationInsightsExtensions.cs @@ -217,13 +217,13 @@ internal static IOpenTelemetryBuilder UseApplicationInsightsTelemetry(this IOpen else { WorkerServiceEventSource.Instance.LogError($"Invalid TracesPerSecond value '{serviceOptions.TracesPerSecond.Value}'. Value must be at least 0. Using default value."); - } - + } } if (serviceOptions.SamplingRatio.HasValue) { - if (serviceOptions.SamplingRatio.Value >= 0.0f && serviceOptions.SamplingRatio.Value <= 1.0f) { + if (serviceOptions.SamplingRatio.Value >= 0.0f && serviceOptions.SamplingRatio.Value <= 1.0f) + { exporterOptions.SamplingRatio = serviceOptions.SamplingRatio.Value; if (!serviceOptions.TracesPerSecond.HasValue) {