28
28
using System . Linq ;
29
29
using System . Threading ;
30
30
using Microsoft . Rest ;
31
+ using Microsoft . ApplicationInsights ;
31
32
32
33
namespace Microsoft . WindowsAzure . Commands . Utilities . Common
33
34
{
@@ -46,12 +47,15 @@ public abstract class AzurePSCmdlet : PSCmdlet, IDisposable
46
47
47
48
protected static AzurePSDataCollectionProfile _dataCollectionProfile = null ;
48
49
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 ;
50
54
51
55
protected AzurePSQoSEvent QosEvent ;
52
56
53
57
protected virtual bool IsUsageMetricEnabled {
54
- get { return false ; }
58
+ get { return true ; }
55
59
}
56
60
57
61
protected virtual bool IsErrorMetricEnabled
@@ -61,7 +65,7 @@ protected virtual bool IsErrorMetricEnabled
61
65
62
66
/// <summary>
63
67
/// Gets the PowerShell module name used for user agent header.
64
- /// By default uses "Azurepowershell "
68
+ /// By default uses "Azure PowerShell "
65
69
/// </summary>
66
70
protected virtual string ModuleName { get { return "AzurePowershell" ; } }
67
71
@@ -81,6 +85,13 @@ protected virtual bool IsErrorMetricEnabled
81
85
public AzurePSCmdlet ( )
82
86
{
83
87
_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
+ } ) ;
84
95
}
85
96
86
97
/// <summary>
@@ -228,7 +239,7 @@ protected override void BeginProcessing()
228
239
ProductInfoHeaderValue userAgentValue = new ProductInfoHeaderValue (
229
240
ModuleName , string . Format ( "v{0}" , ModuleVersion ) ) ;
230
241
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 ) ) ;
232
243
base . BeginProcessing ( ) ;
233
244
}
234
245
@@ -253,7 +264,7 @@ protected override void EndProcessing()
253
264
254
265
protected string CurrentPath ( )
255
266
{
256
- // SessionState is only available within Powershell so default to
267
+ // SessionState is only available within PowerShell so default to
257
268
// the CurrentDirectory when being run from tests.
258
269
return ( SessionState != null ) ?
259
270
SessionState . Path . CurrentLocation . Path :
@@ -273,7 +284,6 @@ protected bool IsVerbose()
273
284
{
274
285
QosEvent . Exception = errorRecord . Exception ;
275
286
QosEvent . IsSuccess = false ;
276
- LogQosEvent ( true ) ;
277
287
}
278
288
279
289
base . WriteError ( errorRecord ) ;
@@ -438,7 +448,7 @@ private void RecordDebugMessages()
438
448
/// <summary>
439
449
/// Invoke this method when the cmdlet is completed or terminated.
440
450
/// </summary>
441
- protected void LogQosEvent ( bool waitForMetricSending = false )
451
+ protected void LogQosEvent ( )
442
452
{
443
453
if ( QosEvent == null )
444
454
{
@@ -461,8 +471,8 @@ protected void LogQosEvent(bool waitForMetricSending = false)
461
471
462
472
try
463
473
{
464
- MetricHelper . LogQoSEvent ( QosEvent , IsUsageMetricEnabled , IsErrorMetricEnabled ) ;
465
- MetricHelper . FlushMetric ( waitForMetricSending ) ;
474
+ _metricHelper . LogQoSEvent ( QosEvent , IsUsageMetricEnabled , IsErrorMetricEnabled ) ;
475
+ _metricHelper . FlushMetric ( ) ;
466
476
WriteDebug ( "Finish sending metric." ) ;
467
477
}
468
478
catch ( Exception e )
0 commit comments