Implemented Telemetry class via an interface for test ease

This commit is contained in:
Lakshan Fernando 2016-03-31 16:37:40 -07:00 committed by Dan Quirk
parent d9e169e77a
commit 25745d9d4e
4 changed files with 75 additions and 20 deletions

View file

@ -0,0 +1,13 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
namespace Microsoft.DotNet.Cli.Utils
{
public interface ITelemetry
{
void TrackEvent(string eventName, IDictionary<string, string> properties, IDictionary<string, double> measurements);
}
}

View file

@ -6,7 +6,7 @@ using System.Diagnostics;
namespace Microsoft.DotNet.Cli.Utils namespace Microsoft.DotNet.Cli.Utils
{ {
public class Telemetry public class Telemetry : ITelemetry
{ {
private static bool _isInitialized = false; private static bool _isInitialized = false;
private static TelemetryClient _client = null; private static TelemetryClient _client = null;
@ -23,13 +23,12 @@ namespace Microsoft.DotNet.Cli.Utils
public Telemetry() public Telemetry()
{ {
if (_isInitialized)
return;
bool optout = Env.GetEnvironmentVariableAsBool(TelemetryOptout); bool optout = Env.GetEnvironmentVariableAsBool(TelemetryOptout);
if (optout) if (optout)
{
return; return;
}
try try
{ {
@ -56,17 +55,19 @@ namespace Microsoft.DotNet.Cli.Utils
} }
} }
public void TrackCommand(string command, IDictionary<string, string> properties = null, IDictionary<string, double> measurements = null) public void TrackEvent(string eventName, IDictionary<string, string> properties, IDictionary<string, double> measurements)
{ {
if (!_isInitialized) if (!_isInitialized)
{
return; return;
}
Dictionary<string, double> eventMeasurements = GetEventMeasures(measurements); Dictionary<string, double> eventMeasurements = GetEventMeasures(measurements);
Dictionary<string, string> eventProperties = GetEventProperties(properties); Dictionary<string, string> eventProperties = GetEventProperties(properties);
try try
{ {
_client.TrackEvent(command, eventProperties, eventMeasurements); _client.TrackEvent(eventName, eventProperties, eventMeasurements);
_client.Flush(); _client.Flush();
} }
catch (Exception) { } catch (Exception) { }
@ -78,12 +79,16 @@ namespace Microsoft.DotNet.Cli.Utils
Dictionary<string, double> eventMeasurements = new Dictionary<string, double>(_commonMeasurements); Dictionary<string, double> eventMeasurements = new Dictionary<string, double>(_commonMeasurements);
if (measurements != null) if (measurements != null)
{ {
foreach (var m in measurements) foreach (var measurement in measurements)
{ {
if (eventMeasurements.ContainsKey(m.Key)) if (eventMeasurements.ContainsKey(measurement.Key))
eventMeasurements[m.Key] = m.Value; {
eventMeasurements[measurement.Key] = measurement.Value;
}
else else
eventMeasurements.Add(m.Key, m.Value); {
eventMeasurements.Add(measurement.Key, measurement.Value);
}
} }
} }
return eventMeasurements; return eventMeasurements;
@ -94,12 +99,16 @@ namespace Microsoft.DotNet.Cli.Utils
Dictionary<string, string> eventProperties = new Dictionary<string, string>(_commonProperties); Dictionary<string, string> eventProperties = new Dictionary<string, string>(_commonProperties);
if (properties != null) if (properties != null)
{ {
foreach (var p in properties) foreach (var property in properties)
{ {
if (eventProperties.ContainsKey(p.Key)) if (eventProperties.ContainsKey(property.Key))
eventProperties[p.Key] = p.Value; {
eventProperties[property.Key] = property.Value;
}
else else
eventProperties.Add(p.Key, p.Value); {
eventProperties.Add(property.Key, property.Value);
}
} }
} }
return eventProperties; return eventProperties;

View file

@ -26,14 +26,13 @@ namespace Microsoft.DotNet.Cli
{ {
public class Program public class Program
{ {
public static int Main(string[] args) public static int Main(string[] args)
{ {
DebugHelper.HandleDebugSwitch(ref args); DebugHelper.HandleDebugSwitch(ref args);
try try
{ {
return ProcessArgs(args); return new Program().ProcessArgs(args, new Telemetry());
} }
catch (CommandUnknownException e) catch (CommandUnknownException e)
{ {
@ -44,7 +43,7 @@ namespace Microsoft.DotNet.Cli
} }
private static int ProcessArgs(string[] args) public int ProcessArgs(string[] args, ITelemetry telemetryClient)
{ {
// CommandLineApplication is a bit restrictive, so we parse things ourselves here. Individual apps should use CLA. // CommandLineApplication is a bit restrictive, so we parse things ourselves here. Individual apps should use CLA.
@ -135,10 +134,9 @@ namespace Microsoft.DotNet.Cli
exitCode = result.ExitCode; exitCode = result.ExitCode;
} }
Telemetry telemetryClient = new Telemetry(); telemetryClient.TrackEvent(
telemetryClient.TrackCommand(
command, command,
null,
new Dictionary<string, double> new Dictionary<string, double>
{ {
["ExitCode"] = exitCode ["ExitCode"] = exitCode

View file

@ -0,0 +1,35 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.IO;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools.Test.Utilities;
using Xunit;
using FluentAssertions;
namespace Microsoft.DotNet.Tests
{
public class MockTelemetry : ITelemetry
{
public string EventName{get;set;}
public void TrackEvent(string eventName, IDictionary<string, string> properties, IDictionary<string, double> measurements)
{
EventName = eventName;
}
}
public class TelemetryCommandTests : TestBase
{
[Fact]
public void TestProjectDependencyIsNotAvailableThroughDriver()
{
Program program = new Program();
MockTelemetry mockTelemetry = new MockTelemetry();
string[] args = { "help" };
program.ProcessArgs(args, mockTelemetry);
Assert.Equal(mockTelemetry.EventName, args[0]);
}
}
}