Skip to content

Commit 644538a

Browse files
authored
Set UserAgent for ARM telemetry (#428)
* set UserAgent for ARM telemetry * update function to be private
1 parent 55e0739 commit 644538a

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

src/Common/AzurePSCmdlet.cs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,17 @@ protected virtual void SetupHttpClientPipeline()
322322
AzureSession.Instance.ClientFactory.AddUserAgent("AzurePowershell", string.Format("v{0}", AzVersion));
323323
AzureSession.Instance.ClientFactory.AddUserAgent(PSVERSION, string.Format("v{0}", PowerShellVersion));
324324
AzureSession.Instance.ClientFactory.AddUserAgent(ModuleName, this.ModuleVersion);
325+
try {
326+
string hostEnv = AzurePSCmdlet.getEnvUserAgent();
327+
if (!String.IsNullOrWhiteSpace(hostEnv))
328+
{
329+
AzureSession.Instance.ClientFactory.AddUserAgent(hostEnv);
330+
}
331+
}
332+
catch (Exception)
333+
{
334+
// ignore if it failed.
335+
}
325336

326337
AzureSession.Instance.ClientFactory.AddHandler(
327338
new CmdletInfoHandler(this.CommandRuntime.ToString(),
@@ -331,6 +342,18 @@ protected virtual void SetupHttpClientPipeline()
331342

332343
protected virtual void TearDownHttpClientPipeline()
333344
{
345+
try
346+
{
347+
string hostEnv = AzurePSCmdlet.getEnvUserAgent();
348+
if (!String.IsNullOrWhiteSpace(hostEnv))
349+
{
350+
AzureSession.Instance.ClientFactory.RemoveUserAgent(hostEnv);
351+
}
352+
}
353+
catch (Exception)
354+
{
355+
// ignore if it failed.
356+
}
334357
AzureSession.Instance.ClientFactory.RemoveUserAgent(ModuleName);
335358
AzureSession.Instance.ClientFactory.RemoveHandler(typeof(CmdletInfoHandler));
336359
}
@@ -721,14 +744,14 @@ protected virtual void InitializeQosEvent()
721744
if (AzVersion == null)
722745
{
723746
AzVersion = this.LoadModuleVersion("Az", true);
724-
UserAgent = new ProductInfoHeaderValue("AzurePowershell", string.Format("Az{0}", AzVersion)).ToString();
725-
string hostEnv = Environment.GetEnvironmentVariable("AZUREPS_HOST_ENVIRONMENT");
726-
if (!String.IsNullOrWhiteSpace(hostEnv))
727-
UserAgent += string.Format(" {0}", hostEnv.Trim());
728747
PowerShellVersion = this.LoadPowerShellVersion();
729748
PSHostName = this.Host?.Name;
730749
PSHostVersion = this.Host?.Version?.ToString();
731750
}
751+
UserAgent = new ProductInfoHeaderValue("AzurePowershell", string.Format("Az{0}", AzVersion)).ToString();
752+
string hostEnv = AzurePSCmdlet.getEnvUserAgent();
753+
if (!String.IsNullOrWhiteSpace(hostEnv))
754+
UserAgent += string.Format(" {0}", hostEnv);
732755
if (AzAccountsVersion == null)
733756
{
734757
AzAccountsVersion = this.LoadModuleVersion("Az.Accounts", false);
@@ -777,6 +800,19 @@ protected virtual void InitializeQosEvent()
777800
_qosEvent.SanitizerInfo = new SanitizerTelemetry(OutputSanitizer?.RequireSecretsDetection == true);
778801
}
779802

803+
private static string getEnvUserAgent()
804+
{
805+
string hostEnv = Environment.GetEnvironmentVariable("AZUREPS_HOST_ENVIRONMENT");
806+
if (String.IsNullOrWhiteSpace(hostEnv))
807+
{
808+
return null;
809+
}
810+
else
811+
{
812+
return hostEnv.Trim();
813+
}
814+
}
815+
780816
private void RecordDebugMessages()
781817
{
782818
try

src/Common/CmdletInfoHandler.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,18 @@ protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage reques
5757
{
5858
if (Cmdlet != null)
5959
{
60+
if (request.Headers.Contains("CommandName"))
61+
{
62+
request.Headers.Remove("CommandName");
63+
}
6064
request.Headers.Add("CommandName", Cmdlet);
6165
}
6266
if (ParameterSet != null)
6367
{
68+
if (request.Headers.Contains("ParameterSetName"))
69+
{
70+
request.Headers.Remove("ParameterSetName");
71+
}
6472
request.Headers.Add("ParameterSetName", ParameterSet);
6573
}
6674
if (ClientRequestId != null)

0 commit comments

Comments
 (0)