Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/github-authentication/src/common/experimentationService.ts
3320 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import * as vscode from 'vscode';
7
import TelemetryReporter from '@vscode/extension-telemetry';
8
import { getExperimentationService, IExperimentationService, IExperimentationTelemetry, TargetPopulation } from 'vscode-tas-client';
9
10
export class ExperimentationTelemetry implements IExperimentationTelemetry {
11
private sharedProperties: Record<string, string> = {};
12
private experimentationServicePromise: Promise<IExperimentationService> | undefined;
13
14
constructor(private readonly context: vscode.ExtensionContext, private baseReporter: TelemetryReporter) { }
15
16
private async createExperimentationService(): Promise<IExperimentationService> {
17
let targetPopulation: TargetPopulation;
18
switch (vscode.env.uriScheme) {
19
case 'vscode':
20
targetPopulation = TargetPopulation.Public;
21
break;
22
case 'vscode-insiders':
23
targetPopulation = TargetPopulation.Insiders;
24
break;
25
case 'vscode-exploration':
26
targetPopulation = TargetPopulation.Internal;
27
break;
28
case 'code-oss':
29
targetPopulation = TargetPopulation.Team;
30
break;
31
default:
32
targetPopulation = TargetPopulation.Public;
33
break;
34
}
35
36
const id = this.context.extension.id;
37
const version = this.context.extension.packageJSON.version;
38
const experimentationService = getExperimentationService(id, version, targetPopulation, this, this.context.globalState);
39
await experimentationService.initialFetch;
40
return experimentationService;
41
}
42
43
/**
44
* @returns A promise that you shouldn't need to await because this is just telemetry.
45
*/
46
async sendTelemetryEvent(eventName: string, properties?: Record<string, string>, measurements?: Record<string, number>) {
47
if (!this.experimentationServicePromise) {
48
this.experimentationServicePromise = this.createExperimentationService();
49
}
50
await this.experimentationServicePromise;
51
52
this.baseReporter.sendTelemetryEvent(
53
eventName,
54
{
55
...this.sharedProperties,
56
...properties,
57
},
58
measurements,
59
);
60
}
61
62
/**
63
* @returns A promise that you shouldn't need to await because this is just telemetry.
64
*/
65
async sendTelemetryErrorEvent(
66
eventName: string,
67
properties?: Record<string, string>,
68
_measurements?: Record<string, number>
69
) {
70
if (!this.experimentationServicePromise) {
71
this.experimentationServicePromise = this.createExperimentationService();
72
}
73
await this.experimentationServicePromise;
74
75
this.baseReporter.sendTelemetryErrorEvent(eventName, {
76
...this.sharedProperties,
77
...properties,
78
});
79
}
80
81
setSharedProperty(name: string, value: string): void {
82
this.sharedProperties[name] = value;
83
}
84
85
postEvent(eventName: string, props: Map<string, string>): void {
86
const event: Record<string, string> = {};
87
for (const [key, value] of props) {
88
event[key] = value;
89
}
90
this.sendTelemetryEvent(eventName, event);
91
}
92
93
dispose(): Promise<any> {
94
return this.baseReporter.dispose();
95
}
96
}
97
98