|
5 | 5 | 'use strict';
|
6 | 6 |
|
7 | 7 | import TelemetryReporter from 'vscode-extension-telemetry';
|
| 8 | +import { getExperimentationServiceAsync, IExperimentationService, IExperimentationTelemetry, TargetPopulation } from 'vscode-tas-client'; |
8 | 9 | import * as util from './common';
|
| 10 | +import { PackageVersion } from './packageVersion'; |
9 | 11 |
|
10 | 12 | interface IPackageInfo {
|
11 | 13 | name: string;
|
12 | 14 | version: string;
|
13 | 15 | }
|
14 | 16 |
|
15 |
| -let telemetryReporter: TelemetryReporter | null; |
| 17 | +export class ExperimentationTelemetry implements IExperimentationTelemetry { |
| 18 | + private sharedProperties: Record<string, string> = {}; |
| 19 | + |
| 20 | + constructor(private baseReporter: TelemetryReporter) { } |
| 21 | + |
| 22 | + sendTelemetryEvent(eventName: string, properties?: Record<string, string>, measurements?: Record<string, number>): void { |
| 23 | + this.baseReporter.sendTelemetryEvent( |
| 24 | + eventName, |
| 25 | + { |
| 26 | + ...this.sharedProperties, |
| 27 | + ...properties |
| 28 | + }, |
| 29 | + measurements |
| 30 | + ); |
| 31 | + } |
| 32 | + |
| 33 | + sendTelemetryErrorEvent(eventName: string, properties?: Record<string, string>, _measurements?: Record<string, number>): void { |
| 34 | + this.baseReporter.sendTelemetryErrorEvent(eventName, { |
| 35 | + ...this.sharedProperties, |
| 36 | + ...properties |
| 37 | + }); |
| 38 | + } |
| 39 | + |
| 40 | + setSharedProperty(name: string, value: string): void { |
| 41 | + this.sharedProperties[name] = value; |
| 42 | + } |
| 43 | + |
| 44 | + postEvent(eventName: string, props: Map<string, string>): void { |
| 45 | + const event: Record<string, string> = {}; |
| 46 | + for (const [key, value] of props) { |
| 47 | + event[key] = value; |
| 48 | + } |
| 49 | + this.sendTelemetryEvent(eventName, event); |
| 50 | + } |
| 51 | + |
| 52 | + dispose(): Promise<any> { |
| 53 | + return this.baseReporter.dispose(); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +let initializationPromise: Promise<IExperimentationService> | undefined; |
| 58 | +let experimentationTelemetry: ExperimentationTelemetry | undefined; |
16 | 59 | const appInsightsKey: string = "AIF-d9b70cd4-b9f9-4d70-929b-a071c400b217";
|
17 | 60 |
|
18 | 61 | export function activate(): void {
|
19 | 62 | try {
|
20 |
| - telemetryReporter = createReporter(); |
| 63 | + if (util.extensionContext) { |
| 64 | + const packageInfo: IPackageInfo = getPackageInfo(); |
| 65 | + if (packageInfo) { |
| 66 | + let targetPopulation: TargetPopulation; |
| 67 | + const userVersion: PackageVersion = new PackageVersion(packageInfo.version); |
| 68 | + if (userVersion.suffix === "") { |
| 69 | + targetPopulation = TargetPopulation.Public; |
| 70 | + } else if (userVersion.suffix === "insiders") { |
| 71 | + targetPopulation = TargetPopulation.Insiders; |
| 72 | + } else { |
| 73 | + targetPopulation = TargetPopulation.Internal; |
| 74 | + } |
| 75 | + experimentationTelemetry = new ExperimentationTelemetry(new TelemetryReporter(packageInfo.name, packageInfo.version, appInsightsKey)); |
| 76 | + initializationPromise = getExperimentationServiceAsync(packageInfo.name, packageInfo.version, targetPopulation, experimentationTelemetry, util.extensionContext.globalState); |
| 77 | + } |
| 78 | + } |
21 | 79 | } catch (e) {
|
22 |
| - // can't really do much about this |
| 80 | + // Handle error with a try/catch, but do nothing for errors. |
23 | 81 | }
|
24 | 82 | }
|
25 | 83 |
|
26 |
| -export function deactivate(): void { |
27 |
| - if (telemetryReporter) { |
28 |
| - telemetryReporter.dispose(); |
29 |
| - } |
| 84 | +export async function getExperimentationService(): Promise<IExperimentationService | undefined> { |
| 85 | + return initializationPromise; |
30 | 86 | }
|
31 | 87 |
|
32 |
| -export function logDebuggerEvent(eventName: string, properties?: { [key: string]: string }): void { |
33 |
| - if (telemetryReporter) { |
34 |
| - const eventNamePrefix: string = "cppdbg/VS/Diagnostics/Debugger/"; |
35 |
| - telemetryReporter.sendTelemetryEvent(eventNamePrefix + eventName, properties); |
| 88 | +export async function deactivate(): Promise<void> { |
| 89 | + if (initializationPromise) { |
| 90 | + try { |
| 91 | + await initializationPromise; |
| 92 | + } catch (e) { |
| 93 | + // Continue even if we were not able to initialize the experimentation platform. |
| 94 | + } |
| 95 | + if (experimentationTelemetry) { |
| 96 | + experimentationTelemetry.dispose(); |
| 97 | + } |
36 | 98 | }
|
37 | 99 | }
|
38 | 100 |
|
39 |
| -export function logLanguageServerEvent(eventName: string, properties?: { [key: string]: string }, metrics?: { [key: string]: number }): void { |
40 |
| - if (telemetryReporter) { |
41 |
| - const eventNamePrefix: string = "C_Cpp/LanguageServer/"; |
42 |
| - telemetryReporter.sendTelemetryEvent(eventNamePrefix + eventName, properties, metrics); |
| 101 | +export async function logDebuggerEvent(eventName: string, properties?: { [key: string]: string }): Promise<void> { |
| 102 | + try { |
| 103 | + await initializationPromise; |
| 104 | + } catch (e) { |
| 105 | + // Continue even if we were not able to initialize the experimentation platform. |
| 106 | + } |
| 107 | + if (experimentationTelemetry) { |
| 108 | + const eventNamePrefix: string = "cppdbg/VS/Diagnostics/Debugger/"; |
| 109 | + experimentationTelemetry.sendTelemetryEvent(eventNamePrefix + eventName, properties); |
43 | 110 | }
|
44 | 111 | }
|
45 | 112 |
|
46 |
| -function createReporter(): TelemetryReporter | null { |
47 |
| - if (util.extensionContext) { |
48 |
| - const packageInfo: IPackageInfo = getPackageInfo(); |
49 |
| - if (packageInfo) { |
50 |
| - return new TelemetryReporter(packageInfo.name, packageInfo.version, appInsightsKey); |
51 |
| - } |
| 113 | +export async function logLanguageServerEvent(eventName: string, properties?: { [key: string]: string }, metrics?: { [key: string]: number }): Promise<void> { |
| 114 | + try { |
| 115 | + await initializationPromise; |
| 116 | + } catch (e) { |
| 117 | + // Continue even if we were not able to initialize the experimentation platform. |
| 118 | + } |
| 119 | + if (experimentationTelemetry) { |
| 120 | + const eventNamePrefix: string = "C_Cpp/LanguageServer/"; |
| 121 | + experimentationTelemetry.sendTelemetryEvent(eventNamePrefix + eventName, properties, metrics); |
52 | 122 | }
|
53 |
| - return null; |
54 | 123 | }
|
55 | 124 |
|
56 | 125 | function getPackageInfo(): IPackageInfo {
|
|
0 commit comments