Skip to content

Commit ebc887f

Browse files
h9jianggopherbot
authored andcommitted
extension/src: use enum for telemetry key
- Move activationLatency function from goMain.ts to goTelemetry.ts. - Replace add() input parameter type of string with enum type TelemetryKey. Follow variable naming conventions: - enum declaration use UpperCamelCase - enum value use CONSTANT_CASE https://google.github.io/styleguide/tsguide.html#naming-rules-by-identifier-type Bucketing variable name follow prometheus histogram naming: L: less than, LE: less than or equal to G: greater than, GE: greater than or equal to For #3697 Change-Id: I1472bc58752f08b6f0feb1316284cfe10b214f4b Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/661056 LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Hongxiang Jiang <[email protected]> Reviewed-by: Madeline Kalil <[email protected]> Reviewed-by: Peter Weinberger <[email protected]> kokoro-CI: kokoro <[email protected]>
1 parent 5496123 commit ebc887f

File tree

5 files changed

+49
-30
lines changed

5 files changed

+49
-30
lines changed

extension/src/goInstallTools.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import {
3939
import util = require('util');
4040
import vscode = require('vscode');
4141
import { RestartReason } from './language/goLanguageServer';
42-
import { telemetryReporter } from './goTelemetry';
42+
import { TelemetryKey, telemetryReporter } from './goTelemetry';
4343
import { allToolsInformation } from './goToolsInformation';
4444

4545
const STATUS_BAR_ITEM_NAME = 'Go Tools';
@@ -937,7 +937,7 @@ export async function maybeInstallVSCGO(
937937
if (extensionMode === vscode.ExtensionMode.Production && executableFileExists(progPath)) {
938938
return progPath; // reuse existing executable.
939939
}
940-
telemetryReporter.add('vscgo_install', 1);
940+
telemetryReporter.add(TelemetryKey.VSCGO_INSTALL, 1);
941941
const mkdir = util.promisify(fs.mkdir);
942942
await mkdir(path.dirname(progPath), { recursive: true });
943943
const execFile = util.promisify(cp.execFile);
@@ -965,7 +965,7 @@ export async function maybeInstallVSCGO(
965965
await execFile(goBinary, args, { cwd, env });
966966
return progPath;
967967
} catch (e) {
968-
telemetryReporter.add('vscgo_install_fail', 1);
968+
telemetryReporter.add(TelemetryKey.VSCGO_INSTALL_FAIL, 1);
969969
return Promise.reject(`failed to install vscgo - ${e}`);
970970
}
971971
}

extension/src/goMain.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ import { GoExtensionContext } from './context';
7373
import * as commands from './commands';
7474
import { toggleVulncheckCommandFactory } from './goVulncheck';
7575
import { GoTaskProvider } from './goTaskProvider';
76-
import { setTelemetryEnvVars, telemetryReporter } from './goTelemetry';
76+
import { setTelemetryEnvVars, activationLatency, telemetryReporter } from './goTelemetry';
7777
import { experiments } from './experimental';
7878
import { allToolsInformation } from './goToolsInformation';
7979

@@ -240,22 +240,6 @@ export async function activate(ctx: vscode.ExtensionContext): Promise<ExtensionA
240240
return extensionAPI;
241241
}
242242

243-
function activationLatency(duration: number): string {
244-
// TODO: generalize and move to goTelemetry.ts
245-
let bucket = '>=5s';
246-
247-
if (duration < 100) {
248-
bucket = '<100ms';
249-
} else if (duration < 500) {
250-
bucket = '<500ms';
251-
} else if (duration < 1000) {
252-
bucket = '<1s';
253-
} else if (duration < 5000) {
254-
bucket = '<5s';
255-
}
256-
return 'activation_latency:' + bucket;
257-
}
258-
259243
export function deactivate() {
260244
return Promise.all([
261245
goCtx.languageClient?.stop(),

extension/src/goTelemetry.ts

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,41 @@ export function recordTelemetryStartTime(storage: vscode.Memento, date: Date) {
3636
storage.update(TELEMETRY_START_TIME_KEY, date.toJSON());
3737
}
3838

39+
/**
40+
* TelemetryKey represents the different types of telemetry events.
41+
*/
42+
export enum TelemetryKey {
43+
// Indicates the installation of vscgo binary.
44+
VSCGO_INSTALL = 'vscgo_install',
45+
VSCGO_INSTALL_FAIL = 'vscgo_install_fail',
46+
47+
// Indicates the activation latency.
48+
ACTIVATION_LATENCY_L_100MS = 'activation_latency:<100ms',
49+
ACTIVATION_LATENCY_L_500MS = 'activation_latency:<500ms',
50+
ACTIVATION_LATENCY_L_1000MS = 'activation_latency:<1000ms',
51+
ACTIVATION_LATENCY_L_5000MS = 'activation_latency:<5000ms',
52+
ACTIVATION_LATENCY_GE_5S = 'activation_latency:>=5s'
53+
}
54+
55+
/**
56+
* Categorizes a duration into a specific latency bucket.
57+
*
58+
* @param duration The duration in milliseconds.
59+
* @returns The TelemetryKey representing the latency bucket.
60+
*/
61+
export function activationLatency(duration: number): TelemetryKey {
62+
if (duration < 100) {
63+
return TelemetryKey.ACTIVATION_LATENCY_L_100MS;
64+
} else if (duration < 500) {
65+
return TelemetryKey.ACTIVATION_LATENCY_L_500MS;
66+
} else if (duration < 1000) {
67+
return TelemetryKey.ACTIVATION_LATENCY_L_1000MS;
68+
} else if (duration < 5000) {
69+
return TelemetryKey.ACTIVATION_LATENCY_L_5000MS;
70+
}
71+
return TelemetryKey.ACTIVATION_LATENCY_GE_5S;
72+
}
73+
3974
function readTelemetryStartTime(storage: vscode.Memento): Date | null {
4075
const value = storage.get<string | number | Date>(TELEMETRY_START_TIME_KEY);
4176
if (!value) {
@@ -109,12 +144,12 @@ export class TelemetryReporter implements vscode.Disposable {
109144
/**
110145
* Adds a numeric value to a counter associated with the given key.
111146
*/
112-
public add(key: string, value: number) {
147+
public add(key: TelemetryKey, value: number) {
113148
if (value <= 0) {
114149
return;
115150
}
116-
key = key.replace(/[\s\n]/g, '_');
117-
this._counters[key] = (this._counters[key] || 0) + value;
151+
const sanitized = key.replace(/[\s\n]/g, '_');
152+
this._counters[sanitized] = (this._counters[sanitized] || 0) + value;
118153
}
119154

120155
/**
@@ -197,7 +232,7 @@ export const telemetryReporter = new TelemetryReporter();
197232

198233
// TODO(hyangah): consolidate the list of all the telemetries and bucketting functions.
199234

200-
export function addTelemetryEvent(name: string, count: number) {
235+
export function addTelemetryEvent(name: TelemetryKey, count: number) {
201236
telemetryReporter.add(name, count);
202237
}
203238

extension/src/goTest.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import {
2525
SuiteToTestMap,
2626
getTestFunctions
2727
} from './testUtils';
28-
import type { GoRunTestCodeLensProvider } from './goRunTestCodelens';
2928

3029
// lastTestConfig holds a reference to the last executed TestConfig which allows
3130
// the last test to be easily re-executed.

extension/test/gopls/telemetry.test.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { describe, it } from 'mocha';
99
import {
1010
GOPLS_MAYBE_PROMPT_FOR_TELEMETRY,
1111
TELEMETRY_START_TIME_KEY,
12+
TelemetryKey,
1213
TelemetryReporter,
1314
TelemetryService,
1415
recordTelemetryStartTime
@@ -189,8 +190,8 @@ describe('# telemetry reporter using vscgo', async function () {
189190
});
190191

191192
it('add succeeds before telemetryReporter.setTool runs', () => {
192-
sut.add('hello', 1);
193-
sut.add('world', 2);
193+
sut.add(TelemetryKey.VSCGO_INSTALL, 1);
194+
sut.add(TelemetryKey.VSCGO_INSTALL_FAIL, 2);
194195
});
195196

196197
it('flush is noop before setTool', async () => {
@@ -202,13 +203,13 @@ describe('# telemetry reporter using vscgo', async function () {
202203
sut.setTool(vscgo);
203204
await sut.flush();
204205
const readAll = fs.readFileSync(counterfile).toString();
205-
assert(readAll.includes('hello 1\n') && readAll.includes('world 2\n'), readAll);
206+
assert(readAll.includes('vscgo_install 1\n') && readAll.includes('vscgo_install_fail 2\n'), readAll);
206207
});
207208

208209
it('dispose triggers flush', async () => {
209-
sut.add('bye', 3);
210+
sut.add(TelemetryKey.ACTIVATION_LATENCY_GE_5S, 3);
210211
await sut.dispose();
211212
const readAll = fs.readFileSync(counterfile).toString();
212-
assert(readAll.includes('bye 3\n'), readAll);
213+
assert(readAll.includes('activation_latency:>=5s 3\n'), readAll);
213214
});
214215
});

0 commit comments

Comments
 (0)