Skip to content

Commit

Permalink
Added option to opt out participating in telemetry. Building multiple…
Browse files Browse the repository at this point in the history
… assemblies
  • Loading branch information
StefanKert committed Apr 27, 2019
1 parent 9251aca commit 125ee49
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 27 deletions.
7 changes: 7 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,11 @@
</PackageReference>
</ItemGroup>

<PropertyGroup>
<DefineConstants Condition="'$(Configuration)' == 'VSIXGallery' ">$(DefineConstants);VSIXGallery</DefineConstants>
<DefineConstants Condition="'$(Configuration)' == 'Marketplace' ">$(DefineConstants);MARKETPLACE</DefineConstants>
<DefineConstants Condition="'$(Configuration)' == 'Release' ">$(DefineConstants);VSIX</DefineConstants>
<DefineConstants Condition="'$(Configuration)' == 'Debug' ">$(DefineConstants);VSIX</DefineConstants>
</PropertyGroup>

</Project>
26 changes: 18 additions & 8 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ jobs:
- job: Build
pool:
vmImage: vs2017-win2016
strategy:
matrix:
Config_Release:
BuildConfiguration: Release
BuildOutputDirectory: Zip
Config_Marketplace:
BuildConfiguration: Marketplace
BuildOutputDirectory: Marketplace
Config_VSIXGallery:
BuildConfiguration: VSIXGallery
BuildOutputDirectory: VSIXGallery

steps:
- task: DotNetCoreCLI@2
inputs:
Expand All @@ -25,6 +37,9 @@ jobs:
displayName: Install NBGV tool
continueOnError: true

- powershell: |
mkdir $(Build.ArtifactStagingDirectory)\$(BuildOutputDirectory)
- script: nbgv cloud -a
displayName: Set Version

Expand All @@ -47,7 +62,7 @@ jobs:
- task: CopyFiles@2
inputs:
contents: '**/bin/$(BuildConfiguration)/*.vsix'
targetFolder: $(Build.ArtifactStagingDirectory)/artifacts
targetFolder: $(Build.ArtifactStagingDirectory)/$(BuildOutputDirectory)
flattenFolders: true

- task: DotNetCoreCLI@2
Expand All @@ -57,13 +72,8 @@ jobs:
arguments: -c $(BuildConfiguration) --no-build --no-restore
displayName: Run Unittests

- powershell: |
& "$env:userprofile/.nuget/packages/xunit.runner.console/2.4.1/tools/net472/xunit.console.x86.exe" (Get-Childitem -Filter "BuildVision.IntegrationTests.dll" -Recurse | Where-Object {$_.FullName -like "*\bin\$(BuildConfiguration)\*" }).FullName
displayName: Execute Integration Tests
enabled: false
- task: PublishBuildArtifacts@1
inputs:
PathtoPublish: $(Build.ArtifactStagingDirectory)/artifacts
ArtifactName: artifacts
PathtoPublish: $(Build.ArtifactStagingDirectory)/$(BuildOutputDirectory)
ArtifactName: $(BuildOutputDirectory)
publishLocation: Container
23 changes: 11 additions & 12 deletions src/BuildVision.Common/Diagnostics/DiagnosticsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,24 @@ public static class DiagnosticsClient
private static bool _initialized;
private static TelemetryClient _client;

public static bool ParticipateInTelemetry { get; set; } = true;

public static void Initialize(string apiKey)
public static void Initialize(string edition, string vsVersion, string apiKey)
{
if (!string.IsNullOrWhiteSpace(apiKey))
{
TelemetryConfiguration.Active.InstrumentationKey = apiKey;
TelemetryConfiguration.Active.TelemetryChannel.DeveloperMode = Debugger.IsAttached;
TelemetryConfiguration.Active.TelemetryInitializers.Add(new VersionTelemetry());
TelemetryConfiguration.Active.TelemetryInitializers.Add(new SessionTelemetry());
TelemetryConfiguration.Active.TelemetryInitializers.Add(new SessionTelemetry(vsVersion, edition));

_initialized = true;

_client = new TelemetryClient();
}
}

public static void OnExit()
{
if (!_initialized)
if (!_initialized || !ParticipateInTelemetry)
{
return;
}
Expand All @@ -41,27 +40,27 @@ public static void OnExit()

public static void TrackEvent(string eventName, IDictionary<string, string> properties = null, IDictionary<string, double> metrics = null)
{
if (!_initialized)
if (!_initialized || !ParticipateInTelemetry)
{
return;
}

_client.TrackEvent(eventName, properties, metrics);
}

public static void TrackTrace(string evt)
public static void TrackTrace(string trace)
{
if (!_initialized)
if (!_initialized || !ParticipateInTelemetry)
{
return;
}

_client.TrackTrace(evt);
_client.TrackTrace(trace);
}

public static void Notify(Exception exception)
public static void TrackException(Exception exception)
{
if (!_initialized)
if (!_initialized || !ParticipateInTelemetry)
{
return;
}
Expand All @@ -71,7 +70,7 @@ public static void Notify(Exception exception)

public static void TrackPageView(string pageName)
{
if (!_initialized)
if (!_initialized || !ParticipateInTelemetry)
{
return;
}
Expand Down
21 changes: 19 additions & 2 deletions src/BuildVision.Common/Diagnostics/SessionTelemetry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,20 @@ class SessionTelemetry : ITelemetryInitializer
private readonly string _userName;
private readonly string _operatingSystem = RuntimeInformation.OSDescription?.Replace("Microsoft ", ""); // Shorter description
private readonly string _session = Guid.NewGuid().ToString();
private readonly string _vsVersion;
private readonly string _dteEdition;

public SessionTelemetry()
#if VSIXGallery
private const string Channel = "vsixgallery";
#elif MARKETPLACE
private const string Channel = "marketplace";
#elif VSIX
private const string Channel = "vsix";
#else
private const string Channel = "vsix";
#endif

public SessionTelemetry(string vsVersion, string dteEdition)
{
try
{
Expand All @@ -28,11 +40,14 @@ public SessionTelemetry()
{
// No user id
}

_vsVersion = vsVersion;
_dteEdition = dteEdition;
}

public void Initialize(ITelemetry telemetry)
{
telemetry.Context.GlobalProperties["Environment"] = "release";
telemetry.Context.GlobalProperties["Environment"] = Channel;
// Always default to development if we're in the debugger
if (Debugger.IsAttached)
{
Expand All @@ -46,6 +61,8 @@ public void Initialize(ITelemetry telemetry)

telemetry.Context.Session.Id = _session;
telemetry.Context.Device.OperatingSystem = _operatingSystem;
telemetry.Context.Device.Model = _vsVersion;
telemetry.Context.Device.Type = _dteEdition;
}
}
}
8 changes: 5 additions & 3 deletions src/BuildVision.Common/Diagnostics/VersionTelemetry.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using Microsoft.ApplicationInsights.Channel;
Expand All @@ -12,9 +13,10 @@ class VersionTelemetry : ITelemetryInitializer

public VersionTelemetry()
{
_appVersion = typeof(DiagnosticsClient).Assembly.GetCustomAttributes<AssemblyMetadataAttribute>()
.FirstOrDefault(ama => string.Equals(ama.Key, "CloudBuildNumber", StringComparison.OrdinalIgnoreCase))
?.Value;
var assembly = Assembly.GetExecutingAssembly();
var versionInfo = FileVersionInfo.GetVersionInfo(assembly.Location);

_appVersion = versionInfo.ProductVersion.ToString();
}

public void Initialize(ITelemetry telemetry)
Expand Down
11 changes: 10 additions & 1 deletion src/BuildVision.UI/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/BuildVision.UI/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -607,4 +607,7 @@ on build done</value>
<data name="UpToDateProjectsIndicator_Header" xml:space="preserve">
<value>Up-to-date Projects</value>
</data>
<data name="GeneralSettings_ParticipateInTelemetry" xml:space="preserve">
<value>Help improve BuildVision by providing anynonymous usage data and crash reports</value>
</data>
</root>
4 changes: 4 additions & 0 deletions src/BuildVision.UI/Settings/GeneralSettingsControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@
Content="{x:Static res:Resources.BuildMessagesSettings_HideUpToDateTargets}"
IsChecked="{Binding HideUpToDateTargets}" />

<CheckBox Margin="8"
Content="{x:Static res:Resources.GeneralSettings_ParticipateInTelemetry}"
IsChecked="{Binding ParticipateInTelemetry}" />

<Grid Margin="4,0,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
Expand Down
3 changes: 3 additions & 0 deletions src/BuildVision.UI/Settings/Models/GeneralSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ public class GeneralSettings : SettingsBase

public bool FillProjectListOnBuildBegin { get; set; }

public bool ParticipateInTelemetry { get; set; }

public GeneralSettings()
{
BuildProgressSettings = new BuildProgressSettings();
EnableStatusBarOutput = true;
IndicatorsPanelVisible = true;
ParticipateInTelemetry = true;
}
}
}
22 changes: 21 additions & 1 deletion src/BuildVision/Core/BuildVisionPackage.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows;
Expand Down Expand Up @@ -68,12 +69,31 @@ public BuildVisionPackage()
{
Application.Current.DispatcherUnhandledException += Current_DispatcherUnhandledException;
}

DiagnosticsClient.Initialize(GetEdition(), VisualStudioVersion.ToString(), "c437ad44-0c76-4006-968d-42d4369bc0ed");
}

public static Version VisualStudioVersion => GetGlobalService(typeof(DTE)) is DTE dte
? new Version(int.Parse(dte.Version.Split('.')[0], CultureInfo.InvariantCulture), 0)
: new Version(0, 0, 0, 0);

private string GetEdition()
{
try
{
_dte2 = GetService(typeof(DTE)) as DTE2;
return _dte2.Edition;
}
catch (Exception ex)
{
return "";
}
}

private void Current_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
_logger.Fatal(e.Exception, "Unhandled Exception");
DiagnosticsClient.Notify(e.Exception);
DiagnosticsClient.TrackException(e.Exception);
}

protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
Expand Down
5 changes: 5 additions & 0 deletions src/BuildVision/Services/PackageSettingsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Diagnostics;
using System.Windows;
using BuildVision.Common;
using BuildVision.Common.Diagnostics;
using BuildVision.Common.Logging;
using BuildVision.UI.Settings.Models;
using Microsoft.VisualStudio.Settings;
Expand Down Expand Up @@ -31,6 +32,8 @@ public void Save()
{
SaveSettings();
SettingsChanged?.Invoke();

DiagnosticsClient.ParticipateInTelemetry = Settings.GeneralSettings.ParticipateInTelemetry;
}

private void SaveSettings()
Expand Down Expand Up @@ -59,6 +62,8 @@ private void LoadSettings()
{
Settings = new ControlSettings();
}

DiagnosticsClient.ParticipateInTelemetry = Settings.GeneralSettings.ParticipateInTelemetry;
}
catch (Exception ex)
{
Expand Down

0 comments on commit 125ee49

Please sign in to comment.