Skip to content

Commit 6e49043

Browse files
committed
Merge pull request #1686 from hovsepm/dev
[#111628930] Added telemetry to PowerShell ARM and ASM cmdlets.
2 parents 1fc95aa + 1da8425 commit 6e49043

File tree

5 files changed

+250
-81
lines changed

5 files changed

+250
-81
lines changed

src/Common/Commands.Common/AzurePSCmdlet.cs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
using System.Linq;
2929
using System.Threading;
3030
using Microsoft.Rest;
31+
using Microsoft.ApplicationInsights;
3132

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

4748
protected static AzurePSDataCollectionProfile _dataCollectionProfile = null;
4849
protected static string _errorRecordFolderPath = null;
49-
protected const string _fileTimeStampSuffixFormat = "yyyy-MM-dd-THH-mm-ss-fff";
50+
protected static string _sessionId = Guid.NewGuid().ToString();
51+
protected const string _fileTimeStampSuffixFormat = "yyyy-MM-dd-THH-mm-ss-fff";
52+
protected string _clientRequestId = Guid.NewGuid().ToString();
53+
protected MetricHelper _metricHelper;
5054

5155
protected AzurePSQoSEvent QosEvent;
5256

5357
protected virtual bool IsUsageMetricEnabled {
54-
get { return false; }
58+
get { return true; }
5559
}
5660

5761
protected virtual bool IsErrorMetricEnabled
@@ -61,7 +65,7 @@ protected virtual bool IsErrorMetricEnabled
6165

6266
/// <summary>
6367
/// Gets the PowerShell module name used for user agent header.
64-
/// By default uses "Azurepowershell"
68+
/// By default uses "Azure PowerShell"
6569
/// </summary>
6670
protected virtual string ModuleName { get { return "AzurePowershell"; } }
6771

@@ -81,6 +85,13 @@ protected virtual bool IsErrorMetricEnabled
8185
public AzurePSCmdlet()
8286
{
8387
_debugMessages = new ConcurrentQueue<string>();
88+
89+
//TODO: Inject from CI server
90+
_metricHelper = new MetricHelper();
91+
_metricHelper.AddTelemetryClient(new TelemetryClient
92+
{
93+
InstrumentationKey = "7df6ff70-8353-4672-80d6-568517fed090"
94+
});
8495
}
8596

8697
/// <summary>
@@ -228,7 +239,7 @@ protected override void BeginProcessing()
228239
ProductInfoHeaderValue userAgentValue = new ProductInfoHeaderValue(
229240
ModuleName, string.Format("v{0}", ModuleVersion));
230241
AzureSession.ClientFactory.UserAgents.Add(userAgentValue);
231-
AzureSession.ClientFactory.AddHandler(new CmdletInfoHandler(this.CommandRuntime.ToString(), this.ParameterSetName));
242+
AzureSession.ClientFactory.AddHandler(new CmdletInfoHandler(this.CommandRuntime.ToString(), this.ParameterSetName, this._clientRequestId));
232243
base.BeginProcessing();
233244
}
234245

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

254265
protected string CurrentPath()
255266
{
256-
// SessionState is only available within Powershell so default to
267+
// SessionState is only available within PowerShell so default to
257268
// the CurrentDirectory when being run from tests.
258269
return (SessionState != null) ?
259270
SessionState.Path.CurrentLocation.Path :
@@ -273,7 +284,6 @@ protected bool IsVerbose()
273284
{
274285
QosEvent.Exception = errorRecord.Exception;
275286
QosEvent.IsSuccess = false;
276-
LogQosEvent(true);
277287
}
278288

279289
base.WriteError(errorRecord);
@@ -438,7 +448,7 @@ private void RecordDebugMessages()
438448
/// <summary>
439449
/// Invoke this method when the cmdlet is completed or terminated.
440450
/// </summary>
441-
protected void LogQosEvent(bool waitForMetricSending = false)
451+
protected void LogQosEvent()
442452
{
443453
if (QosEvent == null)
444454
{
@@ -461,8 +471,8 @@ protected void LogQosEvent(bool waitForMetricSending = false)
461471

462472
try
463473
{
464-
MetricHelper.LogQoSEvent(QosEvent, IsUsageMetricEnabled, IsErrorMetricEnabled);
465-
MetricHelper.FlushMetric(waitForMetricSending);
474+
_metricHelper.LogQoSEvent(QosEvent, IsUsageMetricEnabled, IsErrorMetricEnabled);
475+
_metricHelper.FlushMetric();
466476
WriteDebug("Finish sending metric.");
467477
}
468478
catch (Exception e)

src/Common/Commands.Common/CmdletInfoHandler.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,23 @@ public class CmdletInfoHandler : DelegatingHandler, ICloneable
3333
/// The name of the parameter set specified by user.
3434
/// </summary>
3535
public string ParameterSet { get; private set; }
36+
37+
/// <summary>
38+
/// The unique client request id.
39+
/// </summary>
40+
public string ClientRequestId { get; private set; }
3641

3742
/// <summary>
3843
/// Initializes an instance of a CmdletInfoHandler with the name of the cmdlet and the parameter set.
3944
/// </summary>
4045
/// <param name="cmdlet">the name of the cmdlet</param>
4146
/// <param name="parameterSet">the name of the parameter set specified by user</param>
42-
public CmdletInfoHandler(string cmdlet, string parameterSet)
47+
/// <param name="clientRequestId">the unique clientRequestId</param>
48+
public CmdletInfoHandler(string cmdlet, string parameterSet, string clientRequestId)
4349
{
4450
this.Cmdlet = cmdlet;
4551
this.ParameterSet = parameterSet;
52+
this.ClientRequestId = clientRequestId;
4653
}
4754

4855
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
@@ -55,12 +62,20 @@ protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage reques
5562
{
5663
request.Headers.Add("ParameterSetName", ParameterSet);
5764
}
65+
if (ClientRequestId != null)
66+
{
67+
if (request.Headers.Contains("x-ms-client-request-id"))
68+
{
69+
request.Headers.Remove("x-ms-client-request-id");
70+
}
71+
request.Headers.TryAddWithoutValidation("x-ms-client-request-id", ClientRequestId);
72+
}
5873
return base.SendAsync(request, cancellationToken);
5974
}
6075

6176
public object Clone()
6277
{
63-
return new CmdletInfoHandler(this.Cmdlet, this.ParameterSet);
78+
return new CmdletInfoHandler(this.Cmdlet, this.ParameterSet, this.ClientRequestId);
6479
}
6580
}
6681
}

0 commit comments

Comments
 (0)