/// <summary> /// Initializes a new instance of the <see cref="SamplingPercentageEstimatorTelemetryProcessor"/> class. /// <param name="settings">Dynamic sampling estimator settings.</param> /// <param name="callback">Callback to invoke every time sampling percentage is evaluated.</param> /// <param name="next">Next TelemetryProcessor in call chain.</param> /// </summary> public SamplingPercentageEstimatorTelemetryProcessor( Channel.Implementation.SamplingPercentageEstimatorSettings settings, Channel.Implementation.AdaptiveSamplingPercentageEvaluatedCallback callback, ITelemetryProcessor next) { if (settings == null) { throw new ArgumentNullException(nameof(settings)); } if (next == null) { throw new ArgumentNullException(nameof(next)); } this.evaluationCallback = callback; this.settings = settings; this.next = next; this.currenSamplingRate = settings.EffectiveInitialSamplingRate; this.itemCount = new ExponentialMovingAverageCounter(settings.EffectiveMovingAverageRatio); this.samplingPercentageLastChangeDateTime = DateTimeOffset.UtcNow; // set evaluation interval to default value if it is negative or zero this.evaluationInterval = this.settings.EffectiveEvaluationInterval; // set up timer to run math to estimate sampling percentage this.evaluationTimer = new Timer( this.EstimateSamplingPercentage, null, this.evaluationInterval, this.evaluationInterval); }
/// <summary> /// Initializes a new instance of the <see cref="AdaptiveSamplingTelemetryProcessor"/> class. /// <param name="settings">Sampling percentage estimator settings.</param> /// <param name="callback">Callback invoked every time sampling percentage is evaluated.</param> /// <param name="next">Next TelemetryProcessor in call chain.</param> /// </summary> public AdaptiveSamplingTelemetryProcessor( Channel.Implementation.SamplingPercentageEstimatorSettings settings, Channel.Implementation.AdaptiveSamplingPercentageEvaluatedCallback callback, ITelemetryProcessor next) { this.estimatorSettings = settings; this.evaluationCallback = callback; // make estimator telemetry processor work after sampling was done this.estimatorProcessor = new SamplingPercentageEstimatorTelemetryProcessor(settings, this.SamplingPercentageChanged, next); this.samplingProcessor = new SamplingTelemetryProcessor(next, this.estimatorProcessor); }
private void TraceSamplingPercentageEvaluation( double afterSamplingTelemetryItemRatePerSecond, double currentSamplingPercentage, double newSamplingPercentage, bool isSamplingPercentageChanged, Channel.Implementation.SamplingPercentageEstimatorSettings settings) { Trace.WriteLine(string.Format( "[Sampling% evaluation] {0}, Eps: {1}, Current %: {2}, New %: {3}, Changed: {4}", DateTimeOffset.UtcNow.ToString("o"), afterSamplingTelemetryItemRatePerSecond, currentSamplingPercentage, newSamplingPercentage, isSamplingPercentageChanged)); }
private void SamplingPercentageChanged( double afterSamplingTelemetryItemRatePerSecond, double currentSamplingPercentage, double newSamplingPercentage, bool isSamplingPercentageChanged, Channel.Implementation.SamplingPercentageEstimatorSettings settings) { if (isSamplingPercentageChanged) { this.samplingProcessor.SamplingPercentage = newSamplingPercentage; TelemetryChannelEventSource.Log.SamplingChanged(newSamplingPercentage); } this.evaluationCallback?.Invoke( afterSamplingTelemetryItemRatePerSecond, currentSamplingPercentage, newSamplingPercentage, isSamplingPercentageChanged, settings); }