Skip to content

Commit

Permalink
Update telemetry data to add moniker of the detected properties (#430)
Browse files Browse the repository at this point in the history
* Add new telemetry fields CrossCompanyCorrelatingId and Moniker

* Remove CrossCompanyCorrelatingId due to it may cause telemetry data too large
  • Loading branch information
vidai-msft authored Oct 20, 2024
1 parent 4d7fb62 commit ae44bad
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/Common/AzurePSCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -433,13 +433,13 @@ private void WriteSecretsWarningMessage()
var sanitizerInfo = _qosEvent.SanitizerInfo;
if (sanitizerInfo.ShowSecretsWarning && sanitizerInfo.SecretsDetected)
{
if (sanitizerInfo.DetectedProperties.Count == 0)
if (sanitizerInfo.DetectedProperties.IsEmpty)
{
WriteWarning(string.Format(Resources.DisplaySecretsWarningMessageWithoutProperty, MyInvocation.InvocationName));
}
else
{
WriteWarning(string.Format(Resources.DisplaySecretsWarningMessageWithProperty, MyInvocation.InvocationName, string.Join(", ", sanitizerInfo.DetectedProperties)));
WriteWarning(string.Format(Resources.DisplaySecretsWarningMessageWithProperty, MyInvocation.InvocationName, string.Join(", ", sanitizerInfo.DetectedProperties.PropertyNames)));
}
}
}
Expand Down
24 changes: 22 additions & 2 deletions src/Common/MetricHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,28 @@ private void PopulateSanitizerPropertiesFromQos(AzurePSQoSEvent qos, IDictionary
eventProperties["secrets-detected"] = secretsDetected.ToString();
if (secretsDetected)
{
var detectedProperties = qos.SanitizerInfo.DetectedProperties.Count == 0 ? "[None]" : string.Join(";", qos.SanitizerInfo.DetectedProperties);
eventProperties.Add("secrets-detected-properties", detectedProperties);
string detectedProperties = string.Empty;
if (qos.SanitizerInfo.DetectedProperties.IsEmpty)
{
eventProperties.Add("secrets-detected-properties", "[None]");
}
else
{
var sbDetectedProperties = new StringBuilder();
sbDetectedProperties.Append("[");
foreach (var detectedProperty in qos.SanitizerInfo.DetectedProperties)
{
sbDetectedProperties.Append("{");

sbDetectedProperties.Append($"\"name\":\"{detectedProperty.Key}\",");
sbDetectedProperties.Append($"\"moniker\":\"{string.Join(";", detectedProperty.Value)}\"");

sbDetectedProperties.Append("},");
}
sbDetectedProperties.Length--;
sbDetectedProperties.Append("]");
eventProperties.Add("secrets-detected-properties", sbDetectedProperties.ToString());
}
}
if (qos.SanitizerInfo.HasErrorInDetection && qos.SanitizerInfo.DetectionError != null)
{
Expand Down
70 changes: 64 additions & 6 deletions src/Common/Sanitizer/SanitizerTelemetry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,82 @@
// ----------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Generic;

namespace Microsoft.WindowsAzure.Commands.Common.Sanitizer
{
public class SanitizerTelemetry
public class DetectedPropertiesInfo : IEnumerable<KeyValuePair<string, HashSet<string>>>
{
public bool ShowSecretsWarning { get; set; } = false;
private readonly Dictionary<string, HashSet<string>> _internalProperties;

public DetectedPropertiesInfo()
{
_internalProperties = new Dictionary<string, HashSet<string>>();
}

public bool IsEmpty => _internalProperties.Count == 0;

public IEnumerable<string> PropertyNames => _internalProperties.Keys;

public void AddPropertyInfo(string propertyName, string moniker)
{
if (!_internalProperties.TryGetValue(propertyName, out var propertyInfo))
{
propertyInfo = new HashSet<string>();
_internalProperties[propertyName] = propertyInfo;
}

public bool SecretsDetected { get; set; } = false;
propertyInfo.Add(moniker);
}

public HashSet<string> DetectedProperties { get; set; } = new HashSet<string>();
public void AddPropertyInfo(string propertyName, HashSet<string> monikers)
{
if (!_internalProperties.TryGetValue(propertyName, out var propertyInfo))
{
propertyInfo = new HashSet<string>();
_internalProperties[propertyName] = propertyInfo;
}

public bool HasErrorInDetection { get; set; } = false;
propertyInfo.UnionWith(monikers);
}

public bool ContainsProperty(string propertyName)
{
return _internalProperties.ContainsKey(propertyName);
}

public IEnumerator<KeyValuePair<string, HashSet<string>>> GetEnumerator()
{
return _internalProperties.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}

public class SanitizerTelemetry
{
public bool ShowSecretsWarning { get; set; }

public bool SecretsDetected { get; set; }

public bool HasErrorInDetection { get; set; }

public Exception DetectionError { get; set; }

public TimeSpan SanitizeDuration { get; set; }

public DetectedPropertiesInfo DetectedProperties { get; private set; }

public SanitizerTelemetry(bool showSecretsWarning)
{
ShowSecretsWarning = showSecretsWarning;
SecretsDetected = false;
HasErrorInDetection = false;
DetectedProperties = new DetectedPropertiesInfo();
}

public void Combine(SanitizerTelemetry telemetry)
Expand All @@ -42,10 +97,13 @@ public void Combine(SanitizerTelemetry telemetry)
{
ShowSecretsWarning = ShowSecretsWarning || telemetry.ShowSecretsWarning;
SecretsDetected = SecretsDetected || telemetry.SecretsDetected;
DetectedProperties.UnionWith(telemetry.DetectedProperties);
HasErrorInDetection = HasErrorInDetection || telemetry.HasErrorInDetection;
DetectionError = DetectionError ?? telemetry.DetectionError;
SanitizeDuration += telemetry.SanitizeDuration;
foreach (var property in telemetry.DetectedProperties)
{
DetectedProperties.AddPropertyInfo(property.Key, property.Value);
}
}
}
}
Expand Down

0 comments on commit ae44bad

Please sign in to comment.