Skip to content

Commit

Permalink
Initial sampling rate from applicationHost.config fix (#1048)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail-msft authored Jan 10, 2019
1 parent 64a9e5a commit 6b70c45
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Threading;

Expand All @@ -13,7 +14,8 @@
using Microsoft.ApplicationInsights.TestFramework;
using Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using Moq;

[TestClass]
public class AdaptiveSamplingTelemetryProcessorTest
{
Expand Down Expand Up @@ -295,6 +297,44 @@ public void AdaptiveSamplingSetsExcludedTypesOnInternalSamplingProcessor()
Assert.AreEqual("request;", internalProcessor.ExcludedTypes);
}

[TestMethod]
public void CurrentSamplingRateResetsOnInitialSamplingRateChange()
{
var nextMock = new Mock<ITelemetryProcessor>();
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<ITelemetryProcessor>();
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,
Expand Down
16 changes: 15 additions & 1 deletion src/ServerTelemetryChannel/AdaptiveSamplingTelemetryProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
}

/// <summary>
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -221,6 +225,16 @@ public double MovingAverageRatio
}
}

/// <summary>
/// Gets sampling telemetry processor.
/// </summary>
internal SamplingTelemetryProcessor SamplingTelemetryProcessor => this.samplingProcessor;

/// <summary>
/// Gets sampling percentage estimator telemetry processor.
/// </summary>
internal SamplingPercentageEstimatorTelemetryProcessor SamplingPercentageEstimatorTelemetryProcessor => this.estimatorProcessor;

/// <summary>
/// Processes telemetry item.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ public SamplingPercentageEstimatorTelemetryProcessor(
this.evaluationInterval);
}

/// <summary>
/// Gets or sets current sampling rate.
/// </summary>
internal int CurrentSamplingRate
{
get => this.currenSamplingRate;
set => this.currenSamplingRate = value;
}

/// <summary>
/// Processes telemetry item.
/// </summary>
Expand Down

0 comments on commit 6b70c45

Please sign in to comment.