Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
msJinLei committed Aug 15, 2024
1 parent ccc48dc commit 439c8e8
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using System.Collections.Concurrent;
using Newtonsoft.Json;

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Xml.Serialization;
using Newtonsoft.Json;

namespace Microsoft.Azure.Commands.Common.Authentication.Abstractions
{
public class AuthenticationInfo : IAuthenticationInfo
/// <summary>
/// A model class for authenction telemetry record.
/// </summary>
public class AuthTelemetryRecord : IAuthTelemetryRecord
{
/// <summary>
/// Class name of the TokenCredential, stands for the authentication method
Expand All @@ -36,15 +39,14 @@ public class AuthenticationInfo : IAuthenticationInfo
/// Additional properties for AuthenticationInfo
/// </summary>
[JsonIgnore]
[XmlIgnore]
public IDictionary<string, string> ExtendedProperties { get; } = new ConcurrentDictionary<string, string>(StringComparer.OrdinalIgnoreCase);

public AuthenticationInfo()
public AuthTelemetryRecord()
{
TokenCredentialName = null;
}

public AuthenticationInfo(IAuthenticationInfo other, bool? isSuccess = null)
public AuthTelemetryRecord(IAuthTelemetryRecord other, bool? isSuccess = null)
{
this.TokenCredentialName = other.TokenCredentialName;
this.AuthenticationSuccess = isSuccess ?? other.AuthenticationSuccess;
Expand All @@ -59,8 +61,14 @@ public AuthenticationInfo(IAuthenticationInfo other, bool? isSuccess = null)
/// </summary>
public const string TokenCacheEnabled = "TokenCacheEnabled";

/// <summary>
/// Prefix of properties of the first record of authentication telemetry record.
/// </summary>
public const string AuthInfoTelemetryHeadKey = "auth-info-head";

/// <summary>
/// Key of the left records of authentication telemetry.
/// </summary>
public const string AuthInfoTelemetrySubsequentKey = "auth-info-sub";
}
}
48 changes: 48 additions & 0 deletions src/Authentication.Abstractions/AuthenticationTelemetry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using System.Collections.Generic;

namespace Microsoft.Azure.Commands.Common.Authentication.Abstractions
{
/// <summary>
/// A model class for a list of authenction telemetry records.
/// </summary>
public class AuthenticationTelemetry
{
/// <summary>
/// The first record of authentication telemetry data, usually describes the main method of this authentication process.
/// </summary>
public IAuthTelemetryRecord Head { get; } = null;

/// <summary>
/// The left part of authentication telemetry records.
/// </summary>
public IList<IAuthTelemetryRecord> Tail { get; } = new List<IAuthTelemetryRecord>();

public AuthenticationTelemetry(IEnumerable<IAuthTelemetryRecord> records)
{
var enumerator = records.GetEnumerator();
if (enumerator.MoveNext())
{
Head = enumerator.Current;
}

while (enumerator.MoveNext())
{
Tail.Add(enumerator.Current);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@

namespace Microsoft.Azure.Commands.Common.Authentication.Abstractions
{
public interface IAuthenticationInfo : IExtensibleModel
/// <summary>
/// Representation of an authentication telemetry record
/// </summary>
public interface IAuthTelemetryRecord : IExtensibleModel
{
/// <summary>
/// Class name of the TokenCredential, stands for the authentication method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,6 @@ IAccessToken Authenticate(
/// <summary>
/// Get the information to be recorded in Telemetry
/// </summary>
IList<IAuthenticationInfo> GetDataForTelemetry();
AuthenticationTelemetry GetDataForTelemetry();
}
}
3 changes: 1 addition & 2 deletions src/Common/AzurePSCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -431,8 +431,6 @@ protected override void EndProcessing()
WriteWarningMessageForVersionUpgrade();
WriteSecretsWarningMessage();

_qosEvent.AuthInfo = AzureSession.Instance.AuthenticationFactory.GetDataForTelemetry();

if (MetricHelper.IsCalledByUser()
&& SurveyHelper.GetInstance().ShouldPromptAzSurvey()
&& (AzureSession.Instance.TryGetComponent<IConfigManager>(nameof(IConfigManager), out var configManager)
Expand Down Expand Up @@ -845,6 +843,7 @@ protected void LogQosEvent()

_qosEvent.ParameterSetName = this.ParameterSetName;
_qosEvent.FinishQosEvent();
_qosEvent.AuthTelemetry = AzureSession.Instance.AuthenticationFactory.GetDataForTelemetry();

if (!IsUsageMetricEnabled && (!IsErrorMetricEnabled || _qosEvent.IsSuccess))
{
Expand Down
30 changes: 10 additions & 20 deletions src/Common/MetricHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,28 +292,18 @@ public void SetPSHost(PSHost host)
{
}

private static void PopulateAuthenticationInfoFromQos(IEnumerable<IAuthenticationInfo> infos, IDictionary<string, string> eventProperties)
private static void PopulateAuthenticationPropertiesFromQos(AuthenticationTelemetry telemetry, IDictionary<string, string> eventProperties)
{
var enumerator = infos.GetEnumerator();
if (enumerator.MoveNext())
{
var info = enumerator.Current;
eventProperties[$"{AuthenticationInfo.AuthInfoTelemetryHeadKey}-{nameof(info.TokenCredentialName).ToLower()}"] = info.TokenCredentialName;
eventProperties[$"{AuthenticationInfo.AuthInfoTelemetryHeadKey}-{nameof(info.AuthenticationSuccess).ToLower()}"] = info.AuthenticationSuccess.ToString();

foreach (var property in info.ExtendedProperties)
{
eventProperties[$"{AuthenticationInfo.AuthInfoTelemetryHeadKey}-{property.Key.ToLower()}"] = property.Value;
}
}
var record = telemetry.Head;
eventProperties[$"{AuthTelemetryRecord.AuthInfoTelemetryHeadKey}-{nameof(record.TokenCredentialName).ToLower()}"] = record.TokenCredentialName;
eventProperties[$"{AuthTelemetryRecord.AuthInfoTelemetryHeadKey}-{nameof(record.AuthenticationSuccess).ToLower()}"] = record.AuthenticationSuccess.ToString();

var subAuthInfos = new List<AuthenticationInfo>();
while (enumerator.MoveNext())
foreach (var property in record.ExtendedProperties)
{
var info = enumerator.Current;
subAuthInfos.Add(info as AuthenticationInfo);
eventProperties[$"{AuthTelemetryRecord.AuthInfoTelemetryHeadKey}-{property.Key.ToLower()}"] = property.Value;
}
eventProperties[AuthenticationInfo.AuthInfoTelemetrySubsequentKey] = JsonConvert.SerializeObject(subAuthInfos);

eventProperties[AuthTelemetryRecord.AuthInfoTelemetrySubsequentKey] = JsonConvert.SerializeObject(telemetry.Tail);
}

private void PopulatePropertiesFromQos(AzurePSQoSEvent qos, IDictionary<string, string> eventProperties, bool populateException = false)
Expand Down Expand Up @@ -478,7 +468,7 @@ private void PopulatePropertiesFromQos(AzurePSQoSEvent qos, IDictionary<string,

PopulateConfigMetricsFromQos(qos, eventProperties);
PopulateSanitizerPropertiesFromQos(qos, eventProperties);
PopulateAuthenticationInfoFromQos(qos.AuthInfo, eventProperties);
PopulateAuthenticationPropertiesFromQos(qos.AuthTelemetry, eventProperties);

if (qos.InputFromPipeline != null)
{
Expand Down Expand Up @@ -703,7 +693,7 @@ public class AzurePSQoSEvent

public SanitizerTelemetry SanitizerInfo { get; set; }

public IEnumerable<IAuthenticationInfo> AuthInfo { get; set; }
public AuthenticationTelemetry AuthTelemetry { get; set; }

public AzurePSQoSEvent()
{
Expand Down

0 comments on commit 439c8e8

Please sign in to comment.