From 9f1fa26678664b00e6ba1095c36716567ade4166 Mon Sep 17 00:00:00 2001 From: Lei jin Date: Wed, 26 Jun 2024 09:40:28 +0800 Subject: [PATCH] Address review comments --- .../AuthTelemetryRecord.cs | 6 ++- .../Interfaces/IAuthTelemetryRecord.cs | 2 + .../Interfaces/IAuthenticationFactory.cs | 3 +- src/Common/AzurePSCmdlet.cs | 8 +++- src/Common/MetricHelper.cs | 8 ++-- src/Common/ThreadCmdldetMap.cs | 37 +++++++++++++++++++ 6 files changed, 55 insertions(+), 9 deletions(-) create mode 100644 src/Common/ThreadCmdldetMap.cs diff --git a/src/Authentication.Abstractions/AuthTelemetryRecord.cs b/src/Authentication.Abstractions/AuthTelemetryRecord.cs index a45707ad53..34d56c3a3c 100644 --- a/src/Authentication.Abstractions/AuthTelemetryRecord.cs +++ b/src/Authentication.Abstractions/AuthTelemetryRecord.cs @@ -35,6 +35,8 @@ public class AuthTelemetryRecord : IAuthTelemetryRecord /// public bool AuthenticationSuccess { get; set; } = false; + public bool correlationId { get; set; } + /// /// Additional properties for AuthenticationInfo /// @@ -64,11 +66,11 @@ public AuthTelemetryRecord(IAuthTelemetryRecord other, bool? isSuccess = null) /// /// Prefix of properties of the first record of authentication telemetry record. /// - public const string AuthInfoTelemetryHeadKey = "auth-info-head"; + public const string AuthTelemetryPropertyHeadPrefix = "auth-info-head"; /// /// Key of the left records of authentication telemetry. /// - public const string AuthInfoTelemetrySubsequentKey = "auth-info-sub"; + public const string AuthTelemetryPropertyTailKey = "auth-info-tail"; } } diff --git a/src/Authentication.Abstractions/Interfaces/IAuthTelemetryRecord.cs b/src/Authentication.Abstractions/Interfaces/IAuthTelemetryRecord.cs index 5c3b673cb6..d9f75692e9 100644 --- a/src/Authentication.Abstractions/Interfaces/IAuthTelemetryRecord.cs +++ b/src/Authentication.Abstractions/Interfaces/IAuthTelemetryRecord.cs @@ -28,5 +28,7 @@ public interface IAuthTelemetryRecord : IExtensibleModel /// Authentication process succeed or not. /// bool AuthenticationSuccess { get; set; } + + bool correlationId { get; set; } } } diff --git a/src/Authentication.Abstractions/Interfaces/IAuthenticationFactory.cs b/src/Authentication.Abstractions/Interfaces/IAuthenticationFactory.cs index 7c7eeca126..377fc7cef8 100644 --- a/src/Authentication.Abstractions/Interfaces/IAuthenticationFactory.cs +++ b/src/Authentication.Abstractions/Interfaces/IAuthenticationFactory.cs @@ -14,7 +14,6 @@ using Microsoft.Rest; using System; -using System.Collections.Generic; using System.Security; namespace Microsoft.Azure.Commands.Common.Authentication.Abstractions @@ -99,6 +98,6 @@ IAccessToken Authenticate( /// /// Get the information to be recorded in Telemetry /// - AuthenticationTelemetry GetDataForTelemetry(); + AuthenticationTelemetry GetDataForTelemetry(string requestId); } } diff --git a/src/Common/AzurePSCmdlet.cs b/src/Common/AzurePSCmdlet.cs index fb2d13a4b9..534fc7a11d 100644 --- a/src/Common/AzurePSCmdlet.cs +++ b/src/Common/AzurePSCmdlet.cs @@ -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; @@ -380,6 +381,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.PushCurrentCmdlet(this._clientRequestId); + } } private void WriteBreakingChangeOrPreviewMessage() @@ -842,7 +848,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)) { diff --git a/src/Common/MetricHelper.cs b/src/Common/MetricHelper.cs index 4fb9b01b71..6e99e16528 100644 --- a/src/Common/MetricHelper.cs +++ b/src/Common/MetricHelper.cs @@ -295,15 +295,15 @@ public void SetPSHost(PSHost host) private static void PopulateAuthenticationPropertiesFromQos(AuthenticationTelemetry telemetry, IDictionary 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 eventProperties, bool populateException = false) diff --git a/src/Common/ThreadCmdldetMap.cs b/src/Common/ThreadCmdldetMap.cs new file mode 100644 index 0000000000..40e86f7833 --- /dev/null +++ b/src/Common/ThreadCmdldetMap.cs @@ -0,0 +1,37 @@ +// ---------------------------------------------------------------------------------- +// +// 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.Threading; + +namespace Microsoft.Azure.Commands.Common.Authentication.Utilities +{ + public class ThreadCmdldetMap + { + ConcurrentDictionary map = new ConcurrentDictionary(); + public void PushCurrentCmdlet(string cmdletId) + { + map[Thread.CurrentThread.ManagedThreadId] = cmdletId; + } + + public string GetCurrentCmdlet() + { + var id = Thread.CurrentThread.ManagedThreadId; + if (map.ContainsKey(id)) + { + return map[id]; + } + return string.Empty; + } + } +}