Skip to content

Commit a62c89e

Browse files
feat(telemetry): wire up telemetry to Event Bus and bootstrap
Complete the telemetry integration with OpenCode: Integration module (integration.ts): - Subscribe to Session, Message, and File events from Event Bus - Automatic tracking of sessions, turns, tool calls, and file changes - Uses Instance.state for automatic cleanup on disposal - Graceful shutdown flushes pending telemetry data Bootstrap integration: - Initialize telemetry during InstanceBootstrap - Telemetry is now automatically enabled on startup (respects consent) - Errors during initialization are logged but don't block startup Exports: - Telemetry.initIntegration() - Initialize with Event Bus integration - Telemetry.shutdownIntegration() - Explicit shutdown (automatic via Instance.dispose) - Telemetry.completeTurn() - Finalize assistant response - Telemetry.userMessage() - Record user input - Telemetry.retry() - Record turn retry Implements: [CodeQ] Wire up telemetry module
1 parent 8ce5643 commit a62c89e

3 files changed

Lines changed: 355 additions & 5 deletions

File tree

packages/opencode/src/project/bootstrap.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { Log } from "@/util/log"
1313
import { ShareNext } from "@/share/share-next"
1414
import { Snapshot } from "../snapshot"
1515
import { Truncate } from "../tool/truncation"
16+
import { Telemetry } from "@/telemetry"
1617

1718
export async function InstanceBootstrap() {
1819
Log.Default.info("bootstrapping", { directory: Instance.directory })
@@ -27,6 +28,12 @@ export async function InstanceBootstrap() {
2728
Snapshot.init()
2829
Truncate.init()
2930

31+
// Initialize qBraid telemetry (CodeQ-specific)
32+
// This is a no-op if telemetry is disabled by consent or config
33+
await Telemetry.initIntegration().catch((error) => {
34+
Log.Default.warn("telemetry initialization failed", { error })
35+
})
36+
3037
Bus.subscribe(Command.Event.Executed, async (payload) => {
3138
if (payload.properties.name === Command.Default.INIT) {
3239
await Project.setInitialized(Instance.project.id)

packages/opencode/src/telemetry/index.ts

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
* Usage:
88
* import { Telemetry } from "./telemetry"
99
*
10-
* // Initialize at startup
10+
* // Initialize at startup (with Event Bus integration)
11+
* await Telemetry.initIntegration()
12+
*
13+
* // Or initialize manually without Event Bus
1114
* await Telemetry.initialize(authToken)
1215
*
1316
* // Start a session
@@ -33,14 +36,43 @@ import {
3336
type TelemetryCollector,
3437
} from "./collector"
3538
import { getConsentStatus, isTelemetryEnabled, clearConsentCache } from "./consent"
39+
import {
40+
initTelemetryIntegration,
41+
shutdownTelemetryIntegration,
42+
finalizeTurn,
43+
recordUserTurn,
44+
recordRetry,
45+
} from "./integration"
3646
import type { ConsentStatus, TelemetrySession, TelemetryTurn } from "./types"
3747

3848
export namespace Telemetry {
3949
/**
40-
* Initialize the telemetry system
50+
* Initialize the telemetry system with Event Bus integration
51+
*
52+
* This is the recommended way to initialize telemetry. It:
53+
* - Checks consent based on user tier
54+
* - Subscribes to relevant Event Bus events
55+
* - Automatically tracks sessions, messages, tool calls, and file changes
56+
*/
57+
export async function initIntegration(): Promise<void> {
58+
await initTelemetryIntegration()
59+
}
60+
61+
/**
62+
* Shutdown the telemetry system with Event Bus integration
63+
*
64+
* Unsubscribes from events and flushes pending data.
65+
* Should be called on application exit.
66+
*/
67+
export async function shutdownIntegration(): Promise<void> {
68+
await shutdownTelemetryIntegration()
69+
}
70+
71+
/**
72+
* Initialize the telemetry system (manual mode, no Event Bus)
4173
*
42-
* Must be called before any other telemetry functions.
43-
* Will check consent and configure collection accordingly.
74+
* Use this if you want to manually control telemetry collection
75+
* without automatic Event Bus integration.
4476
*
4577
* @param authToken - Optional qBraid auth token for consent lookup
4678
*/
@@ -49,7 +81,7 @@ export namespace Telemetry {
4981
}
5082

5183
/**
52-
* Shutdown the telemetry system
84+
* Shutdown the telemetry system (manual mode)
5385
*
5486
* Flushes any pending data and cleans up resources.
5587
* Should be called on application exit.
@@ -58,6 +90,26 @@ export namespace Telemetry {
5890
await shutdownTelemetry()
5991
}
6092

93+
/**
94+
* Finalize a turn when assistant response is complete
95+
*
96+
* Called to record the assistant's response and complete the turn.
97+
* This should be called after the LLM streaming is complete.
98+
*/
99+
export const completeTurn = finalizeTurn
100+
101+
/**
102+
* Record a user message (start of a turn)
103+
*
104+
* Use this for manual recording when not using Event Bus integration.
105+
*/
106+
export const userMessage = recordUserTurn
107+
108+
/**
109+
* Record that a turn was retried
110+
*/
111+
export const retry = recordRetry
112+
61113
/**
62114
* Start collecting for a new session
63115
*

0 commit comments

Comments
 (0)