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
/
Demo7.cs
134 lines (111 loc) · 5.9 KB
/
Demo7.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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 });
}
}
}
}