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 439c8e8 commit 0358f72
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 9 deletions.
6 changes: 4 additions & 2 deletions src/Authentication.Abstractions/AuthTelemetryRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public class AuthTelemetryRecord : IAuthTelemetryRecord
/// </summary>
public bool AuthenticationSuccess { get; set; } = false;

public bool correlationId { get; set; }

/// <summary>
/// Additional properties for AuthenticationInfo
/// </summary>
Expand Down Expand Up @@ -64,11 +66,11 @@ public AuthTelemetryRecord(IAuthTelemetryRecord other, bool? isSuccess = null)
/// <summary>
/// Prefix of properties of the first record of authentication telemetry record.
/// </summary>
public const string AuthInfoTelemetryHeadKey = "auth-info-head";
public const string AuthTelemetryPropertyHeadPrefix = "auth-info-head";

/// <summary>
/// Key of the left records of authentication telemetry.
/// </summary>
public const string AuthInfoTelemetrySubsequentKey = "auth-info-sub";
public const string AuthTelemetryPropertyTailKey = "auth-info-tail";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,7 @@ public interface IAuthTelemetryRecord : IExtensibleModel
/// Authentication process succeed or not.
/// </summary>
bool AuthenticationSuccess { get; set; }

bool correlationId { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

using Microsoft.Rest;
using System;
using System.Collections.Generic;
using System.Security;

namespace Microsoft.Azure.Commands.Common.Authentication.Abstractions
Expand Down Expand Up @@ -99,6 +98,6 @@ IAccessToken Authenticate(
/// <summary>
/// Get the information to be recorded in Telemetry
/// </summary>
AuthenticationTelemetry GetDataForTelemetry();
AuthenticationTelemetry GetDataForTelemetry(string requestId);
}
}
8 changes: 7 additions & 1 deletion src/Common/AzurePSCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

using Microsoft.Azure.Commands.Common.Authentication;
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
using Microsoft.Azure.Commands.Common.Authentication.Utilities;
using Microsoft.Azure.PowerShell.Common.Config;
using Microsoft.Azure.PowerShell.Common.Share.Survey;
using Microsoft.Azure.PowerShell.Common.UpgradeNotification;
Expand Down Expand Up @@ -381,6 +382,11 @@ protected override void BeginProcessing()
//Now see if the cmdlet has any Breaking change attributes on it and process them if it does
//This will print any breaking change attribute messages that are applied to the cmdlet
WriteBreakingChangeOrPreviewMessage();

if (AzureSession.Instance.TryGetComponent(nameof(ThreadCmdldetMap), out ThreadCmdldetMap threadCmdletMap))
{
threadCmdletMap.PushCmdletId(this._clientRequestId);
}
}

private void WriteBreakingChangeOrPreviewMessage()
Expand Down Expand Up @@ -843,7 +849,7 @@ protected void LogQosEvent()

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

if (!IsUsageMetricEnabled && (!IsErrorMetricEnabled || _qosEvent.IsSuccess))
{
Expand Down
8 changes: 4 additions & 4 deletions src/Common/MetricHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,15 +295,15 @@ public void SetPSHost(PSHost host)
private static void PopulateAuthenticationPropertiesFromQos(AuthenticationTelemetry telemetry, IDictionary<string, string> eventProperties)
{
var record = telemetry.Head;
eventProperties[$"{AuthTelemetryRecord.AuthInfoTelemetryHeadKey}-{nameof(record.TokenCredentialName).ToLower()}"] = record.TokenCredentialName;
eventProperties[$"{AuthTelemetryRecord.AuthInfoTelemetryHeadKey}-{nameof(record.AuthenticationSuccess).ToLower()}"] = record.AuthenticationSuccess.ToString();
eventProperties[$"{AuthTelemetryRecord.AuthTelemetryPropertyHeadPrefix}-{nameof(record.TokenCredentialName).ToLower()}"] = record.TokenCredentialName;
eventProperties[$"{AuthTelemetryRecord.AuthTelemetryPropertyHeadPrefix}-{nameof(record.AuthenticationSuccess).ToLower()}"] = record.AuthenticationSuccess.ToString();

foreach (var property in record.ExtendedProperties)
{
eventProperties[$"{AuthTelemetryRecord.AuthInfoTelemetryHeadKey}-{property.Key.ToLower()}"] = property.Value;
eventProperties[$"{AuthTelemetryRecord.AuthTelemetryPropertyHeadPrefix}-{property.Key.ToLower()}"] = property.Value;
}

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

private void PopulatePropertiesFromQos(AzurePSQoSEvent qos, IDictionary<string, string> eventProperties, bool populateException = false)
Expand Down
66 changes: 66 additions & 0 deletions src/Common/ThreadCmdldetMap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// ----------------------------------------------------------------------------------
//
// 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.Concurrent;
using System.IO;
using System.Threading;

namespace Microsoft.Azure.Commands.Common.Authentication.Utilities
{
public class ThreadCmdldetMap
{
private ConcurrentDictionary<int, string> map = new ConcurrentDictionary<int, string>();

private static string logFile = Path.Combine(AzureSession.Instance.ProfileDirectory, "threadCmdletMap.log");

private void WriteLog(string log)
{
if (!File.Exists(logFile))
{
using (StreamWriter sw = File.CreateText(logFile))
{
sw.WriteLine(log);
}
}
using (StreamWriter sw = File.AppendText(logFile))
{
sw.WriteLine(log);
}
}

public void PushCmdletId(string cmdletId)
{
#if DEBUG
WriteLog($"[PushCmdletId] ThreadId={Thread.CurrentThread.ManagedThreadId}, CmdletId={cmdletId}");
#endif
map[Thread.CurrentThread.ManagedThreadId] = cmdletId;
}

public string PopCmdletId()
{
var id = Thread.CurrentThread.ManagedThreadId;
if (map.ContainsKey(id))
{
map.TryRemove(id, out string cmdletId);
#if DEBUG
WriteLog($"[PopCmdletId] ThreadId={id}, CmdletId={cmdletId}");
#endif
return cmdletId;
}
#if DEBUG
WriteLog($"[PopCmdletId] ThreadId={id}, CmdletId=NotFound");
#endif
return string.Empty;
}
}
}

0 comments on commit 0358f72

Please sign in to comment.