Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 40c2635

Browse files
committed
Only use VS Telemetry on VS 2017 and above
Microsoft.VisualStudio.Telemetry isn't available on VS 2015.
1 parent 28caef7 commit 40c2635

File tree

3 files changed

+95
-40
lines changed

3 files changed

+95
-40
lines changed

src/GitHub.VisualStudio/GitHub.VisualStudio.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
<Compile Include="Services\ShowDialogService.cs" />
130130
<Compile Include="Services\UsageService.cs" />
131131
<Compile Include="Services\UsageTracker.cs" />
132+
<Compile Include="Services\VisualStudioUsageTracker.cs" />
132133
<Compile Include="Services\VSGitExtFactory.cs" />
133134
<Compile Include="Settings\Constants.cs" />
134135
<Compile Include="Services\ConnectionManager.cs" />

src/GitHub.VisualStudio/Services/UsageTracker.cs

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
using GitHub.Logging;
88
using GitHub.Models;
99
using GitHub.Settings;
10-
using Microsoft.VisualStudio.Telemetry;
11-
using Microsoft.VisualStudio.Text.Editor;
1210
using Microsoft.VisualStudio.Threading;
1311
using Serilog;
1412
using Task = System.Threading.Tasks.Task;
@@ -17,25 +15,6 @@ namespace GitHub.Services
1715
{
1816
public sealed class UsageTracker : IUsageTracker, IDisposable
1917
{
20-
private const int TelemetryVersion = 1; // Please update the version every time you want to indicate a change in telemetry logic when the extension itself is updated
21-
22-
private const string EventNamePrefix = "vs/github/usagetracker/";
23-
private const string PropertyPrefix = "vs.github.";
24-
25-
static class Event
26-
{
27-
public const string UsageTracker = EventNamePrefix + "increment-counter";
28-
}
29-
30-
static class Property
31-
{
32-
public const string TelemetryVersion = PropertyPrefix + nameof(TelemetryVersion);
33-
public const string CounterName = PropertyPrefix + nameof(CounterName);
34-
public const string ExtensionVersion = PropertyPrefix + nameof(ExtensionVersion);
35-
public const string IsGitHubUser = PropertyPrefix + nameof(IsGitHubUser);
36-
public const string IsEnterpriseUser = PropertyPrefix + nameof(IsEnterpriseUser);
37-
}
38-
3918
static readonly ILogger log = LogManager.ForContext<UsageTracker>();
4019
readonly IGitHubServiceProvider gitHubServiceProvider;
4120

@@ -45,6 +24,7 @@ static class Property
4524
IConnectionManager connectionManager;
4625
readonly IPackageSettings userSettings;
4726
IVSServices vsservices;
27+
IUsageTracker visualStudioUsageTracker;
4828
IDisposable timer;
4929
bool firstTick = true;
5030

@@ -77,27 +57,13 @@ public async Task IncrementCounter(Expression<Func<UsageModel.MeasuresModel, int
7757

7858
var updateTask = UpdateUsageMetrics(propertyInfo);
7959

80-
LogTelemetryEvent(counterName);
81-
82-
await updateTask;
83-
}
84-
85-
void LogTelemetryEvent(string counterName)
86-
{
87-
const string numberOfPrefix = "numberof";
88-
if (counterName.StartsWith(numberOfPrefix, StringComparison.OrdinalIgnoreCase))
60+
if (visualStudioUsageTracker != null)
8961
{
90-
counterName = counterName.Substring(numberOfPrefix.Length);
62+
// Not available on Visual Studio 2015
63+
await visualStudioUsageTracker.IncrementCounter(counter);
9164
}
9265

93-
var operation = new TelemetryEvent(Event.UsageTracker);
94-
operation.Properties[Property.TelemetryVersion] = TelemetryVersion;
95-
operation.Properties[Property.CounterName] = counterName;
96-
operation.Properties[Property.ExtensionVersion] = AssemblyVersionInformation.Version;
97-
operation.Properties[Property.IsGitHubUser] = IsGitHubUser;
98-
operation.Properties[Property.IsEnterpriseUser] = IsEnterpriseUser;
99-
100-
TelemetryService.DefaultSession.PostEvent(operation);
66+
await updateTask;
10167
}
10268

10369
bool IsEnterpriseUser =>
@@ -111,7 +77,7 @@ async Task UpdateUsageMetrics(PropertyInfo propertyInfo)
11177
var data = await service.ReadLocalData();
11278
var usage = await GetCurrentReport(data);
11379

114-
var value = (int) propertyInfo.GetValue(usage.Measures);
80+
var value = (int)propertyInfo.GetValue(usage.Measures);
11581
propertyInfo.SetValue(usage.Measures, value + 1);
11682

11783
await service.WriteLocalData(data);
@@ -134,6 +100,15 @@ async Task Initialize()
134100
client = gitHubServiceProvider.TryGetService<IMetricsService>();
135101
connectionManager = gitHubServiceProvider.GetService<IConnectionManager>();
136102
vsservices = gitHubServiceProvider.GetService<IVSServices>();
103+
104+
// Only create VisualStudioUsageTracker on Visual Studio 2017 and above
105+
var dte = gitHubServiceProvider.GetService<EnvDTE.DTE>();
106+
if (new Version(dte.Version) >= new Version(15, 0))
107+
{
108+
log.Verbose("Creating VisualStudioUsageTracker");
109+
visualStudioUsageTracker = new VisualStudioUsageTracker(connectionManager);
110+
}
111+
137112
initialized = true;
138113
}
139114

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using System;
2+
using System.Linq;
3+
using System.Linq.Expressions;
4+
using System.Reflection;
5+
using GitHub.Models;
6+
using Microsoft.VisualStudio.Telemetry;
7+
using Task = System.Threading.Tasks.Task;
8+
9+
namespace GitHub.Services
10+
{
11+
/// <summary>
12+
/// Implementation of <see cref="IUsageTracker" /> that uses the built in Visual Studio telemetry.
13+
/// </summary>
14+
/// <remarks>
15+
/// This should only be created on Visual Studio 2017 and above.
16+
/// </remarks>
17+
public sealed class VisualStudioUsageTracker : IUsageTracker
18+
{
19+
const int TelemetryVersion = 1; // Please update the version every time you want to indicate a change in telemetry logic when the extension itself is updated
20+
21+
const string EventNamePrefix = "vs/github/usagetracker/";
22+
const string PropertyPrefix = "vs.github.";
23+
24+
readonly IConnectionManager connectionManager;
25+
26+
public VisualStudioUsageTracker(IConnectionManager connectionManager)
27+
{
28+
this.connectionManager = connectionManager;
29+
}
30+
31+
public Task IncrementCounter(Expression<Func<UsageModel.MeasuresModel, int>> counter)
32+
{
33+
var property = (MemberExpression)counter.Body;
34+
var propertyInfo = (PropertyInfo)property.Member;
35+
var counterName = propertyInfo.Name;
36+
LogTelemetryEvent(counterName);
37+
38+
return Task.CompletedTask;
39+
}
40+
41+
void LogTelemetryEvent(string counterName)
42+
{
43+
const string numberOfPrefix = "numberof";
44+
if (counterName.StartsWith(numberOfPrefix, StringComparison.OrdinalIgnoreCase))
45+
{
46+
counterName = counterName.Substring(numberOfPrefix.Length);
47+
}
48+
49+
var operation = new TelemetryEvent(Event.UsageTracker);
50+
operation.Properties[Property.TelemetryVersion] = TelemetryVersion;
51+
operation.Properties[Property.CounterName] = counterName;
52+
operation.Properties[Property.ExtensionVersion] = AssemblyVersionInformation.Version;
53+
operation.Properties[Property.IsGitHubUser] = IsGitHubUser;
54+
operation.Properties[Property.IsEnterpriseUser] = IsEnterpriseUser;
55+
56+
TelemetryService.DefaultSession.PostEvent(operation);
57+
}
58+
59+
bool IsEnterpriseUser =>
60+
connectionManager?.Connections.Any(x => !x.HostAddress.IsGitHubDotCom()) ?? false;
61+
62+
bool IsGitHubUser =>
63+
connectionManager?.Connections.Any(x => x.HostAddress.IsGitHubDotCom()) ?? false;
64+
65+
static class Event
66+
{
67+
public const string UsageTracker = EventNamePrefix + "increment-counter";
68+
}
69+
70+
static class Property
71+
{
72+
public const string TelemetryVersion = PropertyPrefix + nameof(TelemetryVersion);
73+
public const string CounterName = PropertyPrefix + nameof(CounterName);
74+
public const string ExtensionVersion = PropertyPrefix + nameof(ExtensionVersion);
75+
public const string IsGitHubUser = PropertyPrefix + nameof(IsGitHubUser);
76+
public const string IsEnterpriseUser = PropertyPrefix + nameof(IsEnterpriseUser);
77+
}
78+
}
79+
}

0 commit comments

Comments
 (0)