fix: Posthog user ID use Continue userId or machine ID

This commit is contained in:
Ting-Wai To 2025-08-08 14:39:52 -07:00
parent a96170cef8
commit c59dcc9d0c
5 changed files with 151 additions and 2 deletions

7
package-lock.json generated
View file

@ -40,6 +40,7 @@
"lowlight": "^3.3.0",
"minimatch": "^10.0.3",
"node-fetch": "^3.3.2",
"node-machine-id": "^1.1.12",
"open": "^10.1.0",
"posthog-node": "^5.6.0",
"react": "^19.1.0",
@ -12581,6 +12582,12 @@
"url": "https://opencollective.com/node-fetch"
}
},
"node_modules/node-machine-id": {
"version": "1.1.12",
"resolved": "https://registry.npmjs.org/node-machine-id/-/node-machine-id-1.1.12.tgz",
"integrity": "sha512-QNABxbrPa3qEIfrE6GOJ7BYIuignnJw7iQ2YPbc3Nla1HzRJjXzZOiikfF8m7eAMfichLt3M4VgLOetqgDmgGQ==",
"license": "MIT"
},
"node_modules/normalize-package-data": {
"version": "6.0.2",
"resolved": "https://registry.npmjs.org/normalize-package-data/-/normalize-package-data-6.0.2.tgz",

View file

@ -67,6 +67,7 @@
"lowlight": "^3.3.0",
"minimatch": "^10.0.3",
"node-fetch": "^3.3.2",
"node-machine-id": "^1.1.12",
"open": "^10.1.0",
"posthog-node": "^5.6.0",
"react": "^19.1.0",

10
spec/telemtry.md Normal file
View file

@ -0,0 +1,10 @@
# Continue anonymous Posthog telemetry
## Behavior
- Used by Continue for product metrics (not used by customers)
- uses public posthog key in repo
- CONTINUE_CLI_ENABLE_TELEMETRY can be set to false to disable
- non-anonymous and private data like code is never sent to posthog
- Event user ids are the Continue user id is signed in, or a unique machine id if not
- Current events are slash command usage and chat calls

View file

@ -0,0 +1,113 @@
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { machineIdSync } from "node-machine-id";
import { vi, describe, it, beforeEach, afterEach, expect } from "vitest";
// Mock the auth module and node-machine-id
vi.mock("../auth/workos.js", () => ({
loadAuthConfig: vi.fn(),
isAuthenticatedConfig: vi.fn(),
}));
vi.mock("node-machine-id", () => ({
machineIdSync: vi.fn(() => "test-machine-id"),
}));
// Import after mocks are set up
import { loadAuthConfig, isAuthenticatedConfig } from "../auth/workos.js";
import { PosthogService } from "./posthogService.js";
describe("PosthogService", () => {
let tempDir: string;
beforeEach(() => {
// Create a temporary directory for each test
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "posthog-test-"));
// Mock os.homedir to return our temp directory
vi.spyOn(os, "homedir").mockReturnValue(tempDir);
// Clear environment variable
delete process.env.CONTINUE_CLI_ENABLE_TELEMETRY;
// Reset all mocks
vi.clearAllMocks();
});
afterEach(() => {
// Clean up temp directory
fs.rmSync(tempDir, { recursive: true, force: true });
// Restore mocks
vi.restoreAllMocks();
});
describe("uniqueId generation", () => {
it("should use Continue user id when signed in", () => {
const mockUserId = "user123";
vi.mocked(loadAuthConfig).mockReturnValue({ userId: mockUserId } as any);
vi.mocked(isAuthenticatedConfig).mockReturnValue(true);
const service = new PosthogService();
// Access private field for testing
const uniqueId = (service as any).uniqueId;
expect(uniqueId).toBe(mockUserId);
expect(loadAuthConfig).toHaveBeenCalled();
expect(isAuthenticatedConfig).toHaveBeenCalled();
});
it("should use machine id when not signed in", () => {
const mockMachineId = "test-machine-id";
vi.mocked(loadAuthConfig).mockReturnValue(null);
vi.mocked(isAuthenticatedConfig).mockReturnValue(false);
vi.mocked(machineIdSync).mockReturnValue(mockMachineId);
const service = new PosthogService();
// Access private field for testing
const uniqueId = (service as any).uniqueId;
expect(uniqueId).toBe(mockMachineId);
expect(loadAuthConfig).toHaveBeenCalled();
expect(isAuthenticatedConfig).toHaveBeenCalled();
expect(machineIdSync).toHaveBeenCalled();
});
it("should use machine id when auth config is environment-based", () => {
const mockMachineId = "test-machine-id";
vi.mocked(loadAuthConfig).mockReturnValue({ accessToken: "token" } as any);
vi.mocked(isAuthenticatedConfig).mockReturnValue(false);
vi.mocked(machineIdSync).mockReturnValue(mockMachineId);
const service = new PosthogService();
// Access private field for testing
const uniqueId = (service as any).uniqueId;
expect(uniqueId).toBe(mockMachineId);
expect(machineIdSync).toHaveBeenCalled();
});
});
describe("telemetry enabled/disabled", () => {
it("should be enabled by default", () => {
const service = new PosthogService();
expect(service.isEnabled).toBe(true);
});
it("should be disabled when CONTINUE_CLI_ENABLE_TELEMETRY is 0", () => {
process.env.CONTINUE_CLI_ENABLE_TELEMETRY = "0";
const service = new PosthogService();
expect(service.isEnabled).toBe(false);
});
});
});

View file

@ -1,14 +1,17 @@
import os from "node:os";
import * as node_machine_id from 'node-machine-id';
import type { PostHog as PostHogType } from "posthog-node";
export class PosthogService {
import { loadAuthConfig, isAuthenticatedConfig } from "../auth/workos.js";
export class PosthogService {
private os: string | undefined;
private uniqueId: string;
constructor() {
this.os = os.platform();
this.uniqueId = "NOT_UNIQUE";
this.uniqueId = this.getEventUserId();
}
get isEnabled() {
@ -33,6 +36,21 @@ export class PosthogService {
return this._client;
}
/**
* - Continue user id if signed in
* - Unique machine id if not signed in
*/
private getEventUserId(): string {
const authConfig = loadAuthConfig();
if (isAuthenticatedConfig(authConfig)) {
return authConfig.userId;
}
// Fall back to unique machine id if not signed in
return node_machine_id.machineIdSync();
}
async capture(event: string, properties: { [key: string]: any }) {
const client = await this.getClient();
if (!client) {