-
Notifications
You must be signed in to change notification settings - Fork 315
/
DataCollectionTelemetryManager.cs
96 lines (78 loc) · 4.72 KB
/
DataCollectionTelemetryManager.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Linq;
using Microsoft.VisualStudio.TestPlatform.Common.DataCollector.Interfaces;
using Microsoft.VisualStudio.TestPlatform.Common.Telemetry;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;
namespace Microsoft.VisualStudio.TestPlatform.Common.DataCollector;
/// <summary>
/// Stores and provides telemetry information for data collection.
/// </summary>
internal class DataCollectionTelemetryManager : IDataCollectionTelemetryManager
{
private const string CorProfilerVariable = "COR_PROFILER";
private const string CoreClrProfilerVariable = "CORECLR_PROFILER";
private const string ClrIeInstrumentationMethodConfigurationPrefix32Variable = "MicrosoftInstrumentationEngine_ConfigPath32_";
private const string ClrIeInstrumentationMethodConfigurationPrefix64Variable = "MicrosoftInstrumentationEngine_ConfigPath64_";
private static readonly Guid ClrIeProfilerGuid = new("{324f817a-7420-4e6d-b3c1-143fbed6d855}");
private const string OverwrittenProfilerName = "overwritten";
private readonly IRequestData _requestData;
internal DataCollectionTelemetryManager(IRequestData requestData)
{
_requestData = requestData;
}
/// <inheritdoc/>
public void RecordEnvironmentVariableAddition(DataCollectorInformation dataCollectorInformation, string name, string value)
{
RecordProfilerMetricForNewVariable(CorProfilerVariable, TelemetryDataConstants.DataCollectorsCorProfiler, dataCollectorInformation, name, value);
RecordProfilerMetricForNewVariable(CoreClrProfilerVariable, TelemetryDataConstants.DataCollectorsCoreClrProfiler, dataCollectorInformation, name, value);
}
/// <inheritdoc/>
public void RecordEnvironmentVariableConflict(DataCollectorInformation dataCollectorInformation, string name, string value, string existingValue)
{
RecordProfilerMetricForConflictedVariable(CorProfilerVariable, TelemetryDataConstants.DataCollectorsCorProfiler, dataCollectorInformation, name, value, existingValue);
RecordProfilerMetricForConflictedVariable(CoreClrProfilerVariable, TelemetryDataConstants.DataCollectorsCoreClrProfiler, dataCollectorInformation, name, value, existingValue);
}
private void RecordProfilerMetricForNewVariable(string profilerVariable, string telemetryPrefix, DataCollectorInformation dataCollectorInformation, string name, string value)
{
if (!string.Equals(profilerVariable, name, StringComparison.Ordinal))
{
return;
}
_requestData.MetricsCollection.Add(GetTelemetryKey(telemetryPrefix, dataCollectorInformation), GetProfilerGuid(value).ToString());
}
private void RecordProfilerMetricForConflictedVariable(string profilerVariable, string telemetryPrefix, DataCollectorInformation dataCollectorInformation, string name, string value, string existingValue)
{
// If data collector is requesting same profiler record it same as new
if (string.Equals(value, existingValue, StringComparison.OrdinalIgnoreCase))
{
RecordProfilerMetricForNewVariable(profilerVariable, telemetryPrefix, dataCollectorInformation, name, value);
return;
}
if (!string.Equals(profilerVariable, name, StringComparison.Ordinal))
{
return;
}
var existingProfilerGuid = GetProfilerGuid(existingValue);
if (ClrIeProfilerGuid == existingProfilerGuid)
{
if (dataCollectorInformation.TestExecutionEnvironmentVariables != null &&
dataCollectorInformation.TestExecutionEnvironmentVariables.Any(pair => pair.Key.StartsWith(ClrIeInstrumentationMethodConfigurationPrefix32Variable)) &&
dataCollectorInformation.TestExecutionEnvironmentVariables.Any(pair => pair.Key.StartsWith(ClrIeInstrumentationMethodConfigurationPrefix64Variable)))
{
_requestData.MetricsCollection.Add(GetTelemetryKey(telemetryPrefix, dataCollectorInformation), ClrIeProfilerGuid.ToString());
return;
}
}
_requestData.MetricsCollection.Add(GetTelemetryKey(telemetryPrefix, dataCollectorInformation), $"{existingProfilerGuid}({OverwrittenProfilerName}:{GetProfilerGuid(value)})");
}
private static Guid GetProfilerGuid(string profilerGuid)
{
return Guid.TryParse(profilerGuid, out var guid) ? guid : Guid.Empty;
}
private static string GetTelemetryKey(string telemetryPrefix, DataCollectorInformation dataCollectorInformation)
{
return $"{telemetryPrefix}.{dataCollectorInformation.DataCollectorConfig?.TypeUri}";
}
}