Skip to content

Commit ae44bad

Browse files
authored
Update telemetry data to add moniker of the detected properties (#430)
* Add new telemetry fields CrossCompanyCorrelatingId and Moniker * Remove CrossCompanyCorrelatingId due to it may cause telemetry data too large
1 parent 4d7fb62 commit ae44bad

File tree

3 files changed

+88
-10
lines changed

3 files changed

+88
-10
lines changed

src/Common/AzurePSCmdlet.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -433,13 +433,13 @@ private void WriteSecretsWarningMessage()
433433
var sanitizerInfo = _qosEvent.SanitizerInfo;
434434
if (sanitizerInfo.ShowSecretsWarning && sanitizerInfo.SecretsDetected)
435435
{
436-
if (sanitizerInfo.DetectedProperties.Count == 0)
436+
if (sanitizerInfo.DetectedProperties.IsEmpty)
437437
{
438438
WriteWarning(string.Format(Resources.DisplaySecretsWarningMessageWithoutProperty, MyInvocation.InvocationName));
439439
}
440440
else
441441
{
442-
WriteWarning(string.Format(Resources.DisplaySecretsWarningMessageWithProperty, MyInvocation.InvocationName, string.Join(", ", sanitizerInfo.DetectedProperties)));
442+
WriteWarning(string.Format(Resources.DisplaySecretsWarningMessageWithProperty, MyInvocation.InvocationName, string.Join(", ", sanitizerInfo.DetectedProperties.PropertyNames)));
443443
}
444444
}
445445
}

src/Common/MetricHelper.cs

+22-2
Original file line numberDiff line numberDiff line change
@@ -481,8 +481,28 @@ private void PopulateSanitizerPropertiesFromQos(AzurePSQoSEvent qos, IDictionary
481481
eventProperties["secrets-detected"] = secretsDetected.ToString();
482482
if (secretsDetected)
483483
{
484-
var detectedProperties = qos.SanitizerInfo.DetectedProperties.Count == 0 ? "[None]" : string.Join(";", qos.SanitizerInfo.DetectedProperties);
485-
eventProperties.Add("secrets-detected-properties", detectedProperties);
484+
string detectedProperties = string.Empty;
485+
if (qos.SanitizerInfo.DetectedProperties.IsEmpty)
486+
{
487+
eventProperties.Add("secrets-detected-properties", "[None]");
488+
}
489+
else
490+
{
491+
var sbDetectedProperties = new StringBuilder();
492+
sbDetectedProperties.Append("[");
493+
foreach (var detectedProperty in qos.SanitizerInfo.DetectedProperties)
494+
{
495+
sbDetectedProperties.Append("{");
496+
497+
sbDetectedProperties.Append($"\"name\":\"{detectedProperty.Key}\",");
498+
sbDetectedProperties.Append($"\"moniker\":\"{string.Join(";", detectedProperty.Value)}\"");
499+
500+
sbDetectedProperties.Append("},");
501+
}
502+
sbDetectedProperties.Length--;
503+
sbDetectedProperties.Append("]");
504+
eventProperties.Add("secrets-detected-properties", sbDetectedProperties.ToString());
505+
}
486506
}
487507
if (qos.SanitizerInfo.HasErrorInDetection && qos.SanitizerInfo.DetectionError != null)
488508
{

src/Common/Sanitizer/SanitizerTelemetry.cs

+64-6
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,82 @@
1313
// ----------------------------------------------------------------------------------
1414

1515
using System;
16+
using System.Collections;
1617
using System.Collections.Generic;
1718

1819
namespace Microsoft.WindowsAzure.Commands.Common.Sanitizer
1920
{
20-
public class SanitizerTelemetry
21+
public class DetectedPropertiesInfo : IEnumerable<KeyValuePair<string, HashSet<string>>>
2122
{
22-
public bool ShowSecretsWarning { get; set; } = false;
23+
private readonly Dictionary<string, HashSet<string>> _internalProperties;
24+
25+
public DetectedPropertiesInfo()
26+
{
27+
_internalProperties = new Dictionary<string, HashSet<string>>();
28+
}
29+
30+
public bool IsEmpty => _internalProperties.Count == 0;
31+
32+
public IEnumerable<string> PropertyNames => _internalProperties.Keys;
33+
34+
public void AddPropertyInfo(string propertyName, string moniker)
35+
{
36+
if (!_internalProperties.TryGetValue(propertyName, out var propertyInfo))
37+
{
38+
propertyInfo = new HashSet<string>();
39+
_internalProperties[propertyName] = propertyInfo;
40+
}
2341

24-
public bool SecretsDetected { get; set; } = false;
42+
propertyInfo.Add(moniker);
43+
}
2544

26-
public HashSet<string> DetectedProperties { get; set; } = new HashSet<string>();
45+
public void AddPropertyInfo(string propertyName, HashSet<string> monikers)
46+
{
47+
if (!_internalProperties.TryGetValue(propertyName, out var propertyInfo))
48+
{
49+
propertyInfo = new HashSet<string>();
50+
_internalProperties[propertyName] = propertyInfo;
51+
}
2752

28-
public bool HasErrorInDetection { get; set; } = false;
53+
propertyInfo.UnionWith(monikers);
54+
}
55+
56+
public bool ContainsProperty(string propertyName)
57+
{
58+
return _internalProperties.ContainsKey(propertyName);
59+
}
60+
61+
public IEnumerator<KeyValuePair<string, HashSet<string>>> GetEnumerator()
62+
{
63+
return _internalProperties.GetEnumerator();
64+
}
65+
66+
IEnumerator IEnumerable.GetEnumerator()
67+
{
68+
return GetEnumerator();
69+
}
70+
}
71+
72+
public class SanitizerTelemetry
73+
{
74+
public bool ShowSecretsWarning { get; set; }
75+
76+
public bool SecretsDetected { get; set; }
77+
78+
public bool HasErrorInDetection { get; set; }
2979

3080
public Exception DetectionError { get; set; }
3181

3282
public TimeSpan SanitizeDuration { get; set; }
3383

84+
public DetectedPropertiesInfo DetectedProperties { get; private set; }
85+
3486
public SanitizerTelemetry(bool showSecretsWarning)
3587
{
3688
ShowSecretsWarning = showSecretsWarning;
89+
SecretsDetected = false;
90+
HasErrorInDetection = false;
91+
DetectedProperties = new DetectedPropertiesInfo();
3792
}
3893

3994
public void Combine(SanitizerTelemetry telemetry)
@@ -42,10 +97,13 @@ public void Combine(SanitizerTelemetry telemetry)
4297
{
4398
ShowSecretsWarning = ShowSecretsWarning || telemetry.ShowSecretsWarning;
4499
SecretsDetected = SecretsDetected || telemetry.SecretsDetected;
45-
DetectedProperties.UnionWith(telemetry.DetectedProperties);
46100
HasErrorInDetection = HasErrorInDetection || telemetry.HasErrorInDetection;
47101
DetectionError = DetectionError ?? telemetry.DetectionError;
48102
SanitizeDuration += telemetry.SanitizeDuration;
103+
foreach (var property in telemetry.DetectedProperties)
104+
{
105+
DetectedProperties.AddPropertyInfo(property.Key, property.Value);
106+
}
49107
}
50108
}
51109
}

0 commit comments

Comments
 (0)