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

Commit 7262890

Browse files
committed
wip
1 parent 171a340 commit 7262890

File tree

1 file changed

+44
-35
lines changed

1 file changed

+44
-35
lines changed

src/GitHub.VisualStudio/Services/UsageTracker.cs

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,23 @@ namespace GitHub.Services
1818
public sealed class UsageTracker : IUsageTracker, IDisposable
1919
{
2020
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-
private const string TelemetryVersionProperty = "TelemetryVersion";
2221

23-
private const string EventNameBase = "vs/github/extension/";
24-
private const string PropertyBase = "vs.github.extension.";
22+
private const string EventNamePrefix = "vs/github/";
23+
private const string PropertyPrefix = "vs.github.";
24+
25+
static class Event
26+
{
27+
public const string UsageTracker = EventNamePrefix + nameof(UsageTracker);
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+
}
2538

2639
static readonly ILogger log = LogManager.ForContext<UsageTracker>();
2740
readonly IGitHubServiceProvider gitHubServiceProvider;
@@ -56,50 +69,46 @@ public void Dispose()
5669
public async Task IncrementCounter(Expression<Func<UsageModel.MeasuresModel, int>> counter)
5770
{
5871
await Initialize();
59-
var data = await service.ReadLocalData();
60-
var usage = await GetCurrentReport(data);
72+
6173
var property = (MemberExpression)counter.Body;
6274
var propertyInfo = (PropertyInfo)property.Member;
63-
var eventName = propertyInfo.Name;
64-
log.Verbose("Increment counter {Name}", eventName);
75+
var counterName = propertyInfo.Name;
76+
log.Verbose("Increment counter {Name}", counterName);
77+
78+
var updateTask = UpdateUsageMetrics(propertyInfo, counterName);
6579

66-
await ReportGitHubMetrics(propertyInfo, eventName, usage, data);
80+
LogTelemetryEvent(counterName);
6781

68-
ReportMicrosoftTelemetry(eventName);
82+
await updateTask;
6983
}
7084

71-
static void ReportMicrosoftTelemetry(string eventName)
85+
void LogTelemetryEvent(string counterName)
7286
{
73-
try
74-
{
75-
const string numberOfPrefix = "numberof";
76-
if (eventName.IndexOf(numberOfPrefix, StringComparison.InvariantCultureIgnoreCase) != -1)
77-
{
78-
eventName = eventName.Substring(numberOfPrefix.Length);
79-
}
80-
81-
var operation = new TelemetryEvent(EventNameBase + eventName);
82-
operation.Properties[PropertyBase + TelemetryVersionProperty] = TelemetryVersion;
83-
TelemetryService.DefaultSession.PostEvent(operation);
84-
}
85-
catch (Exception e)
87+
const string numberOfPrefix = "numberof";
88+
if (counterName.StartsWith(numberOfPrefix, StringComparison.OrdinalIgnoreCase))
8689
{
87-
log.Error(e, "Error recording Microsoft Telmetry");
90+
counterName = counterName.Substring(numberOfPrefix.Length);
8891
}
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] = connectionManager.Connections.Any(x => x.HostAddress.IsGitHubDotCom());
98+
operation.Properties[Property.IsEnterpriseUser] = connectionManager.Connections.Any(x => !x.HostAddress.IsGitHubDotCom());
99+
100+
TelemetryService.DefaultSession.PostEvent(operation);
89101
}
90102

91-
async Task ReportGitHubMetrics(PropertyInfo propertyInfo, string eventName, UsageModel usage, UsageData data)
103+
async Task UpdateUsageMetrics(PropertyInfo propertyInfo, string eventName)
92104
{
93-
try
94-
{
95-
var value = (int) propertyInfo.GetValue(usage.Measures);
96-
propertyInfo.SetValue(usage.Measures, value + 1);
97-
await service.WriteLocalData(data);
98-
}
99-
catch (Exception e)
100-
{
101-
log.Error(e, "Error recording GitHub Metrics");
102-
}
105+
var data = await service.ReadLocalData();
106+
var usage = await GetCurrentReport(data);
107+
108+
var value = (int) propertyInfo.GetValue(usage.Measures);
109+
propertyInfo.SetValue(usage.Measures, value + 1);
110+
111+
await service.WriteLocalData(data);
103112
}
104113

105114
IDisposable StartTimer()

0 commit comments

Comments
 (0)