Skip to content

Added AppInsights telemetry (#110446326) #1624

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 6 commits into from
Jan 13, 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
22 changes: 16 additions & 6 deletions src/CLU/Commands.Common/AzurePSCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
using System.Linq;
using System.Threading;
using Microsoft.Azure.Commands.Common.Authentication.Factories;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.Rest;

namespace Microsoft.Azure.Commands.Utilities.Common
Expand All @@ -45,7 +47,7 @@ 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 string _clientRequestId = Guid.NewGuid().ToString();
public IClientFactory ClientFactory { get; set; }

public IAuthenticationFactory AuthenticationFactory { get; set; }
Expand All @@ -55,7 +57,7 @@ public abstract class AzurePSCmdlet : PSCmdlet, IDisposable
protected AzurePSQoSEvent QosEvent;

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

protected virtual bool IsErrorMetricEnabled
Expand Down Expand Up @@ -85,6 +87,11 @@ protected virtual bool IsErrorMetricEnabled
public AzurePSCmdlet()
{
_debugMessages = new ConcurrentQueue<string>();
//TODO: Inject from CI server
MetricHelper.AddTelemetryClient(new TelemetryClient
{
InstrumentationKey = "963c4276-ec20-48ad-b9ab-3968e9da5578"
});
#if DEBUG
if (!TestMockSupport.RunningMocked)
{
Expand Down Expand Up @@ -214,6 +221,9 @@ protected static AzurePSDataCollectionProfile GetDataCollectionProfile()
/// <returns>true if allowed</returns>
public static bool IsDataCollectionAllowed()
{
//TODO: CLU - remove before final release
return true;

if (_dataCollectionProfile != null &&
_dataCollectionProfile.EnableAzureDataCollection.HasValue &&
_dataCollectionProfile.EnableAzureDataCollection.Value)
Expand Down Expand Up @@ -299,7 +309,7 @@ protected override void BeginProcessing()
ProductInfoHeaderValue userAgentValue = new ProductInfoHeaderValue(
ModuleName, string.Format("v{0}", ModuleVersion));
ClientFactory.UserAgents.Add(userAgentValue);
ClientFactory.AddHandler(new CmdletInfoHandler(this.CommandRuntime.ToString(), this.ParameterSetName));
ClientFactory.AddHandler(new CmdletInfoHandler(this.CommandRuntime.ToString(), this.ParameterSetName, this._clientRequestId));
ServiceClientTracing.AddTracingInterceptor(_adalListener);
ServiceClientTracing.IsEnabled = true;
base.BeginProcessing();
Expand Down Expand Up @@ -347,7 +357,7 @@ protected bool IsVerbose()
{
QosEvent.Exception = errorRecord.Exception;
QosEvent.IsSuccess = false;
LogQosEvent(true);
LogQosEvent();
}

base.WriteError(errorRecord);
Expand Down Expand Up @@ -500,7 +510,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 @@ -524,7 +534,7 @@ protected void LogQosEvent(bool waitForMetricSending = false)
try
{
MetricHelper.LogQoSEvent(QosEvent, IsUsageMetricEnabled, IsErrorMetricEnabled);
MetricHelper.FlushMetric(waitForMetricSending);
MetricHelper.FlushMetric();
WriteDebug("Finish sending metric.");
}
catch (Exception e)
Expand Down
2 changes: 1 addition & 1 deletion src/CLU/Commands.Common/AzurePowerShell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class AzurePowerShell
public const string DataStoreVariable = "_azpsh_data_store";

public static ProductInfoHeaderValue UserAgentValue = new ProductInfoHeaderValue(
"AzurePowershell",
"CLU",
string.Format("v{0}", AzurePowerShell.AssemblyVersion));

public static String ProfileDirectory = Directory.GetCurrentDirectory();
Expand Down
19 changes: 15 additions & 4 deletions src/CLU/Commands.Common/CmdletInfoHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,33 +34,44 @@ public class CmdletInfoHandler : DelegatingHandler
/// </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)
{
if (Cmdlet != null)
{
request.Headers.Add("CommandName", Cmdlet);
request.Headers.TryAddWithoutValidation("CommandName", Cmdlet);
}
if (ParameterSet != null)
{
request.Headers.Add("ParameterSetName", ParameterSet);
request.Headers.TryAddWithoutValidation("ParameterSetName", ParameterSet);
}
if (ClientRequestId != null)
{
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