Skip to content

Commit

Permalink
extension/src/goTelemetry: set telemetry env vars
Browse files Browse the repository at this point in the history
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 <hyangah@gmail.com>
Reviewed-by: Robert Findley <rfindley@google.com>
kokoro-CI: kokoro <noreply+kokoro@google.com>
  • Loading branch information
hyangah committed Jun 27, 2024
1 parent c05badc commit cb91241
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
4 changes: 3 additions & 1 deletion extension/src/goMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ import { GoExtensionContext } from './context';
import * as commands from './commands';
import { toggleVulncheckCommandFactory } from './goVulncheck';
import { GoTaskProvider } from './goTaskProvider';
import { telemetryReporter } from './goTelemetry';
import { setTelemetryEnvVars, telemetryReporter } from './goTelemetry';

const goCtx: GoExtensionContext = {};

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

setTelemetryEnvVars(ctx.globalState, process.env);

const cfg = getGoConfig();
WelcomePanel.activate(ctx, goCtx);

Expand Down
24 changes: 22 additions & 2 deletions extension/src/goTelemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,27 @@ export class TelemetryService {

// exported for testing.
public hashMachineID(salt?: string): number {
const hash = createHash('md5').update(`${vscode.env.machineId}${salt}`).digest('hex');
return parseInt(hash.substring(0, 8), 16);
return hashMachineID(salt);
}
}

// Set telemetry env vars for gopls. See gopls/internal/server/prompt.go
// TODO(hyangah): add an integration testing after gopls v0.17 becomes available.
export function setTelemetryEnvVars(globalState: vscode.Memento, env: NodeJS.ProcessEnv) {
if (!env['GOTELEMETRY_GOPLS_CLIENT_TOKEN']) {
env['GOTELEMETRY_GOPLS_CLIENT_TOKEN'] = `${hashMachineID() + 1}`; // [1, 1000]
}
if (!env['GOTELEMETRY_GOPLS_CLIENT_START_TIME']) {
const start = readTelemetryStartTime(globalState);
if (start) {
const unixSec = Math.floor(start.getTime() / 1000);
env['GOTELEMETRY_GOPLS_CLIENT_START_TIME'] = `${unixSec}`;
}
}
}

// Map vscode.env.machineId to an integer in [0, 1000).
function hashMachineID(salt?: string): number {
const hash = createHash('md5').update(`${vscode.env.machineId}${salt}`).digest('hex');
return parseInt(hash.substring(0, 8), 16) % 1000;
}

0 comments on commit cb91241

Please sign in to comment.