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

Enable or disable telemetry #7638

Merged
19 commits merged into from
Jul 20, 2021
34 changes: 26 additions & 8 deletions src/Microsoft.DotNet.SignTool/src/Telemetry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,44 @@ internal class Telemetry
{ "BUILD_SOURCEVERSION", Environment.GetEnvironmentVariable("BUILD_SOURCEVERSION") }
};

private static bool disableTelemetry;
epananth marked this conversation as resolved.
Show resolved Hide resolved

public Telemetry()
{
_metrics = new Dictionary<string, double>();
disableTelemetry = DisableTelemetry();
}

// enable or disable telemetry
internal bool DisableTelemetry()
{
if (!bool.TryParse(Environment.GetEnvironmentVariable("SIGNTOOL_DISABLE_TELEMETRY"), out bool telemetrySwitch))
epananth marked this conversation as resolved.
Show resolved Hide resolved
{
telemetrySwitch = false;
epananth marked this conversation as resolved.
Show resolved Hide resolved
}
return telemetrySwitch;
}

internal void AddMetric(string name, double value)
{
_metrics.Add(name, value);
if (!disableTelemetry)
{
_metrics.Add(name, value);
}
}

internal void SendEvents()
{
// set APPINSIGHTS_INSTRUMENTATIONKEY environment variable to begin tracking telemetry
TelemetryConfiguration configuration = TelemetryConfiguration.CreateDefault();

TelemetryClient telemetryClient = new TelemetryClient(configuration);
if (!disableTelemetry)
{
// set APPINSIGHTS_INSTRUMENTATIONKEY environment variable to begin tracking telemetry
TelemetryConfiguration configuration = TelemetryConfiguration.CreateDefault();
TelemetryClient telemetryClient = new TelemetryClient(configuration);

telemetryClient.TrackEvent(s_sendEventName, properties: s_properties, metrics: _metrics);
telemetryClient.Flush();
_metrics = null;
telemetryClient.TrackEvent(s_sendEventName, properties: s_properties, metrics: _metrics);
telemetryClient.Flush();
_metrics = null;
}
}
}
}