feat(telemetry): anonymous usage telemetry — documented schema, opt-out, public ingest worker (#834)

Adds anonymous usage statistics (commands/tools used, languages indexed,
connecting agents) with a strict, auditable allowlist. Never code, paths,
file/symbol names, queries, or IPs.

- src/telemetry/: zero-dep client — consent resolution (DO_NOT_TRACK >
  CODEGRAPH_TELEMETRY > stored choice > default-on), random machine UUID,
  in-memory counters → capped JSONL buffer → completed-day rollups; sync
  exit-append (survives process.exit) + opportunistic bounded sends; the
  first-run notice gates the first SEND, never local buffering, so the
  installer's consent toggle always precedes it. Off is off: no recording,
  no socket, buffered data deleted.
- codegraph telemetry status|on|off; per-command counting via preAction hook.
- MCP: tool counting after the reply is on the wire (session + proxy
  in-process fallback), agent attribution from initialize clientInfo,
  unref'd daemon flush interval. Zero hot-path cost, zero stdout.
- Installer: visible default-on consent toggle (asked once, never re-asked),
  install/index/uninstall lifecycle events.
- telemetry-worker/: public Cloudflare Worker behind telemetry.getcodegraph.com
  — allowlist validation, IP stripping, per-machine rate limit, forwards to
  PostHog as anonymous events. Ships nowhere with the npm package.
- TELEMETRY.md (field-by-field contract) + README section + design doc.
- 20 unit tests; suite-wide CODEGRAPH_TELEMETRY=0 guard so tests never
  pollute real telemetry. Full suite: 1448 passing.

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Colby Mchenry
2026-06-12 10:37:19 -05:00
committed by GitHub
co-authored by Claude Opus 4.8
parent 7db4c1d2f8
commit 848fde9f59
20 changed files with 3234 additions and 1 deletions
+6
View File
@@ -49,6 +49,7 @@ import {
} from './daemon';
import { connectWithHello, runLocalHandshakeProxy } from './proxy';
import { getDaemonSocketPath } from './daemon-paths';
import { getTelemetry } from '../telemetry';
import { supervisionLostReason } from './ppid-watchdog';
import { treatStdinFailureAsShutdown } from './stdin-teardown';
import { HOST_PPID_ENV } from '../extraction/wasm-runtime-flags';
@@ -245,6 +246,11 @@ export class MCPServer {
* mode — a misbehaving daemon must never block a session from starting.
*/
async start(): Promise<void> {
// Long-lived process (direct / proxy / daemon alike): flush buffered
// telemetry opportunistically. Fire-and-forget + unref'd — adds nothing
// to the handshake path and never keeps the process alive.
getTelemetry().startInterval();
// The detached daemon process itself. Checked before the opt-out so the
// daemon honors the same env it was spawned with (it never sets NO_DAEMON).
if (daemonInternalSet()) {
+13
View File
@@ -28,6 +28,7 @@ import { CodeGraphPackageVersion } from './version';
import { SERVER_INFO, PROTOCOL_VERSION } from './session';
import { SERVER_INSTRUCTIONS } from './server-instructions';
import { getStaticTools } from './tools';
import { getTelemetry, ClientInfo } from '../telemetry';
import type { MCPEngine } from './engine';
/** Default poll cadence for the PPID watchdog (same as the direct server). */
@@ -204,6 +205,10 @@ export async function runLocalHandshakeProxy(deps: LocalHandshakeDeps): Promise<
let daemonStatus: 'connecting' | 'ready' | 'failed' = 'connecting';
let daemonSocket: net.Socket | null = null;
let clientInitId: unknown = undefined; // suppress the daemon's reply to the forwarded initialize
// Telemetry attribution for the in-process fallback only — calls routed to
// the daemon are counted by the daemon's own session (which receives the
// forwarded initialize, clientInfo included), never double-counted here.
let telemetryClient: ClientInfo | undefined;
const pending: string[] = []; // client lines buffered until the daemon resolves
let engine: MCPEngine | null = null;
let engineReady: Promise<void> | null = null;
@@ -246,6 +251,7 @@ export async function runLocalHandshakeProxy(deps: LocalHandshakeDeps): Promise<
const params = (msg.params || {}) as { name: string; arguments?: Record<string, unknown> };
const result = await engine!.getToolHandler().execute(params.name, params.arguments || {});
writeClient({ jsonrpc: '2.0', id, result });
getTelemetry().recordUsage('mcp_tool', params.name, !result.isError, telemetryClient);
} catch (err) {
writeClient({ jsonrpc: '2.0', id, error: { code: -32603, message: err instanceof Error ? err.message : String(err) } });
}
@@ -282,6 +288,13 @@ export async function runLocalHandshakeProxy(deps: LocalHandshakeDeps): Promise<
let msg: JsonRpc; try { msg = JSON.parse(line) as JsonRpc; } catch { routeToDaemon(line); continue; }
if (msg.method === 'initialize') {
clientInitId = msg.id;
const initParams = (msg.params ?? {}) as { clientInfo?: { name?: unknown; version?: unknown } };
if (initParams.clientInfo) {
telemetryClient = {
name: typeof initParams.clientInfo.name === 'string' ? initParams.clientInfo.name : undefined,
version: typeof initParams.clientInfo.version === 'string' ? initParams.clientInfo.version : undefined,
};
}
writeClient({ jsonrpc: '2.0', id: msg.id, result: { protocolVersion: PROTOCOL_VERSION, capabilities: { tools: {} }, serverInfo: SERVER_INFO, instructions: SERVER_INSTRUCTIONS } });
routeToDaemon(line); // prime the daemon so it resolves the project (its reply is suppressed below)
} else if (msg.method === 'tools/list') {
+13
View File
@@ -19,6 +19,7 @@ import { tools } from './tools';
import { SERVER_INSTRUCTIONS, SERVER_INSTRUCTIONS_UNINDEXED } from './server-instructions';
import { CodeGraphPackageVersion } from './version';
import { findNearestCodeGraphRoot } from '../directory';
import { getTelemetry, ClientInfo } from '../telemetry';
/**
* MCP Server Info — kept on the session because some clients log it. The
@@ -82,6 +83,8 @@ export interface MCPSessionOptions {
*/
export class MCPSession {
private clientSupportsRoots = false;
/** From the initialize handshake — attributes usage rollups to the agent host. */
private clientInfo: ClientInfo | undefined;
private rootsAttempted = false;
private resolvePromise: Promise<void> | null = null;
private explicitProjectPath: string | null;
@@ -162,9 +165,16 @@ export class MCPSession {
rootUri?: string;
workspaceFolders?: Array<{ uri: string; name: string }>;
capabilities?: { roots?: unknown };
clientInfo?: { name?: unknown; version?: unknown };
} | undefined;
this.clientSupportsRoots = !!params?.capabilities?.roots;
if (params?.clientInfo) {
this.clientInfo = {
name: typeof params.clientInfo.name === 'string' ? params.clientInfo.name : undefined,
version: typeof params.clientInfo.version === 'string' ? params.clientInfo.version : undefined,
};
}
// Explicit project signal, strongest first: client-provided rootUri /
// workspaceFolders (LSP-style), else the --path the server was launched
@@ -249,6 +259,9 @@ export class MCPSession {
const result = await this.engine.getToolHandler().execute(toolName, toolArgs);
this.transport.sendResult(request.id, result);
// After the reply is on the wire — telemetry must never delay a tool
// response (in-memory increment only; see src/telemetry).
getTelemetry().recordUsage('mcp_tool', toolName, !result.isError, this.clientInfo);
}
/**