Skip to content

Commit b8ff29f

Browse files
committed
Added az temeletry command and improved telemetry Flushing
1 parent 8f07ce1 commit b8ff29f

File tree

7 files changed

+100
-37
lines changed

7 files changed

+100
-37
lines changed

src/CLU/Commands.Common/MetricHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public static void FlushMetric(bool isTelemetryEnabled)
195195
{
196196
foreach (TelemetryClient client in TelemetryClients)
197197
{
198-
client.Flush();
198+
Task.Run(() => client.Flush()).Wait(TimeSpan.FromSeconds(5));
199199
}
200200
}
201201
catch

src/CLU/Microsoft.Azure.Commands.Profile/Account/AddAzureRmAccount.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class AddAzureRMAccountCommand : AzureRMCmdlet
3838
private const string AccessTokenParameterSet = "AccessToken";
3939
private const string SubscriptionNameParameterSet = "SubscriptionName";
4040
private const string SubscriptionIdParameterSet = "SubscriptionId";
41-
private const string CollectTelemetryEnvironmentVariable = "Azure_PS_Data_Collection";
41+
internal const string CollectTelemetryEnvironmentVariable = "Azure_PS_Data_Collection";
4242

4343
public AzureEnvironment Environment { get; set; }
4444

@@ -212,13 +212,13 @@ protected override void ProcessRecord()
212212

213213
bool isInteractive = azureAccount.Id == null;
214214

215+
PromptForDataCollectionProfileIfNotExists(isInteractive);
216+
215217
var profileClient = new RMProfileClient(AuthenticationFactory, ClientFactory, DefaultProfile);
216218
profileClient.WarningLog = (s) => WriteWarning(s);
217-
219+
218220
WriteObject((PSAzureProfile)profileClient.Login(azureAccount, Environment, TenantId, SubscriptionId,
219221
SubscriptionName, password));
220-
221-
PromptForDataCollectionProfileIfNotExists(isInteractive);
222222
}
223223

224224
private void PromptForDataCollectionProfileIfNotExists(bool isInteractive)
@@ -236,8 +236,8 @@ private void PromptForDataCollectionProfileIfNotExists(bool isInteractive)
236236
if (!DefaultProfile.IsTelemetryCollectionEnabled.HasValue && isInteractive)
237237
{
238238
Collection<ChoiceDescription> choices = new Collection<ChoiceDescription>();
239-
choices.Add(new ChoiceDescription("Yes", Resources.DataCollectionConfirmYes));
240-
choices.Add(new ChoiceDescription("No", Resources.DataCollectionConfirmNo));
239+
choices.Add(new ChoiceDescription("&Yes", Resources.DataCollectionConfirmYes));
240+
choices.Add(new ChoiceDescription("&No", Resources.DataCollectionConfirmNo));
241241
try
242242
{
243243
int choice = this.Host.UI.PromptForChoice(Resources.DataCollectionActivity, Resources.DataCollectionPrompt, choices, 1);

src/CLU/Microsoft.Azure.Commands.Profile/Microsoft.Azure.Commands.Profile.format.ps1xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@
1414
<Label>Context</Label>
1515
<Property>Context</Property>
1616
</ListItem>
17-
<ListItem>
17+
<ListItem>
1818
<Label>Environments</Label>
1919
<Property>EnvironmentNames</Property>
2020
</ListItem>
21+
<ListItem>
22+
<Label>TelemetryEnabled</Label>
23+
<Property>IsTelemetryCollectionEnabled</Property>
24+
</ListItem>
2125
</ListItems>
2226
</ListEntry>
2327
</ListEntries>

src/CLU/Microsoft.Azure.Commands.Profile/Properties/Resources.Designer.cs

Lines changed: 7 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/CLU/Microsoft.Azure.Commands.Profile/Properties/Resources.resx

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -142,27 +142,20 @@
142142
<value>Current tenant with Id '{0}' will be used.</value>
143143
</data>
144144
<data name="DataCollectionActivity" xml:space="preserve">
145-
<value>Microsoft Azure PowerShell Data Collection Confirmation</value>
145+
<value>Microsoft Azure CLU Data Collection Confirmation</value>
146146
</data>
147147
<data name="DataCollectionConfirmNo" xml:space="preserve">
148-
<value>You choose not to participate in Microsoft Azure PowerShell data collection.</value>
149-
</data>
150-
<data name="DataCollectionConfirmTime" xml:space="preserve">
151-
<value>This confirmation message will be dismissed in '{0}' second(s)...</value>
148+
<value>You choose not to participate in Microsoft Azure CLU data collection.</value>
152149
</data>
153150
<data name="DataCollectionConfirmYes" xml:space="preserve">
154-
<value>You choose to participate in Microsoft Azure PowerShell data collection.</value>
151+
<value>You choose to participate in Microsoft Azure CLU data collection.</value>
155152
</data>
156153
<data name="DataCollectionPrompt" xml:space="preserve">
157-
<value>Microsoft Azure PowerShell collects data about how users use PowerShell cmdlets and some problems they encounter. Microsoft uses this information to improve our PowerShell cmdlets. Participation is voluntary and when you choose to participate your device automatically sends information to Microsoft about how you use Azure PowerShell.
154+
<value>Microsoft Azure CLU collects data about how users use CLU commands and some problems they encounter. Microsoft uses this information to improve our CLU commands. Participation is voluntary and when you choose to participate your device automatically sends information to Microsoft about how you use Azure CLU.
158155

159-
If you choose to participate, you can stop at any time by using Azure PowerShell as follows:
160-
1. Use the Disable-AzureDataCollection cmdlet to turn the feature Off. The cmdlet can be found in the AzureResourceManager module
161-
To disable data collection: PS &gt; Disable-AzureDataCollection
156+
If you choose to participate, you may stop later at any time. To stop participation run `az telemetry --disable`.
162157

163-
If you choose to not participate, you can enable at any time by using Azure PowerShell as follows:
164-
1. Use the Enable-AzureDataCollection cmdlet to turn the feature On. The cmdlet can be found in the AzureResourceManager module
165-
To enable data collection: PS &gt; Enable-AzureDataCollection
158+
If you choose to not participate, you may start later at any time. To start participation run `az telemetry --enable`.
166159

167160
Select Y to enable data collection [Y/N]:</value>
168161
</data>
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System.Management.Automation;
16+
using Microsoft.Azure.Commands.ResourceManager.Common;
17+
using Microsoft.Azure.Commands.Models;
18+
19+
namespace Microsoft.Azure.Commands.Profile
20+
{
21+
/// <summary>
22+
/// Cmdlet to enable or disable telemetry collection.
23+
/// </summary>
24+
[Cmdlet(VerbsCommon.Set, "AzureTelemetryCollection")]
25+
[OutputType(typeof(PSAzureProfile))]
26+
[CliCommandAlias("telemetry")]
27+
public class SetTelemetryCollectionCommand : AzureRMCmdlet
28+
{
29+
private const string EnabledParameterSet = "Enable";
30+
private const string DisableParameterSet = "Disable";
31+
32+
33+
[Parameter(ParameterSetName = EnabledParameterSet,
34+
Mandatory = true, HelpMessage = "Enable telemetry collection.")]
35+
[Alias("e")]
36+
public SwitchParameter Enable { get; set; }
37+
38+
[Parameter(ParameterSetName = DisableParameterSet,
39+
Mandatory = true, HelpMessage = "Disable telemetry collection.")]
40+
[Alias("d")]
41+
public SwitchParameter Disable { get; set; }
42+
43+
protected override void BeginProcessing()
44+
{
45+
base.BeginProcessing();
46+
}
47+
48+
protected override void ProcessRecord()
49+
{
50+
if (Enable.IsPresent)
51+
{
52+
DefaultProfile.IsTelemetryCollectionEnabled = true;
53+
}
54+
else if (Disable.IsPresent)
55+
{
56+
DefaultProfile.IsTelemetryCollectionEnabled = false;
57+
}
58+
59+
var collectTelemetryEnv = System.Environment.GetEnvironmentVariable(AddAzureRMAccountCommand.CollectTelemetryEnvironmentVariable);
60+
if (!string.IsNullOrEmpty(collectTelemetryEnv))
61+
{
62+
bool collectTelemetry = false;
63+
if (bool.TryParse(collectTelemetryEnv, out collectTelemetry))
64+
{
65+
throw new PSInvalidOperationException(
66+
$"Environment variable '{AddAzureRMAccountCommand.CollectTelemetryEnvironmentVariable}' is already to set to {collectTelemetryEnv}. This environment variable should be removed before executing the command.");
67+
}
68+
}
69+
70+
DefaultProfile.Save();
71+
WriteObject((PSAzureProfile)DefaultProfile);
72+
}
73+
}
74+
}

src/CLU/Microsoft.CLU/CommandBinder/CmdletBinderAndCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ public void Invoke()
268268
}
269269
finally
270270
{
271-
_telemetryClient.Flush();
271+
Task.Run(() => _telemetryClient.Flush()).Wait(TimeSpan.FromSeconds(5));
272272
}
273273
}
274274

0 commit comments

Comments
 (0)