Use NugetCache Sentinel for Telemetry setting.

This commit is contained in:
Lakshmi Priya Sekar 2016-06-15 12:59:42 -07:00
parent eab2494ed5
commit db7f68716b
3 changed files with 70 additions and 66 deletions

View file

@ -84,50 +84,54 @@ namespace Microsoft.DotNet.Cli
var success = true; var success = true;
var command = string.Empty; var command = string.Empty;
var lastArg = 0; var lastArg = 0;
using (INuGetCacheSentinel nugetCacheSentinel = new NuGetCacheSentinel())
for (; lastArg < args.Length; lastArg++)
{ {
if (IsArg(args[lastArg], "v", "verbose")) for (; lastArg < args.Length; lastArg++)
{ {
verbose = true; if (IsArg(args[lastArg], "v", "verbose"))
{
verbose = true;
}
else if (IsArg(args[lastArg], "version"))
{
PrintVersion();
return 0;
}
else if (IsArg(args[lastArg], "info"))
{
PrintInfo();
return 0;
}
else if (IsArg(args[lastArg], "h", "help"))
{
HelpCommand.PrintHelp();
return 0;
}
else if (args[lastArg].StartsWith("-"))
{
Reporter.Error.WriteLine($"Unknown option: {args[lastArg]}");
success = false;
}
else
{
ConfigureDotNetForFirstTimeUse(nugetCacheSentinel);
// It's the command, and we're done!
command = args[lastArg];
break;
}
} }
else if (IsArg(args[lastArg], "version")) if (!success)
{
PrintVersion();
return 0;
}
else if (IsArg(args[lastArg], "info"))
{
PrintInfo();
return 0;
}
else if (IsArg(args[lastArg], "h", "help"))
{ {
HelpCommand.PrintHelp(); HelpCommand.PrintHelp();
return 0; return 1;
} }
else if (args[lastArg].StartsWith("-"))
{
Reporter.Error.WriteLine($"Unknown option: {args[lastArg]}");
success = false;
}
else
{
ConfigureDotNetForFirstTimeUse();
// It's the command, and we're done! if (telemetryClient == null)
command = args[lastArg]; {
break; telemetryClient = new Telemetry(nugetCacheSentinel);
} }
} }
if (!success)
{
HelpCommand.PrintHelp();
return 1;
}
if (telemetryClient == null)
telemetryClient = new Telemetry();
var appArgs = (lastArg + 1) >= args.Length ? Enumerable.Empty<string>() : args.Skip(lastArg + 1).ToArray(); var appArgs = (lastArg + 1) >= args.Length ? Enumerable.Empty<string>() : args.Skip(lastArg + 1).ToArray();
@ -164,28 +168,22 @@ namespace Microsoft.DotNet.Cli
} }
private static void ConfigureDotNetForFirstTimeUse() private static void ConfigureDotNetForFirstTimeUse(INuGetCacheSentinel nugetCacheSentinel)
{ {
using (PerfTrace.Current.CaptureTiming()) using (PerfTrace.Current.CaptureTiming())
{ {
using (var nugetPackagesArchiver = new NuGetPackagesArchiver()) using (var nugetPackagesArchiver = new NuGetPackagesArchiver())
{ {
using (var nugetCacheSentinel = new NuGetCacheSentinel()) var environmentProvider = new EnvironmentProvider();
{ var commandFactory = new DotNetCommandFactory();
var environmentProvider = new EnvironmentProvider(); var nugetCachePrimer =
var commandFactory = new DotNetCommandFactory(); new NuGetCachePrimer(commandFactory, nugetPackagesArchiver, nugetCacheSentinel);
var nugetCachePrimer = var dotnetConfigurer = new DotnetFirstTimeUseConfigurer(
new NuGetCachePrimer(commandFactory, nugetPackagesArchiver, nugetCacheSentinel); nugetCachePrimer,
var dotnetConfigurer = new DotnetFirstTimeUseConfigurer( nugetCacheSentinel,
nugetCachePrimer, environmentProvider);
nugetCacheSentinel,
environmentProvider);
dotnetConfigurer.Configure(); dotnetConfigurer.Configure();
if (!File.Exists(Telemetry.TelemetrySentinel))
File.Create(Telemetry.TelemetrySentinel);
}
} }
} }
} }

View file

@ -8,6 +8,7 @@ using System.IO;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.ApplicationInsights; using Microsoft.ApplicationInsights;
using Microsoft.DotNet.Cli.Utils; using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Configurer;
using Microsoft.DotNet.InternalAbstractions; using Microsoft.DotNet.InternalAbstractions;
namespace Microsoft.DotNet.Cli namespace Microsoft.DotNet.Cli
@ -20,8 +21,6 @@ namespace Microsoft.DotNet.Cli
private Dictionary<string, double> _commonMeasurements = null; private Dictionary<string, double> _commonMeasurements = null;
private Task _trackEventTask = null; private Task _trackEventTask = null;
internal static readonly string TelemetrySentinel = Path.Combine(ApplicationEnvironment.ApplicationBasePath, $"{Product.Version}.dotnetTelemetry");
private const string InstrumentationKey = "74cc1c9e-3e6e-4d05-b3fc-dde9101d0254"; private const string InstrumentationKey = "74cc1c9e-3e6e-4d05-b3fc-dde9101d0254";
private const string TelemetryOptout = "DOTNET_CLI_TELEMETRY_OPTOUT"; private const string TelemetryOptout = "DOTNET_CLI_TELEMETRY_OPTOUT";
private const string TelemetryProfileEnvironmentVariable = "DOTNET_CLI_TELEMETRY_PROFILE"; private const string TelemetryProfileEnvironmentVariable = "DOTNET_CLI_TELEMETRY_PROFILE";
@ -33,9 +32,11 @@ namespace Microsoft.DotNet.Cli
public bool Enabled { get; } public bool Enabled { get; }
public Telemetry() public Telemetry () : this(null) { }
public Telemetry(INuGetCacheSentinel sentinel)
{ {
Enabled = !Env.GetEnvironmentVariableAsBool(TelemetryOptout) && PermissionExists(); Enabled = !Env.GetEnvironmentVariableAsBool(TelemetryOptout) && PermissionExists(sentinel);
if (!Enabled) if (!Enabled)
{ {
@ -45,10 +46,15 @@ namespace Microsoft.DotNet.Cli
//initialize in task to offload to parallel thread //initialize in task to offload to parallel thread
_trackEventTask = Task.Factory.StartNew(() => InitializeTelemetry()); _trackEventTask = Task.Factory.StartNew(() => InitializeTelemetry());
} }
private bool PermissionExists() private bool PermissionExists(INuGetCacheSentinel sentinel)
{ {
return File.Exists(TelemetrySentinel); if (sentinel == null)
{
return false;
}
return sentinel.Exists();
} }
public void TrackEvent(string eventName, IDictionary<string, string> properties, IDictionary<string, double> measurements) public void TrackEvent(string eventName, IDictionary<string, string> properties, IDictionary<string, double> measurements)

View file

@ -15,8 +15,8 @@ namespace Microsoft.DotNet.Tests
{ {
public class GivenThatTheUserIsRunningDotNetForTheFirstTime : TestBase public class GivenThatTheUserIsRunningDotNetForTheFirstTime : TestBase
{ {
private static CommandResult _firstDotnetInfoUseCommandResult; private static CommandResult _firstDotnetNonVerbUseCommandResult;
private static CommandResult _firstDotnetNewUseCommandResult; private static CommandResult _firstDotnetVerbUseCommandResult;
private static DirectoryInfo _nugetCacheFolder; private static DirectoryInfo _nugetCacheFolder;
static GivenThatTheUserIsRunningDotNetForTheFirstTime() static GivenThatTheUserIsRunningDotNetForTheFirstTime()
@ -29,8 +29,8 @@ namespace Microsoft.DotNet.Tests
command.Environment["NUGET_PACKAGES"] = testNugetCache; command.Environment["NUGET_PACKAGES"] = testNugetCache;
command.Environment["DOTNET_SKIP_FIRST_TIME_EXPERIENCE"] = ""; command.Environment["DOTNET_SKIP_FIRST_TIME_EXPERIENCE"] = "";
_firstDotnetInfoUseCommandResult = command.ExecuteWithCapturedOutput("--info"); _firstDotnetNonVerbUseCommandResult = command.ExecuteWithCapturedOutput("--info");
_firstDotnetNewUseCommandResult = command.ExecuteWithCapturedOutput("new"); _firstDotnetVerbUseCommandResult = command.ExecuteWithCapturedOutput("new");
_nugetCacheFolder = new DirectoryInfo(testNugetCache); _nugetCacheFolder = new DirectoryInfo(testNugetCache);
} }
@ -38,15 +38,15 @@ namespace Microsoft.DotNet.Tests
[Fact] [Fact]
public void Using_dotnet_for_the_first_time_succeeds() public void Using_dotnet_for_the_first_time_succeeds()
{ {
_firstDotnetNewUseCommandResult.Should().Pass(); _firstDotnetVerbUseCommandResult.Should().Pass();
} }
[Fact] [Fact]
public void Using_dotnet_for_the_first_time_with_non_verbs_doesnt_print_eula() public void Using_dotnet_for_the_first_time_with_non_verbs_doesnt_print_eula()
{ {
const string firstTimeUseWelcomeMessage = @".NET Command Line Tools"; const string firstTimeNonVerbUseMessage = @".NET Command Line Tools";
_firstDotnetInfoUseCommandResult.StdOut.Should().StartWith(firstTimeUseWelcomeMessage); _firstDotnetNonVerbUseCommandResult.StdOut.Should().StartWith(firstTimeNonVerbUseMessage);
} }
[Fact] [Fact]
@ -64,7 +64,7 @@ Configuring...
------------------- -------------------
A command is running to initially populate your local package cache, to improve restore speed and enable offline access. This command will take up to a minute to complete and will only happen once."; A command is running to initially populate your local package cache, to improve restore speed and enable offline access. This command will take up to a minute to complete and will only happen once.";
_firstDotnetNewUseCommandResult.StdOut.Should().StartWith(firstTimeUseWelcomeMessage); _firstDotnetVerbUseCommandResult.StdOut.Should().StartWith(firstTimeUseWelcomeMessage);
} }
[Fact] [Fact]