Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set UserAgent for ARM telemetry #428

Merged
merged 3 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 40 additions & 4 deletions src/Common/AzurePSCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are try-catch AddUserAgent(), does it make sense to wrap all AddUserAgent() calls in it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are try-catch AddUserAgent(), does it make sense to wrap all AddUserAgent() calls in it?

Here is AzureSession.Instance.ClientFactory.AddUserAgent(hostEnv);. Below is ClientFactory.RemoveUserAgent(hostEnv);. The Client part is UserAgent += string.Format(" {0}", hostEnv); I can't merge them together.

{
// ignore if it failed.
}

AzureSession.Instance.ClientFactory.AddHandler(
new CmdletInfoHandler(this.CommandRuntime.ToString(),
Expand All @@ -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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I add a catch here just in case the env var is not OK for setting UserAgent, we'll ignore it.

{
// ignore if it failed.
}
AzureSession.Instance.ClientFactory.RemoveUserAgent(ModuleName);
AzureSession.Instance.ClientFactory.RemoveHandler(typeof(CmdletInfoHandler));
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/Common/CmdletInfoHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,18 @@ protected override Task<HttpResponseMessage> 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)
Expand Down