forked from DEVtheOPS/opencode-plugin-otel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
292 lines (273 loc) · 10.1 KB
/
Copy pathindex.ts
File metadata and controls
292 lines (273 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import type { Plugin } from "@opencode-ai/plugin"
import { SeverityNumber } from "@opentelemetry/api-logs"
import { logs } from "@opentelemetry/api-logs"
import { ROOT_CONTEXT, SpanStatusCode, trace } from "@opentelemetry/api"
import { AGENT_NAME } from "@arizeai/openinference-semantic-conventions"
import pkg from "../package.json" with { type: "json" }
import type {
EventSessionCreated,
EventSessionIdle,
EventSessionDeleted,
EventSessionError,
EventSessionStatus,
EventMessageUpdated,
EventMessagePartUpdated,
EventPermissionUpdated,
EventPermissionReplied,
EventSessionDiff,
EventCommandExecuted,
} from "@opencode-ai/sdk"
import { LEVELS, type Level, type HandlerContext } from "./types.ts"
import { loadConfig, parseAttributePairs, resolveHelperPath, resolveLogLevel } from "./config.ts"
import { probeEndpoint } from "./probe.ts"
import { setupOtel, createInstruments } from "./otel.ts"
import { remoteParentContext } from "./trace-context.ts"
import { handleSessionCreated, handleSessionIdle, handleSessionDeleted, handleSessionError, handleSessionStatus } from "./handlers/session.ts"
import { handleMessageUpdated, handleMessagePartUpdated, startMessageSpan } from "./handlers/message.ts"
import { handlePermissionUpdated, handlePermissionReplied } from "./handlers/permission.ts"
import { handleSessionDiff, handleCommandExecuted } from "./handlers/activity.ts"
import { agentAttrs, getSessionAgentMeta } from "./util.ts"
const PLUGIN_VERSION: string = (pkg as { version?: string }).version ?? "unknown"
/**
* OpenCode plugin that exports session telemetry via OpenTelemetry (OTLP over gRPC or HTTP/protobuf).
* Instruments metrics (sessions, tokens, cost, lines of code, commits, tool durations)
* and structured log events. All instrumentation is gated on `OPENCODE_ENABLE_TELEMETRY`.
*/
export const OtelPlugin: Plugin = async ({ project, client, directory, worktree }) => {
const config = loadConfig()
const otlpHeadersHelper = resolveHelperPath(config.otlpHeadersHelper, directory, worktree)
let minLevel: Level = "info"
const log: HandlerContext["log"] = async (level, message, extra) => {
if (LEVELS[level] < LEVELS[minLevel]) return
await client.app.log({ body: { service: "opencode-plugin-otel", level, message, extra } })
}
if (!config.enabled) {
await log("info", "telemetry disabled (set OPENCODE_ENABLE_TELEMETRY to enable)")
return {}
}
await log("info", "starting up", {
version: PLUGIN_VERSION,
endpoint: config.endpoint,
protocol: config.protocol,
metricsInterval: config.metricsInterval,
logsInterval: config.logsInterval,
metricPrefix: config.metricPrefix,
headersHelperSet: !!config.otlpHeadersHelper,
})
await log("debug", "config loaded", {
headersSet: !!config.otlpHeaders,
headersHelperSet: !!config.otlpHeadersHelper,
resourceAttributesSet: !!config.resourceAttributes,
spanAttributesSet: !!config.spanAttributes,
})
const probe = await probeEndpoint(config.endpoint)
if (probe.ok) {
await log("info", "OTLP endpoint reachable", { endpoint: config.endpoint, ms: probe.ms })
} else {
await log("warn", "OTLP endpoint unreachable — exports may fail", {
endpoint: config.endpoint,
error: probe.error,
})
}
const { meterProvider, loggerProvider, tracerProvider } = await setupOtel(
config.endpoint,
config.protocol,
config.metricsInterval,
config.logsInterval,
PLUGIN_VERSION,
config.otlpHeaders,
otlpHeadersHelper,
)
await log("info", "OTel SDK initialized")
const instruments = createInstruments(config.metricPrefix)
const logger = logs.getLogger("com.opencode")
const emitLog: HandlerContext["emitLog"] = (record) => {
if (!config.logsEnabled) return
logger.emit(record)
}
const tracer = trace.getTracer("com.opencode")
const remoteContext = remoteParentContext(config.traceparent, config.tracestate)
if (config.traceparent && !remoteContext) {
await log("warn", "invalid OPENCODE_TRACEPARENT ignored", { traceparentLength: config.traceparent.length })
}
const rootContext = remoteContext ? () => remoteContext : () => ROOT_CONTEXT
const pendingToolSpans = new Map()
const pendingPermissions = new Map()
const sessionTotals = new Map()
const sessionDiffTotals = new Map()
const sessionSpans = new Map()
const messageSpans = new Map()
const sessionInputs = new Map()
const messageOutputs = new Map()
const { disabledMetrics, disabledTraces } = config
const commonAttrs = {
...parseAttributePairs(config.spanAttributes),
"project.id": project.id,
} as const
if (disabledMetrics.size > 0) {
await log("info", "metrics disabled", { disabled: [...disabledMetrics] })
}
if (disabledTraces.size > 0) {
await log("info", "traces disabled", { disabled: [...disabledTraces] })
}
if (!config.logsEnabled) {
await log("info", "OTLP log events disabled")
}
const ctx: HandlerContext = {
log,
emitLog,
instruments,
commonAttrs,
pendingToolSpans,
pendingPermissions,
sessionTotals,
sessionDiffTotals,
disabledMetrics,
disabledTraces,
tracer,
tracePrefix: config.metricPrefix,
rootContext,
sessionSpans,
messageSpans,
sessionInputs,
messageOutputs,
}
async function shutdown() {
for (const [sessionID, sessionSpan] of sessionSpans) {
const totals = sessionTotals.get(sessionID)
if (totals) {
sessionSpan.setAttributes({
[AGENT_NAME]: totals.agent,
"session.total_tokens": totals.tokens,
"session.total_cost_usd": totals.cost,
"session.total_messages": totals.messages,
})
}
sessionSpan.setStatus({ code: SpanStatusCode.OK })
sessionSpan.end()
}
sessionSpans.clear()
await Promise.allSettled([meterProvider.shutdown(), loggerProvider.shutdown(), tracerProvider.shutdown()])
}
process.on("SIGTERM", () => { shutdown().then(() => process.exit(0)).catch(() => process.exit(1)) })
process.on("SIGINT", () => { shutdown().then(() => process.exit(0)).catch(() => process.exit(1)) })
process.on("beforeExit", () => { shutdown().catch(() => {}) })
const safe = <T extends unknown[]>(
name: string,
fn: (...args: T) => Promise<void> | void,
): ((...args: T) => Promise<void>) =>
async (...args: T) => {
try {
await fn(...args)
} catch (err) {
await log("error", `otel: unhandled error in ${name}`, {
error: err instanceof Error ? err.message : String(err),
stack: err instanceof Error ? err.stack : undefined,
})
}
}
return {
config: async (cfg) => {
if (cfg.logLevel) {
const next = resolveLogLevel(cfg.logLevel, minLevel)
if (next !== minLevel) {
minLevel = next
await log("info", `log level set to "${minLevel}"`)
} else if (cfg.logLevel.toLowerCase() !== minLevel) {
await log("warn", `unknown log level "${cfg.logLevel}", keeping "${minLevel}"`)
}
}
},
"chat.message": safe("chat.message", async (input, output) => {
const agent = input.agent ?? "unknown"
const { agentType } = getSessionAgentMeta(input.sessionID, ctx)
const totals = sessionTotals.get(input.sessionID)
if (totals) totals.agent = agent
const sessionSpan = sessionSpans.get(input.sessionID)
if (sessionSpan) sessionSpan.setAttributes({ [AGENT_NAME]: agent, "agent.type": agentType })
const promptText = output.parts.map((part) => {
switch (part.type) {
case "text":
return part.text
case "file":
return part.filename ?? part.url
case "agent":
return part.name
case "subtask":
return part.description
default:
return ""
}
}).filter(Boolean).join("\n")
sessionInputs.set(input.sessionID, promptText)
const promptLength = promptText.length
emitLog({
severityNumber: SeverityNumber.INFO,
severityText: "INFO",
timestamp: Date.now(),
observedTimestamp: Date.now(),
body: "user_prompt",
attributes: {
"event.name": "user_prompt",
"session.id": input.sessionID,
...agentAttrs(agent, agentType),
prompt_length: promptLength,
model: input.model
? `${input.model.providerID}/${input.model.modelID}`
: "unknown",
...commonAttrs,
},
})
}),
event: safe("event", async ({ event }) => {
switch (event.type) {
case "session.created":
await handleSessionCreated(event as EventSessionCreated, ctx)
break
case "session.idle":
handleSessionIdle(event as EventSessionIdle, ctx)
break
case "session.deleted":
handleSessionDeleted(event as EventSessionDeleted, ctx)
break
case "session.error":
handleSessionError(event as EventSessionError, ctx)
break
case "session.status":
handleSessionStatus(event as EventSessionStatus, ctx)
break
case "session.diff":
handleSessionDiff(event as EventSessionDiff, ctx)
break
case "command.executed":
handleCommandExecuted(event as EventCommandExecuted, ctx)
break
case "permission.updated":
handlePermissionUpdated(event as EventPermissionUpdated, ctx)
break
case "permission.replied":
handlePermissionReplied(event as EventPermissionReplied, ctx)
break
case "message.updated": {
const msgEvt = event as EventMessageUpdated
const info = msgEvt.properties.info
if (info.role === "assistant" && !info.time?.completed) {
startMessageSpan(
info.sessionID,
info.id,
info.modelID ?? "unknown",
info.providerID ?? "unknown",
info.time?.created ?? Date.now(),
ctx,
)
}
await handleMessageUpdated(msgEvt, ctx)
break
}
case "message.part.updated":
await handleMessagePartUpdated(event as EventMessagePartUpdated, ctx)
break
}
}),
}
}