From 6b70c45ce9aa26f7581f938fdf75f47a2fd3f110 Mon Sep 17 00:00:00 2001 From: Mikhail-Pranovich <35508138+Mikhail-Pranovich@users.noreply.github.com> Date: Wed, 9 Jan 2019 17:12:35 -0800 Subject: [PATCH] Initial sampling rate from applicationHost.config fix (#1048) --- CHANGELOG.md | 3 ++ .../AdaptiveSamplingTelemetryProcessorTest.cs | 42 ++++++++++++++++++- .../AdaptiveSamplingTelemetryProcessor.cs | 16 ++++++- ...ngPercentageEstimatorTelemetryProcessor.cs | 9 ++++ 4 files changed, 68 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 805f27acfe..21ec7a3be0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ This changelog will be used to generate documentation on [release notes page](http://azure.microsoft.com/documentation/articles/app-insights-release-notes-dotnet/). +## Version next: +- InitialSamplingRate is now correctly applied if set in applicationInsights.config (https://github.com/Microsoft/ApplicationInsights-dotnet/pull/1048) + ## Version 2.9.0-beta3 - [Flatten IExtension and Unknown ITelemetry implementations for Rich Payload Event Source consumption](https://github.com/Microsoft/ApplicationInsights-dotnet/pull/1017) - [Fix: Start/StopOperation with W3C distributed tracing enabled does not track telemetry](https://github.com/Microsoft/ApplicationInsights-dotnet/pull/1031) diff --git a/Test/ServerTelemetryChannel.Test/Shared.Tests/AdaptiveSamplingTelemetryProcessorTest.cs b/Test/ServerTelemetryChannel.Test/Shared.Tests/AdaptiveSamplingTelemetryProcessorTest.cs index 83cdd2a70b..d00282b430 100644 --- a/Test/ServerTelemetryChannel.Test/Shared.Tests/AdaptiveSamplingTelemetryProcessorTest.cs +++ b/Test/ServerTelemetryChannel.Test/Shared.Tests/AdaptiveSamplingTelemetryProcessorTest.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; + using System.Linq; using System.Reflection; using System.Threading; @@ -13,7 +14,8 @@ using Microsoft.ApplicationInsights.TestFramework; using Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel; using Microsoft.VisualStudio.TestTools.UnitTesting; - + using Moq; + [TestClass] public class AdaptiveSamplingTelemetryProcessorTest { @@ -295,6 +297,44 @@ public void AdaptiveSamplingSetsExcludedTypesOnInternalSamplingProcessor() Assert.AreEqual("request;", internalProcessor.ExcludedTypes); } + [TestMethod] + public void CurrentSamplingRateResetsOnInitialSamplingRateChange() + { + var nextMock = new Mock(); + var next = nextMock.Object; + var adaptiveSamplingProcessor = new AdaptiveSamplingTelemetryProcessor( + new Channel.Implementation.SamplingPercentageEstimatorSettings + { + InitialSamplingPercentage = 20, + }, + null, + next); + + Assert.AreEqual(20, adaptiveSamplingProcessor.InitialSamplingPercentage); + Assert.AreEqual(100 / 20, adaptiveSamplingProcessor.SamplingPercentageEstimatorTelemetryProcessor.CurrentSamplingRate); + + // change in InitialSamplingPercentage should change the CurrentSamplingPercentage: + adaptiveSamplingProcessor.InitialSamplingPercentage = 50; + Assert.AreEqual(50, adaptiveSamplingProcessor.InitialSamplingPercentage); + Assert.AreEqual(100 / 50, adaptiveSamplingProcessor.SamplingPercentageEstimatorTelemetryProcessor.CurrentSamplingRate); + } + + [TestMethod] + public void SettingsFromPassedInTelemetryProcessorsAreAppliedToSamplingTelemetryProcessor() + { + var nextMock = new Mock(); + var next = nextMock.Object; + var adaptiveSamplingProcessor = new AdaptiveSamplingTelemetryProcessor( + new Channel.Implementation.SamplingPercentageEstimatorSettings + { + InitialSamplingPercentage = 25, + }, + null, + next); + var percentageEstimatorProcessor = adaptiveSamplingProcessor.SamplingTelemetryProcessor; + Assert.AreEqual(25, percentageEstimatorProcessor.SamplingPercentage); + } + private void TraceSamplingPercentageEvaluation( double afterSamplingTelemetryItemRatePerSecond, double currentSamplingPercentage, diff --git a/src/ServerTelemetryChannel/AdaptiveSamplingTelemetryProcessor.cs b/src/ServerTelemetryChannel/AdaptiveSamplingTelemetryProcessor.cs index bf16e5610e..438ad69b93 100644 --- a/src/ServerTelemetryChannel/AdaptiveSamplingTelemetryProcessor.cs +++ b/src/ServerTelemetryChannel/AdaptiveSamplingTelemetryProcessor.cs @@ -57,7 +57,10 @@ public AdaptiveSamplingTelemetryProcessor( // make estimator telemetry processor work after sampling was done this.estimatorProcessor = new SamplingPercentageEstimatorTelemetryProcessor(settings, this.SamplingPercentageChanged, next); - this.samplingProcessor = new SamplingTelemetryProcessor(next, this.estimatorProcessor); + this.samplingProcessor = new SamplingTelemetryProcessor(next, this.estimatorProcessor) + { + SamplingPercentage = this.estimatorSettings.InitialSamplingPercentage + }; } /// @@ -99,6 +102,7 @@ public double InitialSamplingPercentage // note: 'initial' percentage will affect sampling even // if it was running for a while this.estimatorSettings.InitialSamplingPercentage = value; + this.estimatorProcessor.CurrentSamplingRate = this.estimatorSettings.EffectiveInitialSamplingRate; this.samplingProcessor.SamplingPercentage = value; } } @@ -221,6 +225,16 @@ public double MovingAverageRatio } } + /// + /// Gets sampling telemetry processor. + /// + internal SamplingTelemetryProcessor SamplingTelemetryProcessor => this.samplingProcessor; + + /// + /// Gets sampling percentage estimator telemetry processor. + /// + internal SamplingPercentageEstimatorTelemetryProcessor SamplingPercentageEstimatorTelemetryProcessor => this.estimatorProcessor; + /// /// Processes telemetry item. /// diff --git a/src/ServerTelemetryChannel/Implementation/SamplingPercentageEstimatorTelemetryProcessor.cs b/src/ServerTelemetryChannel/Implementation/SamplingPercentageEstimatorTelemetryProcessor.cs index 8853e7205c..8fbe23c672 100644 --- a/src/ServerTelemetryChannel/Implementation/SamplingPercentageEstimatorTelemetryProcessor.cs +++ b/src/ServerTelemetryChannel/Implementation/SamplingPercentageEstimatorTelemetryProcessor.cs @@ -120,6 +120,15 @@ public SamplingPercentageEstimatorTelemetryProcessor( this.evaluationInterval); } + /// + /// Gets or sets current sampling rate. + /// + internal int CurrentSamplingRate + { + get => this.currenSamplingRate; + set => this.currenSamplingRate = value; + } + /// /// Processes telemetry item. ///