Skip to content

[#111628930] Added telemetry to PowerShell ARM and ASM cmdlets. #1686

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

Merged
merged 2 commits into from
Jan 20, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
28 changes: 19 additions & 9 deletions src/Common/Commands.Common/AzurePSCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
using System.Linq;
using System.Threading;
using Microsoft.Rest;
using Microsoft.ApplicationInsights;

namespace Microsoft.WindowsAzure.Commands.Utilities.Common
{
Expand All @@ -46,12 +47,15 @@ public abstract class AzurePSCmdlet : PSCmdlet, IDisposable

protected static AzurePSDataCollectionProfile _dataCollectionProfile = null;
protected static string _errorRecordFolderPath = null;
protected const string _fileTimeStampSuffixFormat = "yyyy-MM-dd-THH-mm-ss-fff";
protected static string _sessionId = Guid.NewGuid().ToString();
protected const string _fileTimeStampSuffixFormat = "yyyy-MM-dd-THH-mm-ss-fff";
protected string _clientRequestId = Guid.NewGuid().ToString();
protected MetricHelper _metricHelper;

protected AzurePSQoSEvent QosEvent;

protected virtual bool IsUsageMetricEnabled {
get { return false; }
get { return true; }
}

protected virtual bool IsErrorMetricEnabled
Expand All @@ -61,7 +65,7 @@ protected virtual bool IsErrorMetricEnabled

/// <summary>
/// Gets the PowerShell module name used for user agent header.
/// By default uses "Azurepowershell"
/// By default uses "Azure PowerShell"
/// </summary>
protected virtual string ModuleName { get { return "AzurePowershell"; } }

Expand All @@ -81,6 +85,13 @@ protected virtual bool IsErrorMetricEnabled
public AzurePSCmdlet()
{
_debugMessages = new ConcurrentQueue<string>();

//TODO: Inject from CI server
_metricHelper = new MetricHelper();
_metricHelper.AddTelemetryClient(new TelemetryClient
{
InstrumentationKey = "7df6ff70-8353-4672-80d6-568517fed090"
});
}

/// <summary>
Expand Down Expand Up @@ -228,7 +239,7 @@ protected override void BeginProcessing()
ProductInfoHeaderValue userAgentValue = new ProductInfoHeaderValue(
ModuleName, string.Format("v{0}", ModuleVersion));
AzureSession.ClientFactory.UserAgents.Add(userAgentValue);
AzureSession.ClientFactory.AddHandler(new CmdletInfoHandler(this.CommandRuntime.ToString(), this.ParameterSetName));
AzureSession.ClientFactory.AddHandler(new CmdletInfoHandler(this.CommandRuntime.ToString(), this.ParameterSetName, this._clientRequestId));
base.BeginProcessing();
}

Expand All @@ -253,7 +264,7 @@ protected override void EndProcessing()

protected string CurrentPath()
{
// SessionState is only available within Powershell so default to
// SessionState is only available within PowerShell so default to
// the CurrentDirectory when being run from tests.
return (SessionState != null) ?
SessionState.Path.CurrentLocation.Path :
Expand All @@ -273,7 +284,6 @@ protected bool IsVerbose()
{
QosEvent.Exception = errorRecord.Exception;
QosEvent.IsSuccess = false;
LogQosEvent(true);
}

base.WriteError(errorRecord);
Expand Down Expand Up @@ -438,7 +448,7 @@ private void RecordDebugMessages()
/// <summary>
/// Invoke this method when the cmdlet is completed or terminated.
/// </summary>
protected void LogQosEvent(bool waitForMetricSending = false)
protected void LogQosEvent()
{
if (QosEvent == null)
{
Expand All @@ -461,8 +471,8 @@ protected void LogQosEvent(bool waitForMetricSending = false)

try
{
MetricHelper.LogQoSEvent(QosEvent, IsUsageMetricEnabled, IsErrorMetricEnabled);
MetricHelper.FlushMetric(waitForMetricSending);
_metricHelper.LogQoSEvent(QosEvent, IsUsageMetricEnabled, IsErrorMetricEnabled);
_metricHelper.FlushMetric();
WriteDebug("Finish sending metric.");
}
catch (Exception e)
Expand Down
19 changes: 17 additions & 2 deletions src/Common/Commands.Common/CmdletInfoHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,23 @@ public class CmdletInfoHandler : DelegatingHandler, ICloneable
/// The name of the parameter set specified by user.
/// </summary>
public string ParameterSet { get; private set; }

/// <summary>
/// The unique client request id.
/// </summary>
public string ClientRequestId { get; private set; }

/// <summary>
/// Initializes an instance of a CmdletInfoHandler with the name of the cmdlet and the parameter set.
/// </summary>
/// <param name="cmdlet">the name of the cmdlet</param>
/// <param name="parameterSet">the name of the parameter set specified by user</param>
public CmdletInfoHandler(string cmdlet, string parameterSet)
/// <param name="clientRequestId">the unique clientRequestId</param>
public CmdletInfoHandler(string cmdlet, string parameterSet, string clientRequestId)
{
this.Cmdlet = cmdlet;
this.ParameterSet = parameterSet;
this.ClientRequestId = clientRequestId;
}

protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
Expand All @@ -55,12 +62,20 @@ protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage reques
{
request.Headers.Add("ParameterSetName", ParameterSet);
}
if (ClientRequestId != null)
{
if (request.Headers.Contains("x-ms-client-request-id"))
{
request.Headers.Remove("x-ms-client-request-id");
}
request.Headers.TryAddWithoutValidation("x-ms-client-request-id", ClientRequestId);
}
return base.SendAsync(request, cancellationToken);
}

public object Clone()
{
return new CmdletInfoHandler(this.Cmdlet, this.ParameterSet);
return new CmdletInfoHandler(this.Cmdlet, this.ParameterSet, this.ClientRequestId);
}
}
}
Loading