77using GitHub . Logging ;
88using GitHub . Models ;
99using GitHub . Settings ;
10+ using Microsoft . VisualStudio . Telemetry ;
11+ using Microsoft . VisualStudio . Text . Editor ;
1012using Microsoft . VisualStudio . Threading ;
1113using Serilog ;
1214using 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