Skip to content

Commit cb91241

Browse files
committed
extension/src/goTelemetry: set telemetry env vars
GOTELEMETRY_GOPLS_CLIENT_START_TIME and GOTELEMETRY_GOPLS_CLIENT_TOKEN are the env vars read by gopls v0.17+. See CL 589517 Use them to forward the vscode-go's telemetry decision as we transition to gopls based prompting logic. For golang/go#67821 Change-Id: Ib3e014217ae7718a8677c7d8f181005fbc0577f6 Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/595535 Commit-Queue: Hyang-Ah Hana Kim <[email protected]> Reviewed-by: Robert Findley <[email protected]> kokoro-CI: kokoro <[email protected]>
1 parent c05badc commit cb91241

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

extension/src/goMain.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ import { GoExtensionContext } from './context';
7272
import * as commands from './commands';
7373
import { toggleVulncheckCommandFactory } from './goVulncheck';
7474
import { GoTaskProvider } from './goTaskProvider';
75-
import { telemetryReporter } from './goTelemetry';
75+
import { setTelemetryEnvVars, telemetryReporter } from './goTelemetry';
7676

7777
const goCtx: GoExtensionContext = {};
7878

@@ -99,6 +99,8 @@ export async function activate(ctx: vscode.ExtensionContext): Promise<ExtensionA
9999
setWorkspaceState(ctx.workspaceState);
100100
setEnvironmentVariableCollection(ctx.environmentVariableCollection);
101101

102+
setTelemetryEnvVars(ctx.globalState, process.env);
103+
102104
const cfg = getGoConfig();
103105
WelcomePanel.activate(ctx, goCtx);
104106

extension/src/goTelemetry.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,27 @@ export class TelemetryService {
215215

216216
// exported for testing.
217217
public hashMachineID(salt?: string): number {
218-
const hash = createHash('md5').update(`${vscode.env.machineId}${salt}`).digest('hex');
219-
return parseInt(hash.substring(0, 8), 16);
218+
return hashMachineID(salt);
220219
}
221220
}
221+
222+
// Set telemetry env vars for gopls. See gopls/internal/server/prompt.go
223+
// TODO(hyangah): add an integration testing after gopls v0.17 becomes available.
224+
export function setTelemetryEnvVars(globalState: vscode.Memento, env: NodeJS.ProcessEnv) {
225+
if (!env['GOTELEMETRY_GOPLS_CLIENT_TOKEN']) {
226+
env['GOTELEMETRY_GOPLS_CLIENT_TOKEN'] = `${hashMachineID() + 1}`; // [1, 1000]
227+
}
228+
if (!env['GOTELEMETRY_GOPLS_CLIENT_START_TIME']) {
229+
const start = readTelemetryStartTime(globalState);
230+
if (start) {
231+
const unixSec = Math.floor(start.getTime() / 1000);
232+
env['GOTELEMETRY_GOPLS_CLIENT_START_TIME'] = `${unixSec}`;
233+
}
234+
}
235+
}
236+
237+
// Map vscode.env.machineId to an integer in [0, 1000).
238+
function hashMachineID(salt?: string): number {
239+
const hash = createHash('md5').update(`${vscode.env.machineId}${salt}`).digest('hex');
240+
return parseInt(hash.substring(0, 8), 16) % 1000;
241+
}

0 commit comments

Comments
 (0)