Skip to content
This repository has been archived by the owner on Jun 17, 2024. It is now read-only.

Commit

Permalink
demo 7
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Kanzhelev committed Sep 27, 2018
1 parent 36917a3 commit 830e07b
Show file tree
Hide file tree
Showing 4 changed files with 249 additions and 6 deletions.
54 changes: 54 additions & 0 deletions ApplicationInsightsDataROI/AggressivelySampleFastDependencies.cs
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 ApplicationInsightsDataROI/AggressivelySampleFastRequests.cs
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);
}
}
}
134 changes: 134 additions & 0 deletions ApplicationInsightsDataROI/Demo7.cs
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 });
}
}
}
}
13 changes: 7 additions & 6 deletions ApplicationInsightsDataROI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ public static void Main(string[] args)
CancellationTokenSource tokenSource = new CancellationTokenSource();
CancellationToken token = tokenSource.Token;

Demo1.RunAsync(token).Wait(); // default AI model with request/dependency/exception/trace and event
Demo2.RunAsync(token).Wait(); // price calculation and fixed & adaptive sampling
Demo3.RunAsync(token).Wait(); // exemplification of dependencies
Demo4.RunAsync(token).Wait(); // filtering of dependencies
Demo5.RunAsync(token).Wait(); // metrics aggregation, channeling business telemetry into a different iKey and default context settings
Demo6.RunAsync(token).Wait(); // LiveMetrics enablement
// Demo1.RunAsync(token).Wait(); // default AI model with request/dependency/exception/trace and event
// Demo2.RunAsync(token).Wait(); // price calculation and fixed & adaptive sampling
// Demo3.RunAsync(token).Wait(); // exemplification of dependencies
// Demo4.RunAsync(token).Wait(); // filtering of dependencies
// Demo5.RunAsync(token).Wait(); // metrics aggregation, channeling business telemetry into a different iKey and default context settings
// Demo6.RunAsync(token).Wait(); // LiveMetrics enablement
// Demo7.RunAsync(token).Wait(); // aggressive sampling instead of filtering

Console.ReadKey();
}
Expand Down

0 comments on commit 830e07b

Please sign in to comment.