This repository has been archived by the owner on Jun 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sergey Kanzhelev
committed
Sep 27, 2018
1 parent
36917a3
commit 830e07b
Showing
4 changed files
with
249 additions
and
6 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
ApplicationInsightsDataROI/AggressivelySampleFastDependencies.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
namespace ApplicationInsightsDataROI | ||
{ | ||
using System; | ||
using Microsoft.ApplicationInsights.Channel; | ||
using Microsoft.ApplicationInsights.DataContracts; | ||
using Microsoft.ApplicationInsights.Extensibility; | ||
using Microsoft.ApplicationInsights.WindowsServer.Channel.Implementation; | ||
using Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel; | ||
|
||
/// <summary> | ||
/// This initializer applies aggressive sampling to dependency telemetry that runs faster than threshold value. | ||
/// </summary> | ||
internal class AggressivelySampleFastDependencies : ITelemetryProcessor | ||
{ | ||
private readonly ITelemetryProcessor next; | ||
private readonly AdaptiveSamplingTelemetryProcessor samplingProcessor; | ||
|
||
public AggressivelySampleFastDependencies(ITelemetryProcessor next) | ||
{ | ||
this.next = next; | ||
this.samplingProcessor = new AdaptiveSamplingTelemetryProcessor(next); | ||
} | ||
|
||
/// <summary> | ||
/// Threshold defining whether request call considered fast or slow. | ||
/// </summary> | ||
public TimeSpan Threshold { get; set; } = TimeSpan.FromMilliseconds(300); | ||
|
||
public double InitialSamplingPercentage { get => this.samplingProcessor.InitialSamplingPercentage; set => this.samplingProcessor.InitialSamplingPercentage = value; } | ||
|
||
public double MinSamplingPercentage { get => this.samplingProcessor.MinSamplingPercentage; set => this.samplingProcessor.MinSamplingPercentage = value; } | ||
|
||
public double MaxSamplingPercentage { get => this.samplingProcessor.MaxSamplingPercentage; set => this.samplingProcessor.MaxSamplingPercentage = value; } | ||
|
||
public void Process(ITelemetry item) | ||
{ | ||
// check the telemetry type and duration | ||
if (item is DependencyTelemetry) | ||
{ | ||
var d = item as DependencyTelemetry; | ||
if (d.Duration < this.Threshold) | ||
{ | ||
// let sampling processor decide what to do | ||
// with this fast incoming request | ||
this.samplingProcessor.Process(item); | ||
return; | ||
} | ||
} | ||
|
||
// in all other cases simply call next | ||
this.next.Process(item); | ||
} | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
ApplicationInsightsDataROI/AggressivelySampleFastRequests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
namespace ApplicationInsightsDataROI | ||
{ | ||
using System; | ||
using Microsoft.ApplicationInsights.Channel; | ||
using Microsoft.ApplicationInsights.DataContracts; | ||
using Microsoft.ApplicationInsights.Extensibility; | ||
using Microsoft.ApplicationInsights.WindowsServer.Channel.Implementation; | ||
using Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel; | ||
|
||
/// <summary> | ||
/// This initializer applies aggressive sampling to request telemetry that runs faster than threshold value. | ||
/// </summary> | ||
internal class AggressivelySampleFastRequests : ITelemetryProcessor | ||
{ | ||
private readonly ITelemetryProcessor next; | ||
private readonly AdaptiveSamplingTelemetryProcessor samplingProcessor; | ||
|
||
public AggressivelySampleFastRequests(ITelemetryProcessor next) | ||
{ | ||
this.next = next; | ||
this.samplingProcessor = new AdaptiveSamplingTelemetryProcessor(next); | ||
} | ||
|
||
/// <summary> | ||
/// Threshold defining whether request call considered fast or slow. | ||
/// </summary> | ||
public TimeSpan Threshold { get; set; } = TimeSpan.FromMilliseconds(500); | ||
|
||
public double InitialSamplingPercentage { get => this.samplingProcessor.InitialSamplingPercentage; set => this.samplingProcessor.InitialSamplingPercentage = value; } | ||
|
||
public double MinSamplingPercentage { get => this.samplingProcessor.MinSamplingPercentage; set => this.samplingProcessor.MinSamplingPercentage = value; } | ||
|
||
public double MaxSamplingPercentage { get => this.samplingProcessor.MaxSamplingPercentage; set => this.samplingProcessor.MaxSamplingPercentage = value; } | ||
|
||
public void Process(ITelemetry item) | ||
{ | ||
// check the telemetry type and duration | ||
if (item is RequestTelemetry) | ||
{ | ||
var r = item as RequestTelemetry; | ||
if (r.Duration < this.Threshold) | ||
{ | ||
// let sampling processor decide what to do | ||
// with this fast incoming request | ||
this.samplingProcessor.Process(item); | ||
return; | ||
} | ||
} | ||
|
||
// in all other cases simply call next | ||
this.next.Process(item); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
namespace ApplicationInsightsDataROI | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.ApplicationInsights; | ||
using Microsoft.ApplicationInsights.DataContracts; | ||
using Microsoft.ApplicationInsights.DependencyCollector; | ||
using Microsoft.ApplicationInsights.Extensibility; | ||
using Microsoft.ApplicationInsights.WindowsServer.Channel.Implementation; | ||
using Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel; | ||
|
||
internal class Demo7 | ||
{ | ||
public static async Task RunAsync(CancellationToken token) | ||
{ | ||
// set Instrumentation Key | ||
var configuration = new TelemetryConfiguration(); | ||
configuration.InstrumentationKey = "4282169f-e83f-46f7-b38a-436087fa856d"; | ||
|
||
// automatically correlate all telemetry data with request | ||
configuration.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer()); | ||
|
||
// initialize state for the telemetry size calculation | ||
var collectedItems = new ProcessedItems(); | ||
var sentItems = new ProcessedItems(); | ||
|
||
// build telemetry processing pipeline | ||
configuration.TelemetryProcessorChainBuilder | ||
|
||
// this telemetry processor will be executed first for all telemetry items to calculate the size and # of items | ||
.Use((next) => { return new SizeCalculatorTelemetryProcessor(next, collectedItems); }) | ||
|
||
.Use((next) => | ||
{ | ||
return new AggressivelySampleFastRequests(next) | ||
{ | ||
InitialSamplingPercentage = 5, | ||
MaxSamplingPercentage = 5, | ||
MinSamplingPercentage = 5, | ||
}; | ||
}) | ||
|
||
.Use((next) => | ||
{ | ||
return new AggressivelySampleFastDependencies(next) | ||
{ | ||
InitialSamplingPercentage = 5, | ||
MaxSamplingPercentage = 5, | ||
MinSamplingPercentage = 5, | ||
}; | ||
}) | ||
|
||
// this is a standard adaptive sampling telemetry processor that will sample in/out any telemetry item it receives | ||
.Use((next) => | ||
{ | ||
return new AdaptiveSamplingTelemetryProcessor(next) | ||
{ | ||
InitialSamplingPercentage = 25, | ||
MaxSamplingPercentage = 25, | ||
MinSamplingPercentage = 25, | ||
}; | ||
}) | ||
|
||
// this telemetry processor will be execuyted ONLY when telemetry is sampled in | ||
.Use((next) => { return new SizeCalculatorTelemetryProcessor(next, sentItems); }) | ||
.Build(); | ||
|
||
var client = new TelemetryClient(configuration); | ||
|
||
var iteration = 0; | ||
var http = new HttpClient(); | ||
|
||
while (!token.IsCancellationRequested) | ||
{ | ||
iteration++; | ||
|
||
var t1 = new Task(() => | ||
{ | ||
using (var operation = client.StartOperation<RequestTelemetry>("Slow request")) | ||
{ | ||
client.TrackEvent("test", new Dictionary<string, string>() { { "iteration", iteration.ToString() } }); | ||
client.TrackTrace($"Iteration {iteration} happened", SeverityLevel.Information); | ||
using (var fastDependency = client.StartOperation<DependencyTelemetry>("Fast dependency")) | ||
{ | ||
Thread.Sleep(TimeSpan.FromMilliseconds(2)); | ||
} | ||
using (var slowDependency = client.StartOperation<DependencyTelemetry>("slow dependency")) | ||
{ | ||
Thread.Sleep(TimeSpan.FromMilliseconds(1000)); | ||
} | ||
client.StopOperation(operation); | ||
Console.WriteLine($"Iteration {iteration}. Elapsed time: {operation.Telemetry.Duration}. Collected Telemetry: {collectedItems.Size}/{collectedItems.Count}. Sent Telemetry: {sentItems.Size}/{sentItems.Count}. Ratio: {1.0 * collectedItems.Size / sentItems.Size}"); | ||
client.TrackMetric("[RAW] Reduction Size", collectedItems.Size - sentItems.Size); | ||
} | ||
}); | ||
|
||
var t2 = new Task(() => | ||
{ | ||
using (var operation = client.StartOperation<RequestTelemetry>("Fast request")) | ||
{ | ||
client.TrackEvent("test", new Dictionary<string, string>() { { "iteration", iteration.ToString() } }); | ||
client.TrackTrace($"Iteration {iteration} happened", SeverityLevel.Information); | ||
using (var fastDependency = client.StartOperation<DependencyTelemetry>("Fast dependency")) | ||
{ | ||
Thread.Sleep(TimeSpan.FromMilliseconds(2)); | ||
} | ||
using (var slowDependency = client.StartOperation<DependencyTelemetry>("slow dependency")) | ||
{ | ||
Thread.Sleep(TimeSpan.FromMilliseconds(400)); | ||
} | ||
client.StopOperation(operation); | ||
Console.WriteLine($"Iteration {iteration}. Elapsed time: {operation.Telemetry.Duration}. Collected Telemetry: {collectedItems.Size}/{collectedItems.Count}. Sent Telemetry: {sentItems.Size}/{sentItems.Count}. Ratio: {1.0 * collectedItems.Size / sentItems.Size}"); | ||
client.TrackMetric("[RAW] Reduction Size", collectedItems.Size - sentItems.Size); | ||
} | ||
}); | ||
|
||
t1.Start(); | ||
t2.Start(); | ||
Task.WaitAll(new Task[] { t1, t2 }); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters