Skip to content

Commit 841eee4

Browse files
committed
Split usage/err enable flag
1 parent f2fb746 commit 841eee4

File tree

6 files changed

+36
-47
lines changed

6 files changed

+36
-47
lines changed

src/Common/Commands.Common/ApplicationInsights.config

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/Common/Commands.Common/AzurePSCmdlet.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,15 @@ public abstract class AzurePSCmdlet : PSCmdlet
3838

3939
protected AzurePSQoSEvent QosEvent;
4040

41-
protected virtual bool IsMetricEnabled {
41+
protected virtual bool IsUsageMetricEnabled {
4242
get { return false; }
4343
}
4444

45+
protected virtual bool IsErrorMetricEnabled
46+
{
47+
get { return true; }
48+
}
49+
4550
[Parameter(Mandatory = false, HelpMessage = "In-memory profile.")]
4651
public AzureProfile Profile { get; set; }
4752

@@ -522,7 +527,7 @@ protected void InitializeQosEvent()
522527
{
523528
CmdletType = this.GetType().Name,
524529
IsSuccess = true,
525-
UID = MetricHelper.GenerateSha256HashString(this.Profile.DefaultSubscription.Id.ToString())
530+
Uid = MetricHelper.GenerateSha256HashString(this.Profile.DefaultSubscription.Id.ToString())
526531
};
527532
}
528533

@@ -533,15 +538,12 @@ protected void LogQosEvent(bool waitForMetricSending = false)
533538
{
534539
QosEvent.FinishQosEvent();
535540
WriteVerbose(QosEvent.ToString());
536-
if (!IsMetricEnabled)
537-
{
538-
return;
539-
}
540541

541542
try
542543
{
543-
MetricHelper.LogUsageEvent(QosEvent);
544+
MetricHelper.LogQoSEvent(QosEvent, IsUsageMetricEnabled, IsErrorMetricEnabled);
544545
MetricHelper.FlushMetric(waitForMetricSending);
546+
WriteVerbose("Finish sending metric.");
545547
}
546548
catch (Exception e)
547549
{

src/Common/Commands.Common/Commands.Common.csproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,6 @@
190190
</EmbeddedResource>
191191
</ItemGroup>
192192
<ItemGroup>
193-
<Content Include="ApplicationInsights.config">
194-
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
195-
</Content>
196193
<None Include="packages.config">
197194
<SubType>Designer</SubType>
198195
</None>

src/Common/Commands.Common/MetricHelper.cs

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
4-
using System.Diagnostics.Eventing.Reader;
5-
using System.Globalization;
6-
using System.Linq;
7-
using System.Management.Automation;
84
using System.Security.Cryptography;
95
using System.Text;
106
using System.Threading.Tasks;
@@ -23,6 +19,8 @@ public static class MetricHelper
2319
static MetricHelper()
2420
{
2521
TelemetryClient = new TelemetryClient();
22+
//InstrumentationKey shall be injected in build server
23+
TelemetryClient.InstrumentationKey = "ce08abab-065a-4af8-a997-fdd4cd5481b4";
2624
//TelemetryClient.Context.Location.Ip = "0.0.0.0";
2725

2826
if (!IsMetricTermAccepted())
@@ -37,40 +35,47 @@ static MetricHelper()
3735
}
3836
}
3937

40-
public static void LogUsageEvent(AzurePSQoSEvent qos)
38+
public static void LogQoSEvent(AzurePSQoSEvent qos, bool isUsageMetricEnabled, bool isErrorMetricEnabled)
4139
{
4240
if (!IsMetricTermAccepted())
4341
{
4442
return;
4543
}
4644

47-
var tcEvent = new EventTelemetry("CmdletUsage");
48-
//tcEvent.Context.Location.Ip = "0.0.0.0";
49-
tcEvent.Context.User.Id = qos.UID;
50-
tcEvent.Context.User.UserAgent = AzurePowerShell.UserAgentValue.ToString();
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));
54-
55-
TelemetryClient.TrackEvent(tcEvent);
45+
if (isUsageMetricEnabled)
46+
{
47+
LogUsageEvent(qos);
48+
}
5649

57-
if (qos.Exception != null)
50+
if (isErrorMetricEnabled && qos.Exception != null)
5851
{
5952
LogExceptionEvent(qos);
6053
}
6154
}
6255

63-
private static void LogExceptionEvent(AzurePSQoSEvent qos)
56+
private static void LogUsageEvent(AzurePSQoSEvent qos)
6457
{
58+
var tcEvent = new RequestTelemetry(qos.CmdletType, qos.StartTime, qos.Duration, string.Empty, qos.IsSuccess);
59+
tcEvent.Context.User.Id = qos.Uid;
60+
tcEvent.Context.User.UserAgent = AzurePowerShell.UserAgentValue.ToString();
61+
tcEvent.Context.Device.OperatingSystem = Environment.OSVersion.VersionString;
62+
63+
TelemetryClient.TrackRequest(tcEvent);
64+
}
6565

66+
private static void LogExceptionEvent(AzurePSQoSEvent qos)
67+
{
68+
//Log as custome event to exclude actual exception message
6669
var tcEvent = new EventTelemetry("CmdletError");
6770
tcEvent.Properties.Add("ExceptionType", qos.Exception.GetType().FullName);
71+
tcEvent.Properties.Add("StackTrace", qos.Exception.StackTrace);
6872
if (qos.Exception.InnerException != null)
6973
{
7074
tcEvent.Properties.Add("InnerExceptionType", qos.Exception.InnerException.GetType().FullName);
75+
tcEvent.Properties.Add("InnerStackTrace", qos.Exception.InnerException.StackTrace);
7176
}
7277

73-
tcEvent.Context.User.Id = qos.UID;
78+
tcEvent.Context.User.Id = qos.Uid;
7479
tcEvent.Properties.Add("CmdletType", qos.CmdletType);
7580

7681
TelemetryClient.TrackEvent(tcEvent);
@@ -129,14 +134,16 @@ public class AzurePSQoSEvent
129134
{
130135
private readonly Stopwatch _timer;
131136

137+
public DateTimeOffset StartTime { get; set; }
132138
public TimeSpan Duration { get; set; }
133139
public bool IsSuccess { get; set; }
134140
public string CmdletType { get; set; }
135141
public Exception Exception { get; set; }
136-
public string UID { get; set; }
142+
public string Uid { get; set; }
137143

138144
public AzurePSQoSEvent()
139145
{
146+
StartTime = DateTimeOffset.Now;
140147
_timer = new Stopwatch();
141148
_timer.Start();
142149
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public abstract class ComputeClientBaseCmdlet : AzurePSCmdlet
2323
{
2424
protected const string VirtualMachineExtensionType = "Microsoft.Compute/virtualMachines/extensions";
2525

26-
protected override bool IsMetricEnabled
26+
protected override bool IsUsageMetricEnabled
2727
{
2828
get { return true; }
2929
}

src/ResourceManager/Compute/Commands.Compute/VirtualMachine/Config/NewAzureVMConfigCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class NewAzureVMConfigCommand : AzurePSCmdlet
5151
[ValidateNotNullOrEmpty]
5252
public string AvailabilitySetId { get; set; }
5353

54-
protected override bool IsMetricEnabled
54+
protected override bool IsUsageMetricEnabled
5555
{
5656
get { return true; }
5757
}

0 commit comments

Comments
 (0)