Skip to content

Commit 9922ce3

Browse files
author
Hovsep
committed
Merge pull request Azure#1624 from stankovski/clu
Added AppInsights telemetry (#110446326)
2 parents 718fcf2 + 5f55c08 commit 9922ce3

File tree

8 files changed

+223
-76
lines changed

8 files changed

+223
-76
lines changed

src/CLU/Commands.Common/AzurePSCmdlet.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
using System.Linq;
3030
using System.Threading;
3131
using Microsoft.Azure.Commands.Common.Authentication.Factories;
32+
using Microsoft.ApplicationInsights;
33+
using Microsoft.ApplicationInsights.Extensibility;
3234
using Microsoft.Rest;
3335

3436
namespace Microsoft.Azure.Commands.Utilities.Common
@@ -45,7 +47,7 @@ public abstract class AzurePSCmdlet : PSCmdlet, IDisposable
4547
protected static AzurePSDataCollectionProfile _dataCollectionProfile = null;
4648
protected static string _errorRecordFolderPath = null;
4749
protected const string _fileTimeStampSuffixFormat = "yyyy-MM-dd-THH-mm-ss-fff";
48-
50+
protected string _clientRequestId = Guid.NewGuid().ToString();
4951
public IClientFactory ClientFactory { get; set; }
5052

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

5759
protected virtual bool IsUsageMetricEnabled {
58-
get { return false; }
60+
get { return true; }
5961
}
6062

6163
protected virtual bool IsErrorMetricEnabled
@@ -85,6 +87,11 @@ protected virtual bool IsErrorMetricEnabled
8587
public AzurePSCmdlet()
8688
{
8789
_debugMessages = new ConcurrentQueue<string>();
90+
//TODO: Inject from CI server
91+
MetricHelper.AddTelemetryClient(new TelemetryClient
92+
{
93+
InstrumentationKey = "963c4276-ec20-48ad-b9ab-3968e9da5578"
94+
});
8895
#if DEBUG
8996
if (!TestMockSupport.RunningMocked)
9097
{
@@ -214,6 +221,9 @@ protected static AzurePSDataCollectionProfile GetDataCollectionProfile()
214221
/// <returns>true if allowed</returns>
215222
public static bool IsDataCollectionAllowed()
216223
{
224+
//TODO: CLU - remove before final release
225+
return true;
226+
217227
if (_dataCollectionProfile != null &&
218228
_dataCollectionProfile.EnableAzureDataCollection.HasValue &&
219229
_dataCollectionProfile.EnableAzureDataCollection.Value)
@@ -299,7 +309,7 @@ protected override void BeginProcessing()
299309
ProductInfoHeaderValue userAgentValue = new ProductInfoHeaderValue(
300310
ModuleName, string.Format("v{0}", ModuleVersion));
301311
ClientFactory.UserAgents.Add(userAgentValue);
302-
ClientFactory.AddHandler(new CmdletInfoHandler(this.CommandRuntime.ToString(), this.ParameterSetName));
312+
ClientFactory.AddHandler(new CmdletInfoHandler(this.CommandRuntime.ToString(), this.ParameterSetName, this._clientRequestId));
303313
ServiceClientTracing.AddTracingInterceptor(_adalListener);
304314
ServiceClientTracing.IsEnabled = true;
305315
base.BeginProcessing();
@@ -347,7 +357,7 @@ protected bool IsVerbose()
347357
{
348358
QosEvent.Exception = errorRecord.Exception;
349359
QosEvent.IsSuccess = false;
350-
LogQosEvent(true);
360+
LogQosEvent();
351361
}
352362

353363
base.WriteError(errorRecord);
@@ -500,7 +510,7 @@ private void RecordDebugMessages()
500510
/// <summary>
501511
/// Invoke this method when the cmdlet is completed or terminated.
502512
/// </summary>
503-
protected void LogQosEvent(bool waitForMetricSending = false)
513+
protected void LogQosEvent()
504514
{
505515
if (QosEvent == null)
506516
{
@@ -524,7 +534,7 @@ protected void LogQosEvent(bool waitForMetricSending = false)
524534
try
525535
{
526536
MetricHelper.LogQoSEvent(QosEvent, IsUsageMetricEnabled, IsErrorMetricEnabled);
527-
MetricHelper.FlushMetric(waitForMetricSending);
537+
MetricHelper.FlushMetric();
528538
WriteDebug("Finish sending metric.");
529539
}
530540
catch (Exception e)

src/CLU/Commands.Common/AzurePowerShell.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class AzurePowerShell
4545
public const string DataStoreVariable = "_azpsh_data_store";
4646

4747
public static ProductInfoHeaderValue UserAgentValue = new ProductInfoHeaderValue(
48-
"AzurePowershell",
48+
"CLU",
4949
string.Format("v{0}", AzurePowerShell.AssemblyVersion));
5050

5151
public static String ProfileDirectory = Directory.GetCurrentDirectory();

src/CLU/Commands.Common/CmdletInfoHandler.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,33 +34,44 @@ public class CmdletInfoHandler : DelegatingHandler
3434
/// </summary>
3535
public string ParameterSet { get; private set; }
3636

37+
/// <summary>
38+
/// The unique client request id.
39+
/// </summary>
40+
public string ClientRequestId { get; private set; }
41+
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)
4956
{
5057
if (Cmdlet != null)
5158
{
52-
request.Headers.Add("CommandName", Cmdlet);
59+
request.Headers.TryAddWithoutValidation("CommandName", Cmdlet);
5360
}
5461
if (ParameterSet != null)
5562
{
56-
request.Headers.Add("ParameterSetName", ParameterSet);
63+
request.Headers.TryAddWithoutValidation("ParameterSetName", ParameterSet);
64+
}
65+
if (ClientRequestId != null)
66+
{
67+
request.Headers.TryAddWithoutValidation("x-ms-client-request-id", ClientRequestId);
5768
}
5869
return base.SendAsync(request, cancellationToken);
5970
}
6071

6172
public object Clone()
6273
{
63-
return new CmdletInfoHandler(this.Cmdlet, this.ParameterSet);
74+
return new CmdletInfoHandler(this.Cmdlet, this.ParameterSet, this.ClientRequestId);
6475
}
6576
}
6677
}

0 commit comments

Comments
 (0)