Skip to content

Commit 7b3ac7d

Browse files
committed
Add AzureQoSEvent
1 parent 7360d6b commit 7b3ac7d

File tree

3 files changed

+98
-54
lines changed

3 files changed

+98
-54
lines changed

src/Common/Commands.Common/AzurePSCmdlet.cs

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public abstract class AzurePSCmdlet : PSCmdlet
3535
private DebugStreamTraceListener _adalListener;
3636
protected static AzureProfile _currentProfile = null;
3737
protected static AzurePSDataCollectionProfile _dataCollectionProfile = null;
38+
39+
protected AzurePSQoSEvent QosEvent;
40+
3841
protected virtual bool IsMetricEnabled {
3942
get { return false; }
4043
}
@@ -308,7 +311,7 @@ protected override void BeginProcessing()
308311
{
309312
InitializeProfile();
310313
PromptForDataCollectionProfileIfNotExists();
311-
LogMetricHelperUsage();
314+
InitializeQosEvent();
312315
if (string.IsNullOrEmpty(ParameterSetName))
313316
{
314317
WriteDebugWithTimestamp(string.Format(Resources.BeginProcessingWithoutParameterSetLog, this.GetType().Name));
@@ -349,16 +352,13 @@ protected virtual void InitializeProfile()
349352
/// </summary>
350353
protected override void EndProcessing()
351354
{
355+
LogQosEvent();
352356
string message = string.Format(Resources.EndProcessingLog, this.GetType().Name);
353357
WriteDebugWithTimestamp(message);
354358

355359
RecordingTracingInterceptor.RemoveFromContext(_httpTracingInterceptor);
356360
DebugStreamTraceListener.RemoveAdalTracing(_adalListener);
357361
FlushDebugMessages();
358-
if (IsMetricEnabled)
359-
{
360-
MetricHelper.FlushMetric();
361-
}
362362

363363
base.EndProcessing();
364364
}
@@ -386,7 +386,9 @@ protected bool IsVerbose()
386386
public new void WriteError(ErrorRecord errorRecord)
387387
{
388388
FlushDebugMessages();
389-
LogMetricHelperErrorEvent(errorRecord);
389+
QosEvent.Exception = errorRecord.Exception;
390+
QosEvent.IsSuccess = false;
391+
LogQosEvent(true);
390392
base.WriteError(errorRecord);
391393
}
392394

@@ -514,34 +516,32 @@ private void FlushDebugMessages()
514516
}
515517
}
516518

517-
protected void LogMetricHelperUsage()
519+
protected void InitializeQosEvent()
518520
{
519-
if (!IsMetricEnabled)
520-
{
521-
return;
522-
}
523-
524-
try
521+
QosEvent = new AzurePSQoSEvent()
525522
{
526-
MetricHelper.LogUsageEvent(this.Profile.DefaultSubscription.Id.ToString(), this.GetType().Name);
527-
}
528-
catch (Exception e)
529-
{
530-
//Swallow error from Application Insights event collection.
531-
WriteErrorWithTimestamp(e.ToString());
532-
}
523+
CmdletType = this.GetType().Name,
524+
IsSuccess = true,
525+
UID = MetricHelper.GenerateSha256HashString(this.Profile.DefaultSubscription.Id.ToString())
526+
};
533527
}
534528

535-
protected void LogMetricHelperErrorEvent(ErrorRecord er)
529+
/// <summary>
530+
/// Invoke this method when the cmdlet is completed or terminated.
531+
/// </summary>
532+
protected void LogQosEvent(bool waitForMetricSending = false)
536533
{
534+
QosEvent.FinishQosEvent();
535+
WriteVerbose(QosEvent.ToString());
537536
if (!IsMetricEnabled)
538537
{
539538
return;
540539
}
541540

542541
try
543542
{
544-
MetricHelper.LogErrorEvent(er, this.Profile.DefaultSubscription.Id.ToString(), this.GetType().Name);
543+
MetricHelper.LogUsageEvent(QosEvent);
544+
MetricHelper.FlushMetric(waitForMetricSending);
545545
}
546546
catch (Exception e)
547547
{
@@ -560,10 +560,12 @@ protected void LogMetricHelperErrorEvent(ErrorRecord er)
560560
/// <param name="action">The action code</param>
561561
protected void ConfirmAction(bool force, string actionMessage, string processMessage, string target, Action action)
562562
{
563+
QosEvent.PauseQoSTimer();
563564
if (force || ShouldContinue(actionMessage, ""))
564565
{
565566
if (ShouldProcess(target, processMessage))
566567
{
568+
QosEvent.ResumeQosTimer();
567569
action();
568570
}
569571
}

src/Common/Commands.Common/MetricHelper.cs

Lines changed: 71 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Diagnostics.Eventing.Reader;
5+
using System.Globalization;
36
using System.Linq;
47
using System.Management.Automation;
58
using System.Security.Cryptography;
@@ -20,6 +23,7 @@ public static class MetricHelper
2023
static MetricHelper()
2124
{
2225
TelemetryClient = new TelemetryClient();
26+
TelemetryClient.Context.Location.Ip = "0.0.0.0";
2327

2428
if (!IsMetricTermAccepted())
2529
{
@@ -30,43 +34,44 @@ static MetricHelper()
3034
{
3135
//TODO enable in final cr
3236
//TelemetryConfiguration.Active.DisableTelemetry = true;
33-
//TelemetryConfiguration.Active.TelemetryChannel.DeveloperMode = true;
3437
}
3538
}
3639

37-
public static void LogUsageEvent(string subscriptionId, string cmdletName)
40+
public static void LogUsageEvent(AzurePSQoSEvent qos)
3841
{
3942
if (!IsMetricTermAccepted())
4043
{
4144
return;
4245
}
4346

4447
var tcEvent = new EventTelemetry("CmdletUsage");
45-
46-
tcEvent.Context.User.Id = GenerateSha256HashString(subscriptionId);
48+
//tcEvent.Context.Location.Ip = "0.0.0.0";
49+
tcEvent.Context.User.Id = qos.UID;
4750
tcEvent.Context.User.UserAgent = AzurePowerShell.UserAgentValue.ToString();
48-
tcEvent.Properties.Add("CmdletType", cmdletName);
51+
tcEvent.Properties.Add("CmdletType", qos.CmdletType);
52+
tcEvent.Properties.Add("IsSuccess", qos.IsSuccess.ToString());
53+
tcEvent.Properties.Add("Duration", qos.Duration.TotalSeconds.ToString(CultureInfo.InvariantCulture));
4954

5055
TelemetryClient.TrackEvent(tcEvent);
51-
}
5256

53-
public static void LogErrorEvent(ErrorRecord err, string subscriptionId, string cmdletName)
54-
{
55-
if (!IsMetricTermAccepted())
57+
if (qos.Exception != null)
5658
{
57-
return;
59+
LogExceptionEvent(qos);
5860
}
61+
}
62+
63+
private static void LogExceptionEvent(AzurePSQoSEvent qos)
64+
{
5965

6066
var tcEvent = new EventTelemetry("CmdletError");
61-
tcEvent.Properties.Add("ExceptionType", err.Exception.GetType().FullName);
62-
if (err.Exception.InnerException != null)
67+
tcEvent.Properties.Add("ExceptionType", qos.Exception.GetType().FullName);
68+
if (qos.Exception.InnerException != null)
6369
{
64-
tcEvent.Properties.Add("InnerExceptionType", err.Exception.InnerException.GetType().FullName);
70+
tcEvent.Properties.Add("InnerExceptionType", qos.Exception.InnerException.GetType().FullName);
6571
}
66-
67-
tcEvent.Context.User.Id = GenerateSha256HashString(subscriptionId);
68-
tcEvent.Context.User.UserAgent = AzurePowerShell.UserAgentValue.ToString();
69-
tcEvent.Properties.Add("CmdletType", cmdletName);
72+
73+
tcEvent.Context.User.Id = qos.UID;
74+
tcEvent.Properties.Add("CmdletType", qos.CmdletType);
7075

7176
TelemetryClient.TrackEvent(tcEvent);
7277
}
@@ -77,24 +82,28 @@ public static bool IsMetricTermAccepted()
7782
return true;
7883
}
7984

80-
public static void FlushMetric()
85+
public static void FlushMetric(bool waitForMetricSending)
8186
{
8287
if (!IsMetricTermAccepted())
8388
{
8489
return;
8590
}
8691

87-
var t = Task.Run(() => FlushAi());
88-
//TelemetryClient.Flush();
92+
if (waitForMetricSending)
93+
{
94+
FlushAi();
95+
}
96+
else
97+
{
98+
Task.Run(() => FlushAi());
99+
}
89100
}
90101

91102
private static void FlushAi()
92103
{
93104
try
94105
{
95106
TelemetryClient.Flush();
96-
97-
//return Task.FromResult(default(object));
98107
}
99108
catch
100109
{
@@ -115,3 +124,43 @@ public static string GenerateSha256HashString(string originInput)
115124
}
116125
}
117126
}
127+
128+
public class AzurePSQoSEvent
129+
{
130+
private readonly Stopwatch _timer;
131+
132+
public TimeSpan Duration { get; set; }
133+
public bool IsSuccess { get; set; }
134+
public string CmdletType { get; set; }
135+
public Exception Exception { get; set; }
136+
public string UID { get; set; }
137+
138+
public AzurePSQoSEvent()
139+
{
140+
_timer = new Stopwatch();
141+
_timer.Start();
142+
}
143+
144+
public void PauseQoSTimer()
145+
{
146+
_timer.Stop();
147+
}
148+
149+
public void ResumeQosTimer()
150+
{
151+
_timer.Start();
152+
}
153+
154+
public void FinishQosEvent()
155+
{
156+
_timer.Stop();
157+
Duration = _timer.Elapsed;
158+
}
159+
160+
public override string ToString()
161+
{
162+
return string.Format(
163+
"AzureQoSEvent: CmdletType - {0}; IsSuccess - {1}; Duration - {2}; Exception - {3};",
164+
CmdletType, IsSuccess, Duration, Exception);
165+
}
166+
}

src/ResourceManager/Compute/Commands.Compute/Common/ComputeClientBaseCmdlet.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,11 @@ protected void ExecuteClientAction(Action action)
5959
{
6060
try
6161
{
62-
try
63-
{
64-
action();
65-
}
66-
catch (CloudException ex)
67-
{
68-
throw new ComputeCloudException(ex);
69-
}
62+
action();
7063
}
71-
catch (Exception ex)
64+
catch (CloudException ex)
7265
{
73-
WriteExceptionError(ex);
66+
throw new ComputeCloudException(ex);
7467
}
7568
}
7669
}

0 commit comments

Comments
 (0)