This repository has been archived by the owner on May 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathtelemetry.ts
130 lines (109 loc) · 3.19 KB
/
telemetry.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import * as vscode from "vscode";
export interface RequestEvent {
request: string;
requestTime: number;
lspVersion: string;
uri?: string;
errorClass?: string;
errorMessage?: string;
backtrace?: string;
params?: string;
rubyVersion: string;
yjitEnabled: boolean;
}
export interface ConfigurationEvent {
namespace: string;
field: string;
value: string;
}
export interface CodeLensEvent {
type: "test" | "debug" | "test_in_terminal";
lspVersion: string;
}
export type TelemetryEvent = RequestEvent | ConfigurationEvent | CodeLensEvent;
const ONE_DAY_IN_MS = 24 * 60 * 60 * 1000;
export interface TelemetryApi {
sendEvent(event: TelemetryEvent): Promise<void>;
}
class DevelopmentApi implements TelemetryApi {
// eslint-disable-next-line @typescript-eslint/require-await
async sendEvent(event: TelemetryEvent): Promise<void> {
// eslint-disable-next-line no-console
console.log(event);
}
}
export class Telemetry {
private api?: TelemetryApi;
private readonly context: vscode.ExtensionContext;
constructor(context: vscode.ExtensionContext, api?: TelemetryApi) {
this.context = context;
if (context.extensionMode === vscode.ExtensionMode.Development && !api) {
this.api = new DevelopmentApi();
} else {
this.api = api;
}
}
async sendEvent(event: TelemetryEvent) {
if (await this.initialize()) {
this.api!.sendEvent(event);
}
}
async sendConfigurationEvents() {
const lastConfigurationTelemetry: number | undefined =
this.context.globalState.get("rubyLsp.lastConfigurationTelemetry");
if (
lastConfigurationTelemetry &&
Date.now() - lastConfigurationTelemetry <= ONE_DAY_IN_MS
) {
return;
}
const promises: Promise<void>[] = [
{ namespace: "workbench", field: "colorTheme" },
{ namespace: "rubyLsp", field: "enableExperimentalFeatures" },
{ namespace: "rubyLsp", field: "yjit" },
{ namespace: "rubyLsp", field: "rubyVersionManager" },
{ namespace: "rubyLsp", field: "formatter" },
].map(({ namespace, field }) => {
return this.sendEvent({
namespace,
field,
value: (
vscode.workspace.getConfiguration(namespace).get(field) ?? ""
).toString(),
});
});
const enabledFeatures = vscode.workspace
.getConfiguration("rubyLsp")
.get("enabledFeatures")!;
Object.entries(enabledFeatures).forEach(([field, value]) => {
promises.push(
this.sendEvent({
namespace: "rubyLsp.enabledFeatures",
field,
value: value.toString(),
}),
);
});
await Promise.all(promises);
this.context.globalState.update(
"rubyLsp.lastConfigurationTelemetry",
Date.now(),
);
}
async sendCodeLensEvent(type: CodeLensEvent["type"], lspVersion: string) {
await this.sendEvent({ type, lspVersion });
}
private async initialize(): Promise<boolean> {
try {
if (!this.api) {
this.api = await vscode.commands.executeCommand(
"ruby-lsp.getPrivateTelemetryApi",
);
}
return Boolean(this.api);
} catch (_error) {
// Do nothing if no telemetry api is available
return false;
}
}
}