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

Commit 4f2a900

Browse files
Merge pull request #2393 from github/visual-studio-telemetry
Adding Visual Studio Telemetry
2 parents ce9e5c4 + ab65b6e commit 4f2a900

File tree

2 files changed

+70
-8
lines changed

2 files changed

+70
-8
lines changed

src/GitHub.VisualStudio/GitHub.VisualStudio.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,9 @@
296296
</ItemGroup>
297297
<ItemGroup>
298298
<PackageReference Include="Madskristensen.VisualStudio.SDK" Version="14.3.75-pre" />
299+
<PackageReference Include="Microsoft.VisualStudio.Telemetry">
300+
<Version>15.0.691-master31907920</Version>
301+
</PackageReference>
299302
<PackageReference Include="Microsoft.VSSDK.BuildTools">
300303
<Version>15.8.3252</Version>
301304
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>

src/GitHub.VisualStudio/Services/UsageTracker.cs

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
using GitHub.Logging;
88
using GitHub.Models;
99
using GitHub.Settings;
10+
using Microsoft.VisualStudio.Telemetry;
11+
using Microsoft.VisualStudio.Text.Editor;
1012
using Microsoft.VisualStudio.Threading;
1113
using Serilog;
1214
using Task = System.Threading.Tasks.Task;
@@ -15,14 +17,33 @@ namespace GitHub.Services
1517
{
1618
public sealed class UsageTracker : IUsageTracker, IDisposable
1719
{
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+
1839
static readonly ILogger log = LogManager.ForContext<UsageTracker>();
1940
readonly IGitHubServiceProvider gitHubServiceProvider;
2041

2142
bool initialized;
2243
IMetricsService client;
23-
IUsageService service;
44+
readonly IUsageService service;
2445
IConnectionManager connectionManager;
25-
IPackageSettings userSettings;
46+
readonly IPackageSettings userSettings;
2647
IVSServices vsservices;
2748
IDisposable timer;
2849
bool firstTick = true;
@@ -48,13 +69,51 @@ public void Dispose()
4869
public async Task IncrementCounter(Expression<Func<UsageModel.MeasuresModel, int>> counter)
4970
{
5071
await Initialize();
51-
var data = await service.ReadLocalData();
52-
var usage = await GetCurrentReport(data);
72+
5373
var property = (MemberExpression)counter.Body;
5474
var propertyInfo = (PropertyInfo)property.Member;
55-
log.Verbose("Increment counter {Name}", propertyInfo.Name);
56-
var value = (int)propertyInfo.GetValue(usage.Measures);
75+
var counterName = propertyInfo.Name;
76+
log.Verbose("Increment counter {Name}", counterName);
77+
78+
var updateTask = UpdateUsageMetrics(propertyInfo);
79+
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))
89+
{
90+
counterName = counterName.Substring(numberOfPrefix.Length);
91+
}
92+
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);
101+
}
102+
103+
bool IsEnterpriseUser =>
104+
this.connectionManager?.Connections.Any(x => !x.HostAddress.IsGitHubDotCom()) ?? false;
105+
106+
bool IsGitHubUser =>
107+
this.connectionManager?.Connections.Any(x => x.HostAddress.IsGitHubDotCom()) ?? false;
108+
109+
async Task UpdateUsageMetrics(PropertyInfo propertyInfo)
110+
{
111+
var data = await service.ReadLocalData();
112+
var usage = await GetCurrentReport(data);
113+
114+
var value = (int) propertyInfo.GetValue(usage.Measures);
57115
propertyInfo.SetValue(usage.Measures, value + 1);
116+
58117
await service.WriteLocalData(data);
59118
}
60119

@@ -138,8 +197,8 @@ async Task<UsageModel> GetCurrentReport(UsageData data)
138197
current.Dimensions.AppVersion = AssemblyVersionInformation.Version;
139198
current.Dimensions.VSVersion = vsservices.VSVersion;
140199

141-
current.Dimensions.IsGitHubUser = connectionManager.Connections.Any(x => x.HostAddress.IsGitHubDotCom());
142-
current.Dimensions.IsEnterpriseUser = connectionManager.Connections.Any(x => !x.HostAddress.IsGitHubDotCom());
200+
current.Dimensions.IsGitHubUser = IsGitHubUser;
201+
current.Dimensions.IsEnterpriseUser = IsEnterpriseUser;
143202
return current;
144203
}
145204

0 commit comments

Comments
 (0)