-
Notifications
You must be signed in to change notification settings - Fork 286
/
SamplingScoreGenerator.cs
59 lines (50 loc) · 1.71 KB
/
SamplingScoreGenerator.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
namespace Microsoft.ApplicationInsights.WindowsServer.Channel.Implementation
{
using System;
using Microsoft.ApplicationInsights.Channel;
/// <summary>
/// Utility class for sampling score generation.
/// </summary>
internal static class SamplingScoreGenerator
{
/// <summary>
/// Generates telemetry sampling score between 0 and 100.
/// </summary>
/// <param name="telemetry">Telemetry item to score.</param>
/// <returns>Item sampling score.</returns>
public static double GetSamplingScore(ITelemetry telemetry)
{
double samplingScore = 0;
if (telemetry.Context.User.Id != null)
{
samplingScore = (double)telemetry.Context.User.Id.GetSamplingHashCode() / int.MaxValue;
}
else if (telemetry.Context.Operation.Id != null)
{
samplingScore = (double)telemetry.Context.Operation.Id.GetSamplingHashCode() / int.MaxValue;
}
else
{
samplingScore = (double)WeakConcurrentRandom.Instance.Next() / ulong.MaxValue;
}
return samplingScore * 100;
}
internal static int GetSamplingHashCode(this string input)
{
if (input == null)
{
return 0;
}
while (input.Length < 8)
{
input = input + input;
}
int hash = 5381;
for (int i = 0; i < input.Length; i++)
{
hash = ((hash << 5) + hash) + (int)input[i];
}
return hash == int.MinValue ? int.MaxValue : Math.Abs(hash);
}
}
}