-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathTelemetry.cs
More file actions
124 lines (108 loc) · 4.8 KB
/
Copy pathTelemetry.cs
File metadata and controls
124 lines (108 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System;
using System.Linq;
using System.Reflection;
using System.Threading;
using Fallout.Common.IO;
using Fallout.Solutions;
using Fallout.Common.Utilities;
using static Fallout.Common.ControlFlow;
namespace Fallout.Common.Execution;
internal static partial class Telemetry
{
// https://docs.microsoft.com/en-us/dotnet/core/tools/telemetry
// https://dotnet.microsoft.com/platform/telemetry
// https://docs.microsoft.com/en-us/azure/azure-monitor/app/console
// https://docs.microsoft.com/en-us/azure/azure-monitor/app/ip-collection
public const string OptOutEnvironmentKey = "FALLOUT_TELEMETRY_OPTOUT";
public const string LegacyOptOutEnvironmentKey = "NUKE_TELEMETRY_OPTOUT";
public const int CurrentVersion = 1;
// Telemetry is currently a no-op for Fallout. The original NUKE maintainer owned an
// Azure Application Insights endpoint we can't reuse; Microsoft.ApplicationInsights was
// dropped from our deps in #79. The awareness/disclosure machinery below stays so we can
// wire up a Fallout-controlled backend later without re-introducing the consent flow.
private const string VersionPropertyName = "FalloutTelemetryVersion";
private const string LegacyVersionPropertyName = "NukeTelemetryVersion";
static Telemetry()
{
var optoutParameter = ParameterService.GetParameter<string>(OptOutEnvironmentKey)
?? ParameterService.GetParameter<string>(LegacyOptOutEnvironmentKey)
?? string.Empty;
if (optoutParameter == "1" || optoutParameter.EqualsOrdinalIgnoreCase(bool.TrueString))
return;
// No telemetry endpoint configured for Fallout yet — short-circuit. When a backend lands,
// re-enable by calling ProjectModelTasks.Initialize() + CheckAwareness() here and wiring
// the resulting consent through to TrackEvent (in Telemetry.Events.cs).
}
private static int? CheckAwareness()
{
AbsolutePath GetCookieFile(string name, int version)
=> Constants.GlobalFalloutDirectory / "telemetry-awareness" / $"v{version}" / name;
// Check for calls from Fallout.Cli and custom global tools
if (SuppressErrors(() => FalloutBuild.BuildProjectFile, logWarning: false) == null)
{
var cookieName = Assembly.GetEntryAssembly().NotNull().GetName().Name;
var cookieFile = GetCookieFile(cookieName, CurrentVersion);
if (!cookieFile.Exists())
{
PrintDisclosure($"create awareness cookie for {cookieName.SingleQuote()}");
cookieFile.TouchFile();
}
return CurrentVersion;
}
var project = ProjectModelTasks.ParseProject(FalloutBuild.BuildProjectFile);
var property = project.Properties.SingleOrDefault(x => x.Name.EqualsOrdinalIgnoreCase(VersionPropertyName))
?? project.Properties.SingleOrDefault(x => x.Name.EqualsOrdinalIgnoreCase(LegacyVersionPropertyName));
if (property?.EvaluatedValue != CurrentVersion.ToString())
{
if (FalloutBuild.IsServerBuild)
{
PrintDisclosure(action: null);
return null;
}
PrintDisclosure($"set the {VersionPropertyName.SingleQuote()} property");
project.SetProperty(VersionPropertyName, CurrentVersion.ToString());
project.Save();
}
for (var version = CurrentVersion; version > 0; version--)
{
var cookieFile = GetCookieFile("Fallout.Cli", version);
if (cookieFile.FileExists())
return version;
}
return FalloutBuild.IsServerBuild ? CurrentVersion : null;
}
private static void PrintDisclosure(string action)
{
var disclosure =
new[]
{
$"Telemetry v{CurrentVersion}",
"------------",
"Fallout collects anonymous usage data in order to help us improve your experience.",
$"Read more about scope, data points, and opt-out: {Constants.FalloutTelemetryDocsUrl}",
string.Empty
}.JoinNewLine();
if (action != null &&
Environment.UserInteractive &&
!Console.IsInputRedirected)
{
Host.Information(disclosure);
Thread.Sleep(2000);
Host.Information($"Press <Enter> to {action} ...");
WaitForEnter();
}
else
{
Host.Warning(disclosure);
Host.Warning("Run in interactive console to fix this warning");
}
}
private static void WaitForEnter()
{
ConsoleKey response;
do
{
response = Console.ReadKey(intercept: true).Key;
} while (response != ConsoleKey.Enter);
}
}