Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various internal refactoring in TelemetryManager #2764

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal sealed class TelemetryManager : ITelemetryManager, IOutputDeviceDataPro

public void AddTelemetryCollectorProvider(Func<IServiceProvider, ITelemetryCollector> telemetryFactory)
{
ApplicationStateGuard.Ensure(_telemetryFactory is null, PlatformResources.TelemetryProviderAlreadySetErrorMessage);
ArgumentGuard.IsNotNull(telemetryFactory);
_telemetryFactory = telemetryFactory;
}

Expand All @@ -51,34 +51,34 @@ public async Task<ITelemetryCollector> BuildAsync(ServiceProvider serviceProvide

// If the environment variable is not set or is set to 0, telemetry is opted in.
ICommandLineOptions commandLineOptions = serviceProvider.GetCommandLineOptions();
bool dontShowLogo = commandLineOptions.IsOptionSet(PlatformCommandLineProvider.NoBannerOptionKey);
bool doNotShowLogo = commandLineOptions.IsOptionSet(PlatformCommandLineProvider.NoBannerOptionKey);

string? noBanner = environment.GetEnvironmentVariable(EnvironmentVariableConstants.TESTINGPLATFORM_NOBANNER);
await logger.LogInformationAsync($"{EnvironmentVariableConstants.TESTINGPLATFORM_NOBANNER} environment variable: '{noBanner}'");
dontShowLogo = (noBanner is not null and ("1" or "true")) || dontShowLogo;
string? noBannerEnvVar = environment.GetEnvironmentVariable(EnvironmentVariableConstants.TESTINGPLATFORM_NOBANNER);
await logger.LogInformationAsync($"{EnvironmentVariableConstants.TESTINGPLATFORM_NOBANNER} environment variable: '{noBannerEnvVar}'");
doNotShowLogo = (noBannerEnvVar is not null and ("1" or "true")) || doNotShowLogo;

string? dotnet_noLogo = environment.GetEnvironmentVariable(EnvironmentVariableConstants.DOTNET_NOLOGO);
await logger.LogInformationAsync($"{EnvironmentVariableConstants.DOTNET_NOLOGO} environment variable: '{dotnet_noLogo}'");
dontShowLogo = (dotnet_noLogo is not null and ("1" or "true")) || dontShowLogo;
string? dotnetNoLogoEnvVar = environment.GetEnvironmentVariable(EnvironmentVariableConstants.DOTNET_NOLOGO);
await logger.LogInformationAsync($"{EnvironmentVariableConstants.DOTNET_NOLOGO} environment variable: '{dotnetNoLogoEnvVar}'");
doNotShowLogo = (dotnetNoLogoEnvVar is not null and ("1" or "true")) || doNotShowLogo;

await logger.LogInformationAsync($"Telemetry is '{(!isTelemetryOptedOut ? "ENABLED" : "DISABLED")}'");

if (!isTelemetryOptedOut && !dontShowLogo)
if (!isTelemetryOptedOut && !doNotShowLogo)
{
ITestApplicationModuleInfo testApplicationModuleInfo = serviceProvider.GetTestApplicationModuleInfo();
string fileName = Path.ChangeExtension(Path.GetFileName(testApplicationModuleInfo.GetCurrentTestApplicationFullPath()), "testingPlatformFirstTimeUseSentinel");
string? directory = environment.GetEnvironmentVariable("LOCALAPPDATA") ?? environment.GetEnvironmentVariable("HOME");
if (directory is not null)
{
directory = Path.Combine(directory, "Microsoft", "TestingPlatform");
}

IFileSystem fileSystem = serviceProvider.GetFileSystem();
string fileName = Path.ChangeExtension(Path.GetFileName(testApplicationModuleInfo.GetCurrentTestApplicationFullPath()), "testingPlatformFirstTimeUseSentinel");
bool sentinelIsNotPresent =
RoslynString.IsNullOrWhiteSpace(directory)
|| !fileSystem.Exists(Path.Combine(directory, fileName));

if (!dontShowLogo && sentinelIsNotPresent)
if (!doNotShowLogo && sentinelIsNotPresent)
{
IOutputDevice outputDevice = serviceProvider.GetOutputDevice();
await outputDevice.DisplayAsync(this, new TextOutputDeviceData(PlatformResources.TelemetryNotice));
Expand All @@ -105,18 +105,11 @@ public async Task<ITelemetryCollector> BuildAsync(ServiceProvider serviceProvide
}
}

if (!isTelemetryOptedOut)
{
serviceProvider.TryAddService(new TelemetryInformation(true, TelemetryProperties.VersionValue));
}
else
{
serviceProvider.TryAddService(new TelemetryInformation(false, TelemetryProperties.VersionValue));
}
serviceProvider.TryAddService(new TelemetryInformation(!isTelemetryOptedOut, TelemetryProperties.VersionValue));

ITelemetryCollector telemetryCollector = _telemetryFactory is null
ITelemetryCollector telemetryCollector = _telemetryFactory is null || isTelemetryOptedOut
? new NopTelemetryService(!isTelemetryOptedOut)
: !isTelemetryOptedOut ? _telemetryFactory(serviceProvider) : new NopTelemetryService(!isTelemetryOptedOut);
: _telemetryFactory(serviceProvider);

if (!isTelemetryOptedOut)
{
Expand Down