Skip to content

Commit c213c13

Browse files
committed
Merge pull request Azure#345 from Azure/clu
Clu
2 parents 247fd5e + 682ea87 commit c213c13

File tree

15 files changed

+212
-383
lines changed

15 files changed

+212
-383
lines changed

src/CLU/Commands.Common.Authentication/Models/AzureRMProfile.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,21 @@ public AzureRMProfile(IDataStore dataStore)
7171
[JsonIgnore]
7272
public string ProfilePath { get; private set; }
7373

74+
/// <summary>
75+
/// When set to true, collects telemetry information.
76+
/// </summary>
77+
public bool? IsTelemetryCollectionEnabled { get; set; }
78+
7479
private void Load(string path)
7580
{
7681
this.ProfilePath = path;
7782
if (_dataStore.FileExists(ProfilePath))
7883
{
7984
string contents = _dataStore.ReadFileAsText(ProfilePath);
80-
AzureRMProfile profile = JsonConvert.DeserializeObject<AzureRMProfile>(contents);
81-
Debug.Assert(profile != null);
82-
this.Context = profile.Context;
83-
this.Environments = profile.Environments;
85+
JsonConvert.PopulateObject(contents, this);
8486
}
8587
}
8688

87-
8889
/// <summary>
8990
/// Writes profile to the disk it was opened from disk.
9091
/// </summary>

src/CLU/Commands.Common/AzureDataCmdlet.cs

Lines changed: 3 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -60,64 +60,11 @@ public AzureRMProfile RMProfile
6060
get { return GetSessionVariableValue<PSAzureProfile>(AzurePowerShell.ProfileVariable, null); }
6161
}
6262

63-
protected override void SaveDataCollectionProfile()
63+
protected override bool IsTelemetryCollectionEnabled
6464
{
65-
if (_dataCollectionProfile == null)
66-
{
67-
InitializeDataCollectionProfile();
68-
}
69-
70-
string fileFullPath = Path.Combine(AzurePowerShell.ProfileDirectory, AzurePSDataCollectionProfile.DefaultFileName);
71-
var contents = JsonConvert.SerializeObject(_dataCollectionProfile);
72-
if (!DataStore.DirectoryExists(AzurePowerShell.ProfileDirectory))
73-
{
74-
DataStore.CreateDirectory(AzurePowerShell.ProfileDirectory);
75-
}
76-
DataStore.WriteFile(fileFullPath, contents);
77-
WriteWarning(string.Format(Resources.DataCollectionSaveFileInformation, fileFullPath));
78-
}
79-
80-
protected override void PromptForDataCollectionProfileIfNotExists()
81-
{
82-
// Initialize it from the environment variable or profile file.
83-
InitializeDataCollectionProfile();
84-
85-
if (!_dataCollectionProfile.EnableAzureDataCollection.HasValue && CheckIfInteractive())
65+
get
8666
{
87-
WriteWarning(Resources.DataCollectionPrompt);
88-
89-
const double timeToWaitInSeconds = 60;
90-
var status = string.Format(Resources.DataCollectionConfirmTime, timeToWaitInSeconds);
91-
ProgressRecord record = new ProgressRecord(0, Resources.DataCollectionActivity, status);
92-
93-
var startTime = DateTime.Now;
94-
var endTime = DateTime.Now;
95-
//double elapsedSeconds = 0;
96-
97-
//while (!this.Host.UI.RawUI.KeyAvailable && elapsedSeconds < timeToWaitInSeconds)
98-
//{
99-
// Thread.Sleep(TimeSpan.FromMilliseconds(10));
100-
// endTime = DateTime.Now;
101-
102-
// elapsedSeconds = (endTime - startTime).TotalSeconds;
103-
// record.PercentComplete = ((int) elapsedSeconds*100/(int) timeToWaitInSeconds);
104-
// WriteProgress(record);
105-
//}
106-
107-
bool enabled = false;
108-
//if (this.Host.UI.RawUI.KeyAvailable)
109-
//{
110-
// KeyInfo keyInfo =
111-
// this.Host.UI.RawUI.ReadKey(ReadKeyOptions.NoEcho | ReadKeyOptions.AllowCtrlC |
112-
// ReadKeyOptions.IncludeKeyDown);
113-
// enabled = (keyInfo.Character == 'Y' || keyInfo.Character == 'y');
114-
//}
115-
116-
_dataCollectionProfile.EnableAzureDataCollection = enabled;
117-
118-
WriteWarning(enabled ? Resources.DataCollectionConfirmYes : Resources.DataCollectionConfirmNo);
119-
120-
SaveDataCollectionProfile();
67+
return false;
12168
}
12269
}
12370

src/CLU/Commands.Common/AzurePSCmdlet.cs

Lines changed: 12 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,7 @@ namespace Microsoft.Azure.Commands.Utilities.Common
4141
public abstract class AzurePSCmdlet : PSCmdlet, IDisposable
4242
{
4343
protected readonly ConcurrentQueue<string> _debugMessages;
44-
45-
4644
private DebugStreamTraceListener _adalListener;
47-
protected static AzurePSDataCollectionProfile _dataCollectionProfile = null;
4845
protected static string _errorRecordFolderPath = null;
4946
protected const string _fileTimeStampSuffixFormat = "yyyy-MM-dd-THH-mm-ss-fff";
5047
protected string _clientRequestId = Guid.NewGuid().ToString();
@@ -56,7 +53,8 @@ public abstract class AzurePSCmdlet : PSCmdlet, IDisposable
5653

5754
protected AzurePSQoSEvent QosEvent;
5855

59-
protected virtual bool IsUsageMetricEnabled {
56+
protected virtual bool IsUsageMetricEnabled
57+
{
6058
get { return true; }
6159
}
6260

@@ -67,9 +65,9 @@ protected virtual bool IsErrorMetricEnabled
6765

6866
/// <summary>
6967
/// Gets the PowerShell module name used for user agent header.
70-
/// By default uses "Azurepowershell"
68+
/// By default uses "AzureCLU"
7169
/// </summary>
72-
protected virtual string ModuleName { get { return "AzurePowershell"; } }
70+
protected virtual string ModuleName { get { return "AzureCLU"; } }
7371

7472
/// <summary>
7573
/// Gets PowerShell module version used for user agent header.
@@ -158,128 +156,21 @@ protected virtual void SetSessionVariable<T>(string name, T value) where T : cla
158156
DataStore.WriteFile(variablePath, JsonConvert.SerializeObject(value));
159157
}
160158
}
161-
/// <summary>
162-
/// Initialize the data collection profile
163-
/// </summary>
164-
protected static void InitializeDataCollectionProfile()
165-
{
166-
if (_dataCollectionProfile != null && _dataCollectionProfile.EnableAzureDataCollection.HasValue)
167-
{
168-
return;
169-
}
170-
171-
// Get the value of the environment variable for Azure PS data collection setting.
172-
string value = Environment.GetEnvironmentVariable(AzurePSDataCollectionProfile.EnvironmentVariableName);
173-
if (!string.IsNullOrWhiteSpace(value))
174-
{
175-
if (string.Equals(value, bool.FalseString, StringComparison.OrdinalIgnoreCase))
176-
{
177-
// Disable data collection only if it is explicitly set to 'false'.
178-
_dataCollectionProfile = new AzurePSDataCollectionProfile(true);
179-
}
180-
else if (string.Equals(value, bool.TrueString, StringComparison.OrdinalIgnoreCase))
181-
{
182-
// Enable data collection only if it is explicitly set to 'true'.
183-
_dataCollectionProfile = new AzurePSDataCollectionProfile(false);
184-
}
185-
}
186-
187-
// If the environment value is null or empty, or not correctly set, try to read the setting from default file location.
188-
if (_dataCollectionProfile == null)
189-
{
190-
string fileFullPath = Path.Combine(AzurePowerShell.ProfileDirectory, AzurePSDataCollectionProfile.DefaultFileName);
191-
if (File.Exists(fileFullPath))
192-
{
193-
string contents = File.ReadAllText(fileFullPath);
194-
_dataCollectionProfile = JsonConvert.DeserializeObject<AzurePSDataCollectionProfile>(contents);
195-
}
196-
}
197-
198-
// If the environment variable or file content is not set, create a new profile object.
199-
if (_dataCollectionProfile == null)
200-
{
201-
_dataCollectionProfile = new AzurePSDataCollectionProfile();
202-
}
203-
}
204-
205-
/// <summary>
206-
/// Get the data collection profile
207-
/// </summary>
208-
protected static AzurePSDataCollectionProfile GetDataCollectionProfile()
209-
{
210-
if (_dataCollectionProfile == null)
211-
{
212-
InitializeDataCollectionProfile();
213-
}
214-
215-
return _dataCollectionProfile;
216-
}
217159

218160
/// <summary>
219161
/// Check whether the data collection is opted in from user
220162
/// </summary>
221163
/// <returns>true if allowed</returns>
222-
public static bool IsDataCollectionAllowed()
164+
protected abstract bool IsTelemetryCollectionEnabled
223165
{
224-
//TODO: CLU - remove before final release
225-
return true;
226-
227-
//if (_dataCollectionProfile != null &&
228-
// _dataCollectionProfile.EnableAzureDataCollection.HasValue &&
229-
// _dataCollectionProfile.EnableAzureDataCollection.Value)
230-
//{
231-
// return true;
232-
//}
233-
234-
//return false;
166+
get;
235167
}
236168

237-
/// <summary>
238-
/// Save the current data collection profile Json data into the default file path
239-
/// </summary>
240-
protected abstract void SaveDataCollectionProfile();
241-
242-
protected bool CheckIfInteractive()
243-
{
244-
bool interactive = false;
245-
//if (this.Host == null ||
246-
// this.Host.UI == null ||
247-
// this.Host.UI.RawUI == null ||
248-
// Environment.GetCommandLineArgs().Any(s => s.Equals("-NonInteractive", StringComparison.OrdinalIgnoreCase)))
249-
//{
250-
// interactive = false;
251-
//}
252-
//else
253-
//{
254-
// try
255-
// {
256-
// var test = this.Host.UI.RawUI.KeyAvailable;
257-
// }
258-
// catch
259-
// {
260-
// interactive = false;
261-
// }
262-
//}
263-
264-
if (!interactive && !_dataCollectionProfile.EnableAzureDataCollection.HasValue)
265-
{
266-
_dataCollectionProfile.EnableAzureDataCollection = false;
267-
}
268-
return interactive;
269-
}
270-
271-
/// <summary>
272-
/// Prompt for the current data collection profile
273-
/// </summary>
274-
/// <param name="profile"></param>
275-
protected abstract void PromptForDataCollectionProfileIfNotExists();
276-
277169
/// <summary>
278170
/// Cmdlet begin process. Write to logs, setup Http Tracing and initialize profile
279171
/// </summary>
280172
protected override void BeginProcessing()
281173
{
282-
PromptForDataCollectionProfileIfNotExists();
283174
InitializeQosEvent();
284175
if (string.IsNullOrEmpty(ParameterSetName))
285176
{
@@ -352,7 +243,7 @@ protected bool IsVerbose()
352243

353244
protected new void WriteError(ErrorRecord errorRecord)
354245
{
355-
FlushDebugMessages(IsDataCollectionAllowed());
246+
FlushDebugMessages(IsTelemetryCollectionEnabled);
356247
if (QosEvent != null && errorRecord != null)
357248
{
358249
QosEvent.Exception = errorRecord.Exception;
@@ -524,7 +415,7 @@ protected void LogQosEvent()
524415
return;
525416
}
526417

527-
if (!IsDataCollectionAllowed())
418+
if (!IsTelemetryCollectionEnabled)
528419
{
529420
return;
530421
}
@@ -533,8 +424,10 @@ protected void LogQosEvent()
533424

534425
try
535426
{
536-
MetricHelper.LogQoSEvent(QosEvent, IsUsageMetricEnabled, IsErrorMetricEnabled);
537-
MetricHelper.FlushMetric();
427+
MetricHelper.LogQoSEvent(QosEvent,
428+
IsUsageMetricEnabled && IsTelemetryCollectionEnabled,
429+
IsErrorMetricEnabled && IsTelemetryCollectionEnabled);
430+
MetricHelper.FlushMetric(IsTelemetryCollectionEnabled);
538431
WriteDebug("Finish sending metric.");
539432
}
540433
catch (Exception e)

src/CLU/Commands.Common/AzurePSDataCollectionProfile.cs

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

src/CLU/Commands.Common/MetricHelper.cs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,6 @@ public static void AddTelemetryClient(TelemetryClient client)
8989

9090
public static void LogQoSEvent(AzurePSQoSEvent qos, bool isUsageMetricEnabled, bool isErrorMetricEnabled)
9191
{
92-
if (!IsMetricTermAccepted())
93-
{
94-
return;
95-
}
96-
9792
if (isUsageMetricEnabled)
9893
{
9994
LogUsageEvent(qos);
@@ -189,14 +184,9 @@ private static void PopulatePropertiesFromQos(AzurePSQoSEvent qos, IDictionary<s
189184
}
190185
}
191186

192-
public static bool IsMetricTermAccepted()
193-
{
194-
return AzurePSCmdlet.IsDataCollectionAllowed();
195-
}
196-
197-
public static void FlushMetric()
187+
public static void FlushMetric(bool isTelemetryEnabled)
198188
{
199-
if (!IsMetricTermAccepted())
189+
if (!isTelemetryEnabled)
200190
{
201191
return;
202192
}
@@ -205,7 +195,7 @@ public static void FlushMetric()
205195
{
206196
foreach (TelemetryClient client in TelemetryClients)
207197
{
208-
client.Flush();
198+
Task.Run(() => client.Flush()).Wait(TimeSpan.FromSeconds(5));
209199
}
210200
}
211201
catch

src/CLU/Commands.Common/Models/PSAzureProfile.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ public static implicit operator PSAzureProfile(AzureRMProfile profile)
2828

2929
var result = new PSAzureProfile
3030
{
31-
Context = profile.Context
31+
Context = profile.Context,
32+
IsTelemetryCollectionEnabled = profile.IsTelemetryCollectionEnabled
3233
};
3334

3435
profile.Environments
@@ -50,7 +51,8 @@ public static implicit operator AzureRMProfile(PSAzureProfile profile)
5051

5152
var result = new AzureRMProfile
5253
{
53-
Context = profile.Context
54+
Context = profile.Context,
55+
IsTelemetryCollectionEnabled = profile.IsTelemetryCollectionEnabled
5456
};
5557
profile.Environments.ForEach((e) => result.Environments[e.Key] = (AzureEnvironment) e.Value);
5658
return result;
@@ -74,6 +76,11 @@ public string EnvironmentNames
7476
/// </summary>
7577
public PSAzureContext Context { get; set; }
7678

79+
/// <summary>
80+
/// When set to true, collects telemetry information.
81+
/// </summary>
82+
public bool? IsTelemetryCollectionEnabled { get; set; }
83+
7784
public override string ToString()
7885
{
7986
return Context?.ToString();

0 commit comments

Comments
 (0)