From 55e0739f94e1b9f514fda92cbef057a1ca0ff268 Mon Sep 17 00:00:00 2001 From: Yan Xu Date: Thu, 22 Aug 2024 09:56:28 +0800 Subject: [PATCH 1/3] set subscription id and tenant id in telemetry if it's null (#427) * set subscription id and tenant id in telemetry if it's null * set telemetry sub id and tenant id to AzureRMCmdlet.EndProcessing * remove RequireDefaultContext --- .../Version2016_09_01/AzureRMCmdlet.cs | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/ResourceManager/Version2016_09_01/AzureRMCmdlet.cs b/src/ResourceManager/Version2016_09_01/AzureRMCmdlet.cs index 52e42b2209..cbba3c5a9c 100644 --- a/src/ResourceManager/Version2016_09_01/AzureRMCmdlet.cs +++ b/src/ResourceManager/Version2016_09_01/AzureRMCmdlet.cs @@ -281,6 +281,22 @@ public virtual bool TryGetDefaultContext(out IAzureContext context) return result; } + protected override void EndProcessing() + { + IAzureContext context; + _qosEvent.Uid = "defaultid"; + if (TryGetDefaultContext(out context) && context != null) + { + _qosEvent.SubscriptionId = context.Subscription?.Id; + _qosEvent.TenantId = context.Tenant?.Id; + if (context.Account != null && !String.IsNullOrWhiteSpace(context.Account.Id)) + { + _qosEvent.Uid = MetricHelper.GenerateSha256HashString(context.Account.Id.ToString()); + } + } + base.EndProcessing(); + } + /// /// Gets the current default context. /// @@ -380,17 +396,7 @@ protected override void InitializeQosEvent() { base.InitializeQosEvent(); - IAzureContext context; - _qosEvent.Uid = "defaultid"; - if (RequireDefaultContext() && TryGetDefaultContext(out context)) - { - _qosEvent.SubscriptionId = context.Subscription?.Id; - _qosEvent.TenantId = context.Tenant?.Id; - if (context.Account != null && !String.IsNullOrWhiteSpace(context.Account.Id)) - { - _qosEvent.Uid = MetricHelper.GenerateSha256HashString(context.Account.Id.ToString()); - } - } + } protected override void LogCmdletStartInvocationInfo() From 644538a585b1cc6b6a19bd86882655cbf1dbd617 Mon Sep 17 00:00:00 2001 From: Yan Xu Date: Fri, 23 Aug 2024 11:05:48 +0800 Subject: [PATCH 2/3] Set UserAgent for ARM telemetry (#428) * set UserAgent for ARM telemetry * update function to be private --- src/Common/AzurePSCmdlet.cs | 44 ++++++++++++++++++++++++++++++--- src/Common/CmdletInfoHandler.cs | 8 ++++++ 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/src/Common/AzurePSCmdlet.cs b/src/Common/AzurePSCmdlet.cs index 482d78e5a7..050d5e0fbe 100644 --- a/src/Common/AzurePSCmdlet.cs +++ b/src/Common/AzurePSCmdlet.cs @@ -322,6 +322,17 @@ protected virtual void SetupHttpClientPipeline() AzureSession.Instance.ClientFactory.AddUserAgent("AzurePowershell", string.Format("v{0}", AzVersion)); AzureSession.Instance.ClientFactory.AddUserAgent(PSVERSION, string.Format("v{0}", PowerShellVersion)); AzureSession.Instance.ClientFactory.AddUserAgent(ModuleName, this.ModuleVersion); + try { + string hostEnv = AzurePSCmdlet.getEnvUserAgent(); + if (!String.IsNullOrWhiteSpace(hostEnv)) + { + AzureSession.Instance.ClientFactory.AddUserAgent(hostEnv); + } + } + catch (Exception) + { + // ignore if it failed. + } AzureSession.Instance.ClientFactory.AddHandler( new CmdletInfoHandler(this.CommandRuntime.ToString(), @@ -331,6 +342,18 @@ protected virtual void SetupHttpClientPipeline() protected virtual void TearDownHttpClientPipeline() { + try + { + string hostEnv = AzurePSCmdlet.getEnvUserAgent(); + if (!String.IsNullOrWhiteSpace(hostEnv)) + { + AzureSession.Instance.ClientFactory.RemoveUserAgent(hostEnv); + } + } + catch (Exception) + { + // ignore if it failed. + } AzureSession.Instance.ClientFactory.RemoveUserAgent(ModuleName); AzureSession.Instance.ClientFactory.RemoveHandler(typeof(CmdletInfoHandler)); } @@ -721,14 +744,14 @@ protected virtual void InitializeQosEvent() if (AzVersion == null) { AzVersion = this.LoadModuleVersion("Az", true); - UserAgent = new ProductInfoHeaderValue("AzurePowershell", string.Format("Az{0}", AzVersion)).ToString(); - string hostEnv = Environment.GetEnvironmentVariable("AZUREPS_HOST_ENVIRONMENT"); - if (!String.IsNullOrWhiteSpace(hostEnv)) - UserAgent += string.Format(" {0}", hostEnv.Trim()); PowerShellVersion = this.LoadPowerShellVersion(); PSHostName = this.Host?.Name; PSHostVersion = this.Host?.Version?.ToString(); } + UserAgent = new ProductInfoHeaderValue("AzurePowershell", string.Format("Az{0}", AzVersion)).ToString(); + string hostEnv = AzurePSCmdlet.getEnvUserAgent(); + if (!String.IsNullOrWhiteSpace(hostEnv)) + UserAgent += string.Format(" {0}", hostEnv); if (AzAccountsVersion == null) { AzAccountsVersion = this.LoadModuleVersion("Az.Accounts", false); @@ -777,6 +800,19 @@ protected virtual void InitializeQosEvent() _qosEvent.SanitizerInfo = new SanitizerTelemetry(OutputSanitizer?.RequireSecretsDetection == true); } + private static string getEnvUserAgent() + { + string hostEnv = Environment.GetEnvironmentVariable("AZUREPS_HOST_ENVIRONMENT"); + if (String.IsNullOrWhiteSpace(hostEnv)) + { + return null; + } + else + { + return hostEnv.Trim(); + } + } + private void RecordDebugMessages() { try diff --git a/src/Common/CmdletInfoHandler.cs b/src/Common/CmdletInfoHandler.cs index 18002d432e..0fe5f0c78c 100644 --- a/src/Common/CmdletInfoHandler.cs +++ b/src/Common/CmdletInfoHandler.cs @@ -57,10 +57,18 @@ protected override Task SendAsync(HttpRequestMessage reques { if (Cmdlet != null) { + if (request.Headers.Contains("CommandName")) + { + request.Headers.Remove("CommandName"); + } request.Headers.Add("CommandName", Cmdlet); } if (ParameterSet != null) { + if (request.Headers.Contains("ParameterSetName")) + { + request.Headers.Remove("ParameterSetName"); + } request.Headers.Add("ParameterSetName", ParameterSet); } if (ClientRequestId != null) From 061407ff9f8ddadfe435bfd03c925f5f72dcf7ec Mon Sep 17 00:00:00 2001 From: Yan Xu Date: Fri, 23 Aug 2024 14:28:22 +0800 Subject: [PATCH 3/3] replace invalid char in customized UserAgent (#429) --- src/Common/AzurePSCmdlet.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Common/AzurePSCmdlet.cs b/src/Common/AzurePSCmdlet.cs index 050d5e0fbe..6bd9b90eae 100644 --- a/src/Common/AzurePSCmdlet.cs +++ b/src/Common/AzurePSCmdlet.cs @@ -809,7 +809,7 @@ private static string getEnvUserAgent() } else { - return hostEnv.Trim(); + return hostEnv.Trim().Replace("@", "_").Replace("/", "_"); } }