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 command = string.Empty;
var lastArg = 0;
for (; lastArg < args.Length; lastArg++)
using (INuGetCacheSentinel nugetCacheSentinel = new NuGetCacheSentinel())
{
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"))
{
PrintVersion();
return 0;
}
else if (IsArg(args[lastArg], "info"))
{
PrintInfo();
return 0;
}
else if (IsArg(args[lastArg], "h", "help"))
if (!success)
{
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!
command = args[lastArg];
break;
if (telemetryClient == null)
{
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();
@ -164,28 +168,22 @@ namespace Microsoft.DotNet.Cli
}
private static void ConfigureDotNetForFirstTimeUse()
private static void ConfigureDotNetForFirstTimeUse(INuGetCacheSentinel nugetCacheSentinel)
{
using (PerfTrace.Current.CaptureTiming())
{
using (var nugetPackagesArchiver = new NuGetPackagesArchiver())
{
using (var nugetCacheSentinel = new NuGetCacheSentinel())
{
var environmentProvider = new EnvironmentProvider();
var commandFactory = new DotNetCommandFactory();
var nugetCachePrimer =
new NuGetCachePrimer(commandFactory, nugetPackagesArchiver, nugetCacheSentinel);
var dotnetConfigurer = new DotnetFirstTimeUseConfigurer(
nugetCachePrimer,
nugetCacheSentinel,
environmentProvider);
var environmentProvider = new EnvironmentProvider();
var commandFactory = new DotNetCommandFactory();
var nugetCachePrimer =
new NuGetCachePrimer(commandFactory, nugetPackagesArchiver, nugetCacheSentinel);
var dotnetConfigurer = new DotnetFirstTimeUseConfigurer(
nugetCachePrimer,
nugetCacheSentinel,
environmentProvider);
dotnetConfigurer.Configure();
if (!File.Exists(Telemetry.TelemetrySentinel))
File.Create(Telemetry.TelemetrySentinel);
}
dotnetConfigurer.Configure();
}
}
}

View file

@ -8,6 +8,7 @@ using System.IO;
using System.Threading.Tasks;
using Microsoft.ApplicationInsights;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Configurer;
using Microsoft.DotNet.InternalAbstractions;
namespace Microsoft.DotNet.Cli
@ -20,8 +21,6 @@ namespace Microsoft.DotNet.Cli
private Dictionary<string, double> _commonMeasurements = 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 TelemetryOptout = "DOTNET_CLI_TELEMETRY_OPTOUT";
private const string TelemetryProfileEnvironmentVariable = "DOTNET_CLI_TELEMETRY_PROFILE";
@ -33,9 +32,11 @@ namespace Microsoft.DotNet.Cli
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)
{
@ -45,10 +46,15 @@ namespace Microsoft.DotNet.Cli
//initialize in task to offload to parallel thread
_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)

View file

@ -15,8 +15,8 @@ namespace Microsoft.DotNet.Tests
{
public class GivenThatTheUserIsRunningDotNetForTheFirstTime : TestBase
{
private static CommandResult _firstDotnetInfoUseCommandResult;
private static CommandResult _firstDotnetNewUseCommandResult;
private static CommandResult _firstDotnetNonVerbUseCommandResult;
private static CommandResult _firstDotnetVerbUseCommandResult;
private static DirectoryInfo _nugetCacheFolder;
static GivenThatTheUserIsRunningDotNetForTheFirstTime()
@ -29,8 +29,8 @@ namespace Microsoft.DotNet.Tests
command.Environment["NUGET_PACKAGES"] = testNugetCache;
command.Environment["DOTNET_SKIP_FIRST_TIME_EXPERIENCE"] = "";
_firstDotnetInfoUseCommandResult = command.ExecuteWithCapturedOutput("--info");
_firstDotnetNewUseCommandResult = command.ExecuteWithCapturedOutput("new");
_firstDotnetNonVerbUseCommandResult = command.ExecuteWithCapturedOutput("--info");
_firstDotnetVerbUseCommandResult = command.ExecuteWithCapturedOutput("new");
_nugetCacheFolder = new DirectoryInfo(testNugetCache);
}
@ -38,15 +38,15 @@ namespace Microsoft.DotNet.Tests
[Fact]
public void Using_dotnet_for_the_first_time_succeeds()
{
_firstDotnetNewUseCommandResult.Should().Pass();
_firstDotnetVerbUseCommandResult.Should().Pass();
}
[Fact]
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]
@ -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.";
_firstDotnetNewUseCommandResult.StdOut.Should().StartWith(firstTimeUseWelcomeMessage);
_firstDotnetVerbUseCommandResult.StdOut.Should().StartWith(firstTimeUseWelcomeMessage);
}
[Fact]