From dbe104b69286dc165c5e9be77ec0b2ffa2e1e683 Mon Sep 17 00:00:00 2001 From: enternal111 <48542794+enternal111@users.noreply.github.com> Date: Sat, 14 Nov 2026 19:39:38 +0800 Subject: [PATCH 01/10] fix: gate span content capture behind OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT Span attributes that embed user or model content (input.value, output.value, llm.input_messages, llm.output_messages, session run span prompt text, and tool parameters/inputs/outputs) are now emitted only when capture is explicitly enabled, either via the new captureContent plugin option or the standard OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT environment variable (true/1). The default is privacy-first: content is not captured, matching the OpenTelemetry GenAI semantic-conventions guidance for message content. Fixes #144 --- src/config.ts | 4 ++ src/handlers/session.ts | 2 +- src/handlers/step.ts | 6 +- src/handlers/tool.ts | 6 +- src/plugin.ts | 1 + src/types.ts | 1 + tests/handlers/session.test.ts | 97 +++++++++++++++++------------- tests/handlers/step.test.ts | 54 ++++++++++++----- tests/handlers/tool.test.ts | 64 +++++++++++--------- tests/helpers.ts | 1 + tests/integration/opencode.test.ts | 3 + 11 files changed, 147 insertions(+), 92 deletions(-) diff --git a/src/config.ts b/src/config.ts index a0425a9..ca7e675 100644 --- a/src/config.ts +++ b/src/config.ts @@ -15,6 +15,7 @@ export type PluginConfig = { logsEnabled: boolean capturePromptInLogs: boolean captureModelContext: boolean + captureContent: boolean combineReasoningTokens: boolean logLevel: string | undefined endpoint: string @@ -62,6 +63,7 @@ export type OtelPluginOptions = { logsEnabled?: boolean capturePromptInLogs?: boolean captureModelContext?: boolean + captureContent?: boolean combineReasoningTokens?: boolean logLevel?: string endpoint?: string @@ -201,6 +203,8 @@ export function loadConfig(options: OtelPluginOptions = {}): PluginConfig { logsEnabled: pickBoolean(resolvedOptions.logsEnabled) ?? !hasNonEmptyEnv("OPENCODE_DISABLE_LOGS"), capturePromptInLogs: pickBoolean(resolvedOptions.capturePromptInLogs) ?? hasNonEmptyEnv("OPENCODE_CAPTURE_PROMPT_IN_LOGS"), captureModelContext: pickBoolean(resolvedOptions.captureModelContext) ?? hasNonEmptyEnv("OPENCODE_CAPTURE_MODEL_CONTEXT"), + captureContent: pickBoolean(resolvedOptions.captureContent) + ?? ["true", "1"].includes((process.env["OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT"] ?? "").toLowerCase()), combineReasoningTokens: pickBoolean(resolvedOptions.combineReasoningTokens) ?? hasNonEmptyEnv("OPENCODE_COMBINE_REASONING_TOKENS"), logLevel: pickString(resolvedOptions.logLevel), endpoint: pickString(resolvedOptions.endpoint) ?? process.env["OPENCODE_OTLP_ENDPOINT"] ?? "http://localhost:4317", diff --git a/src/handlers/session.ts b/src/handlers/session.ts index 3d9c65a..8301b87 100644 --- a/src/handlers/session.ts +++ b/src/handlers/session.ts @@ -203,7 +203,7 @@ export function handleExecutionStarted(e: EventOf<"session.execution.started">, [AGENT_NAME]: totals?.agent ?? "unknown", "agent.type": totals?.agentType ?? "primary", "session.is_subagent": isSubagent, - ...(promptText + ...(promptText && ctx.captureContent ? { [INPUT_VALUE]: promptText, [INPUT_MIME_TYPE]: MimeType.TEXT, diff --git a/src/handlers/step.ts b/src/handlers/step.ts index 3bf155c..e61e9eb 100644 --- a/src/handlers/step.ts +++ b/src/handlers/step.ts @@ -100,7 +100,7 @@ export function handleStepStarted(e: EventOf<"session.step.started">, ctx: Handl [LLM_PROVIDER]: d.model.providerID, "gen_ai.provider.name": genAiProviderName(d.model.providerID), [LLM_MODEL_NAME]: d.model.id, - ...(promptText + ...(promptText && ctx.captureContent ? { [INPUT_VALUE]: promptText, [INPUT_MIME_TYPE]: MimeType.TEXT, @@ -195,7 +195,7 @@ export function handleStepEnded(e: EventOf<"session.step.ended">, ctx: HandlerCo [LLM_COST_TOTAL]: d.cost, cost_usd: d.cost, duration_ms: durationMs, - ...(output ? { + ...(output && ctx.captureContent ? { [OUTPUT_VALUE]: output, [OUTPUT_MIME_TYPE]: MimeType.TEXT, [LLM_OUTPUT_MESSAGES]: JSON.stringify([{ role: "assistant", content: output }]), @@ -264,7 +264,7 @@ export function handleStepFailed(e: EventOf<"session.step.failed">, ctx: Handler [LLM_TOKEN_COUNT_TOTAL]: allTokens(d.tokens), } : {}), ...(d.cost !== undefined ? { [LLM_COST_TOTAL]: d.cost, cost_usd: d.cost } : {}), - ...(output ? { [OUTPUT_VALUE]: output, [OUTPUT_MIME_TYPE]: MimeType.TEXT } : {}), + ...(output && ctx.captureContent ? { [OUTPUT_VALUE]: output, [OUTPUT_MIME_TYPE]: MimeType.TEXT } : {}), }) span.setStatus({ code: SpanStatusCode.ERROR, message: error }) span.end(e.created) diff --git a/src/handlers/tool.ts b/src/handlers/tool.ts index 07fe8f6..6b94d1b 100644 --- a/src/handlers/tool.ts +++ b/src/handlers/tool.ts @@ -81,9 +81,7 @@ export function handleToolCalled(e: EventOf<"session.tool.called">, ctx: Handler const { agentName, agentType } = getSessionAgentMeta(d.sessionID, ctx) const inputJson = safeJson(d.input) ctx.tracing.toolSpans.get(d.id)?.setAttributes({ - [TOOL_PARAMETERS]: inputJson, - [INPUT_VALUE]: inputJson, - [INPUT_MIME_TYPE]: MimeType.JSON, + ...(ctx.captureContent ? { [TOOL_PARAMETERS]: inputJson, [INPUT_VALUE]: inputJson, [INPUT_MIME_TYPE]: MimeType.JSON } : {}), [AGENT_NAME]: agentName, "agent.type": agentType, "tool.provider_executed": isProviderExecuted(d), @@ -205,7 +203,7 @@ function finishTool( const span = ctx.tracing.toolSpans.get(callID) if (span) { span.setAttributes({ [AGENT_NAME]: agentName, "agent.type": agentType, "tool.success": success }) - if (output) { + if (output && ctx.captureContent) { span.setAttributes({ [OUTPUT_VALUE]: output, [OUTPUT_MIME_TYPE]: MimeType.TEXT }) } if (success) { diff --git a/src/plugin.ts b/src/plugin.ts index 2ecb0e9..3b493bb 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -104,6 +104,7 @@ export async function setup(ctx: OpenCodeContext): Promise<() => Promise> disabledMetrics: config.disabledMetrics, disabledTraces: config.disabledTraces, combineReasoningTokens: config.combineReasoningTokens, + captureContent: config.captureContent, tracer: shared.tracer, tracePrefix: config.metricPrefix, rootContext: remoteContext ? () => remoteContext : () => ROOT_CONTEXT, diff --git a/src/types.ts b/src/types.ts index 5c4ec11..597d152 100644 --- a/src/types.ts +++ b/src/types.ts @@ -183,6 +183,7 @@ export type HandlerContext = { disabledMetrics: Set disabledTraces: Set combineReasoningTokens: boolean + captureContent: boolean tracer: Tracer tracePrefix: string rootContext: () => Context diff --git a/tests/handlers/session.test.ts b/tests/handlers/session.test.ts index e49e046..85e7e3a 100644 --- a/tests/handlers/session.test.ts +++ b/tests/handlers/session.test.ts @@ -62,16 +62,21 @@ describe("handlePromptEnqueued", () => { describe("handleExecutionStarted", () => { test("starts a run span and attaches the pending prompt", () => { - const { ctx, tracer } = makeCtx() - handleSessionCreated(evt("session.created", { sessionID: "ses_1" }), ctx) - ctx.tracing.pendingPrompts.set("ses_1", [{ text: "hello", startMs: 1 }]) - handleExecutionStarted(evt("session.execution.started", { sessionID: "ses_1" }, 2000), ctx) - const span = tracer.spans[0]! - expect(span.name).toBe("opencode.session") - expect(span.startTime).toBe(2000) - expect(span.attributes["input.value"]).toBe("hello") - expect(ctx.tracing.runSpans.has("ses_1")).toBe(true) - expect(ctx.tracing.activePrompts.get("ses_1")!.text).toBe("hello") + process.env.OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT = "true" + try { + const { ctx, tracer } = makeCtx() + handleSessionCreated(evt("session.created", { sessionID: "ses_1" }), ctx) + ctx.tracing.pendingPrompts.set("ses_1", [{ text: "hello", startMs: 1 }]) + handleExecutionStarted(evt("session.execution.started", { sessionID: "ses_1" }, 2000), ctx) + const span = tracer.spans[0]! + expect(span.name).toBe("opencode.session") + expect(span.startTime).toBe(2000) + expect(span.attributes["input.value"]).toBe("hello") + expect(ctx.tracing.runSpans.has("ses_1")).toBe(true) + expect(ctx.tracing.activePrompts.get("ses_1")!.text).toBe("hello") + } finally { + delete process.env.OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT + } }) test("nests subagent runs under the parent run span", () => { @@ -133,40 +138,50 @@ describe("handleExecutionStarted", () => { }) test("retains a prompt queued during the current execution for the next run", () => { - const { ctx, tracer } = makeCtx() - handleSessionCreated(evt("session.created", { sessionID: "ses_1", projectID: "p" }), ctx) - const prompt = (text: string) => evt("session.inbox.enqueued", { - sessionID: "ses_1", inboxID: text, - item: { type: "user", payload: { text }, delivery: "queue" }, - }) - handlePromptEnqueued(prompt("first"), ctx, false) - handleExecutionStarted(evt("session.execution.started", { sessionID: "ses_1" }), ctx) - handlePromptEnqueued(prompt("second"), ctx, false) - handleExecutionEnded(evt("session.execution.succeeded", { sessionID: "ses_1" }), ctx, { type: "succeeded" }) - finalizeSession("ses_1", ctx) - expect(ctx.tracing.pendingPrompts.get("ses_1")?.[0]?.text).toBe("second") - handleExecutionStarted(evt("session.execution.started", { sessionID: "ses_1" }), ctx) - expect(tracer.spans.at(-1)?.attributes["input.value"]).toBe("second") + process.env.OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT = "true" + try { + const { ctx, tracer } = makeCtx() + handleSessionCreated(evt("session.created", { sessionID: "ses_1", projectID: "p" }), ctx) + const prompt = (text: string) => evt("session.inbox.enqueued", { + sessionID: "ses_1", inboxID: text, + item: { type: "user", payload: { text }, delivery: "queue" }, + }) + handlePromptEnqueued(prompt("first"), ctx, false) + handleExecutionStarted(evt("session.execution.started", { sessionID: "ses_1" }), ctx) + handlePromptEnqueued(prompt("second"), ctx, false) + handleExecutionEnded(evt("session.execution.succeeded", { sessionID: "ses_1" }), ctx, { type: "succeeded" }) + finalizeSession("ses_1", ctx) + expect(ctx.tracing.pendingPrompts.get("ses_1")?.[0]?.text).toBe("second") + handleExecutionStarted(evt("session.execution.started", { sessionID: "ses_1" }), ctx) + expect(tracer.spans.at(-1)?.attributes["input.value"]).toBe("second") + } finally { + delete process.env.OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT + } }) test("applies a steering prompt to the current run, not the next run", () => { - const { ctx, tracer } = makeCtx() - handleSessionCreated(evt("session.created", { sessionID: "ses_1", projectID: "p" }), ctx) - handlePromptEnqueued(evt("session.inbox.enqueued", { - sessionID: "ses_1", inboxID: "first", item: { type: "user", payload: { text: "first" }, delivery: "steer" }, - }), ctx, false) - handleExecutionStarted(evt("session.execution.started", { sessionID: "ses_1" }), ctx) - handlePromptEnqueued(evt("session.inbox.enqueued", { - sessionID: "ses_1", inboxID: "steering", item: { type: "user", payload: { text: "steering" }, delivery: "steer" }, - }), ctx, false) - handlePromptEnqueued(evt("session.inbox.enqueued", { - sessionID: "ses_1", inboxID: "second", item: { type: "user", payload: { text: "second" }, delivery: "queue" }, - }), ctx, false) - expect(ctx.tracing.activePrompts.get("ses_1")?.text).toBe("first\nsteering") - handleExecutionEnded(evt("session.execution.succeeded", { sessionID: "ses_1" }), ctx, { type: "succeeded" }) - finalizeSession("ses_1", ctx) - handleExecutionStarted(evt("session.execution.started", { sessionID: "ses_1" }), ctx) - expect(tracer.spans.at(-1)?.attributes["input.value"]).toBe("second") + process.env.OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT = "true" + try { + const { ctx, tracer } = makeCtx() + handleSessionCreated(evt("session.created", { sessionID: "ses_1", projectID: "p" }), ctx) + handlePromptEnqueued(evt("session.inbox.enqueued", { + sessionID: "ses_1", inboxID: "first", item: { type: "user", payload: { text: "first" }, delivery: "steer" }, + }), ctx, false) + handleExecutionStarted(evt("session.execution.started", { sessionID: "ses_1" }), ctx) + handlePromptEnqueued(evt("session.inbox.enqueued", { + sessionID: "ses_1", inboxID: "steering", item: { type: "user", payload: { text: "steering" }, delivery: "steer" }, + }), ctx, false) + handlePromptEnqueued(evt("session.inbox.enqueued", { + sessionID: "ses_1", inboxID: "second", item: { type: "user", payload: { text: "second" }, delivery: "queue" }, + }), ctx, false) + expect(ctx.tracing.activePrompts.get("ses_1")?.text).toBe("first\nsteering") + handleExecutionEnded(evt("session.execution.succeeded", { sessionID: "ses_1" }), ctx, { type: "succeeded" }) + finalizeSession("ses_1", ctx) + handleExecutionStarted(evt("session.execution.started", { sessionID: "ses_1" }), ctx) + expect(tracer.spans.at(-1)?.attributes["input.value"]).toBe("second") + } finally { + delete process.env.OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT + } }) }) diff --git a/tests/handlers/step.test.ts b/tests/handlers/step.test.ts index 14a367b..c868406 100644 --- a/tests/handlers/step.test.ts +++ b/tests/handlers/step.test.ts @@ -35,28 +35,50 @@ describe("handleStepStarted", () => { describe("handleStepEnded", () => { test("records token, cost, cache, message, and model metrics and ends the span", () => { - const { ctx, tracer, counters, logger } = makeCtx() + process.env.OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT = "true" + try { + const { ctx, tracer, counters, logger } = makeCtx() + seedSession(ctx) + handleStepStarted(stepStarted(), ctx) + handleTextEnded(evt("session.text.ended", { sessionID: "ses_1", assistantMessageID: "msg_1", ordinal: 1, text: "world" }), ctx) + handleTextEnded(evt("session.text.ended", { sessionID: "ses_1", assistantMessageID: "msg_1", ordinal: 0, text: "hello" }), ctx) + handleStepEnded( + evt("session.step.ended", { sessionID: "ses_1", assistantMessageID: "msg_1", finish: "stop", cost: 0.25, tokens: tokens(10, 5, 2, 3, 0) }, 2000), + ctx, + ) + const tokenTypes = counters.token.calls.map((c) => c.attrs["type"]) + expect(tokenTypes).toEqual(["input", "output", "reasoning", "cacheRead", "cacheCreation"]) + expect(counters.cost.calls[0]!.value).toBe(0.25) + expect(counters.cache.calls.map((c) => c.attrs["type"])).toEqual(["cacheRead"]) + expect(counters.message.calls).toHaveLength(1) + expect(counters.modelUsage.calls[0]!.attrs["provider"]).toBe("anthropic") + expect(logger.records.at(-1)!.body).toBe("api_request") + const llm = tracer.spans.find((s) => s.name === "opencode.llm")! + expect(llm.ended).toBe(true) + expect(llm.status.code).toBe(SpanStatusCode.OK) + expect(llm.attributes["llm.token_count.total"]).toBe(20) + expect(llm.attributes["output.value"]).toBe("hello\nworld") + expect(llm.attributes["llm.output_messages"]).toBe('[{"role":"assistant","content":"hello\\nworld"}]') + } finally { + delete process.env.OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT + } + }) + + test("omits prompt and output content unless OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT is enabled", () => { + const { ctx, tracer } = makeCtx() seedSession(ctx) + ctx.tracing.activePrompts.set("ses_1", { text: "secret prompt", startMs: 1 }) handleStepStarted(stepStarted(), ctx) - handleTextEnded(evt("session.text.ended", { sessionID: "ses_1", assistantMessageID: "msg_1", ordinal: 1, text: "world" }), ctx) - handleTextEnded(evt("session.text.ended", { sessionID: "ses_1", assistantMessageID: "msg_1", ordinal: 0, text: "hello" }), ctx) + handleTextEnded(evt("session.text.ended", { sessionID: "ses_1", assistantMessageID: "msg_1", ordinal: 0, text: "secret answer" }), ctx) handleStepEnded( - evt("session.step.ended", { sessionID: "ses_1", assistantMessageID: "msg_1", finish: "stop", cost: 0.25, tokens: tokens(10, 5, 2, 3, 0) }, 2000), + evt("session.step.ended", { sessionID: "ses_1", assistantMessageID: "msg_1", finish: "stop", cost: 0, tokens: tokens(1, 1, 0, 0, 0) }, 2000), ctx, ) - const tokenTypes = counters.token.calls.map((c) => c.attrs["type"]) - expect(tokenTypes).toEqual(["input", "output", "reasoning", "cacheRead", "cacheCreation"]) - expect(counters.cost.calls[0]!.value).toBe(0.25) - expect(counters.cache.calls.map((c) => c.attrs["type"])).toEqual(["cacheRead"]) - expect(counters.message.calls).toHaveLength(1) - expect(counters.modelUsage.calls[0]!.attrs["provider"]).toBe("anthropic") - expect(logger.records.at(-1)!.body).toBe("api_request") const llm = tracer.spans.find((s) => s.name === "opencode.llm")! - expect(llm.ended).toBe(true) - expect(llm.status.code).toBe(SpanStatusCode.OK) - expect(llm.attributes["llm.token_count.total"]).toBe(20) - expect(llm.attributes["output.value"]).toBe("hello\nworld") - expect(llm.attributes["llm.output_messages"]).toBe('[{"role":"assistant","content":"hello\\nworld"}]') + expect(llm.attributes["input.value"]).toBeUndefined() + expect(llm.attributes["llm.input_messages"]).toBeUndefined() + expect(llm.attributes["output.value"]).toBeUndefined() + expect(llm.attributes["llm.output_messages"]).toBeUndefined() }) test("reports reasoning separately from output by default", () => { diff --git a/tests/handlers/tool.test.ts b/tests/handlers/tool.test.ts index b7c10b3..765317b 100644 --- a/tests/handlers/tool.test.ts +++ b/tests/handlers/tool.test.ts @@ -42,17 +42,22 @@ describe("handleToolInputStarted", () => { describe("handleToolCalled", () => { test("attaches parameters and detects a local git commit", () => { - const { ctx, counters, logger, tracer } = makeCtx() - seed(ctx) - handleToolInputStarted(inputStarted("call_1", "shell"), ctx) - handleToolCalled(called("call_1", { command: "git commit -m 'x'" }, local), ctx) - expect(counters.commit.calls).toHaveLength(0) - handleToolSuccess(succeeded("call_1", "commit made", local), ctx) - expect(counters.commit.calls).toHaveLength(1) - expect(logger.records.some((record) => record.body === "commit")).toBe(true) - const span = tracer.spans.find((s) => s.name === "opencode.tool.shell")! - expect(span.attributes["input.value"]).toContain("git commit") - expect(span.attributes["tool.provider_executed"]).toBe(false) + process.env.OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT = "true" + try { + const { ctx, counters, logger, tracer } = makeCtx() + seed(ctx) + handleToolInputStarted(inputStarted("call_1", "shell"), ctx) + handleToolCalled(called("call_1", { command: "git commit -m 'x'" }, local), ctx) + expect(counters.commit.calls).toHaveLength(0) + handleToolSuccess(succeeded("call_1", "commit made", local), ctx) + expect(counters.commit.calls).toHaveLength(1) + expect(logger.records.some((record) => record.body === "commit")).toBe(true) + const span = tracer.spans.find((s) => s.name === "opencode.tool.shell")! + expect(span.attributes["input.value"]).toContain("git commit") + expect(span.attributes["tool.provider_executed"]).toBe(false) + } finally { + delete process.env.OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT + } }) test("ignores non-commit shell commands", () => { @@ -76,22 +81,27 @@ describe("handleToolCalled", () => { describe("handleToolSuccess", () => { test("records a locally executed tool", () => { - const { ctx, histograms, logger, tracer } = makeCtx() - seed(ctx) - handleToolInputStarted(inputStarted("call_1", "read", "ses_1", 1000), ctx) - handleToolCalled(called("call_1", {}, local, 1000), ctx) - handleToolSuccess(succeeded("call_1", "file body", local, 1250), ctx) - expect(histograms.tool.calls).toHaveLength(1) - expect(histograms.tool.calls[0]!.value).toBe(250) - expect(histograms.tool.calls[0]!.attrs["success"]).toBe(true) - const record = logger.records.at(-1)! - expect(record.body).toBe("tool_result") - expect(record.attributes?.["duration_ms"]).toBe(250) - const span = tracer.spans.find((s) => s.name === "opencode.tool.read")! - expect(span.startTime).toBe(1000) - expect(span.endTime).toBe(1250) - expect(span.status.code).toBe(SpanStatusCode.OK) - expect(span.attributes["output.value"]).toBe("file body") + process.env.OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT = "true" + try { + const { ctx, histograms, logger, tracer } = makeCtx() + seed(ctx) + handleToolInputStarted(inputStarted("call_1", "read", "ses_1", 1000), ctx) + handleToolCalled(called("call_1", {}, local, 1000), ctx) + handleToolSuccess(succeeded("call_1", "file body", local, 1250), ctx) + expect(histograms.tool.calls).toHaveLength(1) + expect(histograms.tool.calls[0]!.value).toBe(250) + expect(histograms.tool.calls[0]!.attrs["success"]).toBe(true) + const record = logger.records.at(-1)! + expect(record.body).toBe("tool_result") + expect(record.attributes?.["duration_ms"]).toBe(250) + const span = tracer.spans.find((s) => s.name === "opencode.tool.read")! + expect(span.startTime).toBe(1000) + expect(span.endTime).toBe(1250) + expect(span.status.code).toBe(SpanStatusCode.OK) + expect(span.attributes["output.value"]).toBe("file body") + } finally { + delete process.env.OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT + } }) test("records a provider-executed tool", () => { diff --git a/tests/helpers.ts b/tests/helpers.ts index 2af8128..41238a7 100644 --- a/tests/helpers.ts +++ b/tests/helpers.ts @@ -246,6 +246,7 @@ export function makeCtx( disabledMetrics: new Set(disabledMetrics), disabledTraces: new Set(disabledTraces), combineReasoningTokens, + captureContent: ["true", "1"].includes((process.env["OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT"] ?? "").toLowerCase()), tracer: tracer as unknown as Tracer, tracePrefix: "opencode.", rootContext: () => ROOT_CONTEXT, diff --git a/tests/integration/opencode.test.ts b/tests/integration/opencode.test.ts index 31ac8c3..f03e1c4 100644 --- a/tests/integration/opencode.test.ts +++ b/tests/integration/opencode.test.ts @@ -2,6 +2,9 @@ import { beforeAll, describe, expect, test } from "bun:test" import { runScenario } from "./fixtures.ts" import { attributes, metricPoints, objects, signalRecords } from "./otlp.ts" +// These scenarios assert captured span content, which is opt-in. +process.env.OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT = "true" + test("exports real OpenCode session telemetry over OTLP/JSON", async () => { const { received, requests, result } = await runScenario("text") expect(requests).toHaveLength(1) From 53bb29231bdffd6166b1333260c4e5b37c68df37 Mon Sep 17 00:00:00 2001 From: enternal111 <48542794+enternal111@users.noreply.github.com> Date: Sat, 14 Nov 2026 19:50:00 +0800 Subject: [PATCH 02/10] test: opt integration scenarios into content capture --- tests/integration/fixtures.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/integration/fixtures.ts b/tests/integration/fixtures.ts index e8341e9..7d7eb12 100644 --- a/tests/integration/fixtures.ts +++ b/tests/integration/fixtures.ts @@ -85,6 +85,8 @@ export async function runScenario(scenario: "text" | "read", disabledTraces = "" OPENCODE_OTLP_ENDPOINT: String(receiver.url), OPENCODE_OTLP_PROTOCOL: "http/json", OPENCODE_DISABLE_TRACES: disabledTraces, + // The scenarios assert captured span content, which is opt-in. + OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT: "true", }) const git = Bun.spawnSync(["git", "init", "--quiet", directory], { env }) if (git.exitCode !== 0) throw new Error(git.stderr.toString()) From 5c2d2608e47d65b63fd70424cb7dc6dab4c7ae26 Mon Sep 17 00:00:00 2001 From: enternal111 <48542794+enternal111@users.noreply.github.com> Date: Sun, 15 Nov 2026 14:54:37 +0800 Subject: [PATCH 03/10] fix: rename capture env to OPENCODE_CAPTURE_CONTENT and document the option Follow the repository convention that plugin settings use the OPENCODE_ namespace. Document captureContent in the README environment variable and plugin option tables, and update the privacy note to cover span content. --- README.md | 6 ++++-- src/config.ts | 3 +-- tests/handlers/session.test.ts | 14 +++++++------- tests/handlers/step.test.ts | 8 ++++---- tests/handlers/tool.test.ts | 10 +++++----- tests/helpers.ts | 2 +- tests/integration/fixtures.ts | 3 +-- 7 files changed, 23 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index a6f54cf..b9dfdab 100644 --- a/README.md +++ b/README.md @@ -143,7 +143,7 @@ The environment variables (set them in your shell profile — `~/.zshrc`, `~/.ba | `OPENCODE_METRIC_PREFIX` | `opencode.` | Prefix for all metric names (e.g. set to `claude_code.` for Claude Code dashboard compatibility) | | `OPENCODE_DISABLE_METRICS` | *(unset)* | Comma-separated list of metric name suffixes to disable (e.g. `cache.count,session.duration`) | | `OPENCODE_DISABLE_LOGS` | *(unset)* | Set to any non-empty value to suppress all OTLP log events while leaving metrics and traces unchanged | -| `OPENCODE_CAPTURE_PROMPT_IN_LOGS` | *(unset)* | Set to any non-empty value to include the full prompt text in the `prompt` attribute of `user_prompt` log events. **Log events only** — trace spans carry an observed prompt in `input.value` regardless of this flag (disable span-level capture separately via `OPENCODE_DISABLE_TRACES`). **Off by default — prompts may contain secrets or PII; enable only for trusted collectors.** | +| `OPENCODE_CAPTURE_PROMPT_IN_LOGS` | *(unset)* | Set to any non-empty value to include the full prompt text in the `prompt` attribute of `user_prompt` log events. **Log events only** — span-level content capture is controlled separately by `OPENCODE_CAPTURE_CONTENT`. **Off by default — prompts may contain secrets or PII; enable only for trusted collectors.** | | `OPENCODE_CAPTURE_MODEL_CONTEXT` | *(unset)* | Set to any non-empty value to attach a bounded text-only preview of the primary model-visible context to LLM spans. Off by default; can include system instructions, earlier messages, and tool text. | | `OPENCODE_COMBINE_REASONING_TOKENS` | *(unset)* | Set to any non-empty value to report reasoning tokens as part of `output` instead of a separate `reasoning` type. OpenCode V2 excludes reasoning from `output`, while Claude Code includes it, so enable this for Claude Code dashboard and cost parity. Applies to the `token.usage` metric, `api_request` `output_tokens` (with `reasoning_tokens` omitted), and the LLM span `llm.token_count.completion`; `llm.token_count.completion_details.reasoning` still carries the reasoning subset. | | `OPENCODE_DISABLE_TRACES` | *(unset)* | Comma-separated list of trace types to disable (`session`, `llm`, `tool`). Use `all`, `*`, `true`, or `1` to disable every trace type | @@ -155,8 +155,9 @@ The environment variables (set them in your shell profile — `~/.zshrc`, `~/.ba | `OPENCODE_TRACEPARENT` | *(unset)* | W3C [`traceparent`](https://www.w3.org/TR/trace-context/#traceparent-header) string. When set, all spans are parented under this remote context so opencode traces nest inside a caller's trace (e.g. a CI job). Invalid values are logged and ignored. Note: with the default `ParentBased` sampler, a value with the sampled flag off (`...-00`) suppresses all trace export. | | `OPENCODE_TRACESTATE` | *(unset)* | W3C [`tracestate`](https://www.w3.org/TR/trace-context/#tracestate-header) string, parsed alongside `OPENCODE_TRACEPARENT` and attached to the remote parent context. Ignored unless a valid `OPENCODE_TRACEPARENT` is also set. | | `OPENCODE_TRACE_PROPAGATION_PROVIDERS` | *(unset)* | Comma-separated opencode provider IDs that receive W3C `traceparent` and `tracestate` headers on LLM requests. Use `*` to explicitly enable every provider. | +| `OPENCODE_CAPTURE_CONTENT` | *(unset)* | Set to any non-empty value to include user prompts, model outputs, and tool parameters/inputs/outputs in span attributes (`input.value`, `output.value`, `llm.input_messages`, `llm.output_messages`, `tool.parameters`). **Off by default — span attributes omit all conversation content; enable only for trusted collectors.** | -Prompt logging remains disabled by default. Enable it only when the configured telemetry destination is trusted to receive potentially sensitive prompt contents. +Prompt logging and span content capture remain disabled by default. Enable them only when the configured telemetry destination is trusted to receive potentially sensitive conversation contents. ### Plugin options (opencode.json) @@ -188,6 +189,7 @@ Option keys mirror the resolved config and map to the environment variables: | `enabled` | `OPENCODE_ENABLE_TELEMETRY` | | `logsEnabled` | `OPENCODE_DISABLE_LOGS` (inverted) | | `capturePromptInLogs` | `OPENCODE_CAPTURE_PROMPT_IN_LOGS` | +| `captureContent` | `OPENCODE_CAPTURE_CONTENT` | | `captureModelContext` | `OPENCODE_CAPTURE_MODEL_CONTEXT` | | `combineReasoningTokens` | `OPENCODE_COMBINE_REASONING_TOKENS` | | `logLevel` | *(none — option only)*: `debug`, `info`, `warn`, `error` | diff --git a/src/config.ts b/src/config.ts index ca7e675..fe45452 100644 --- a/src/config.ts +++ b/src/config.ts @@ -203,8 +203,7 @@ export function loadConfig(options: OtelPluginOptions = {}): PluginConfig { logsEnabled: pickBoolean(resolvedOptions.logsEnabled) ?? !hasNonEmptyEnv("OPENCODE_DISABLE_LOGS"), capturePromptInLogs: pickBoolean(resolvedOptions.capturePromptInLogs) ?? hasNonEmptyEnv("OPENCODE_CAPTURE_PROMPT_IN_LOGS"), captureModelContext: pickBoolean(resolvedOptions.captureModelContext) ?? hasNonEmptyEnv("OPENCODE_CAPTURE_MODEL_CONTEXT"), - captureContent: pickBoolean(resolvedOptions.captureContent) - ?? ["true", "1"].includes((process.env["OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT"] ?? "").toLowerCase()), + captureContent: pickBoolean(resolvedOptions.captureContent) ?? hasNonEmptyEnv("OPENCODE_CAPTURE_CONTENT"), combineReasoningTokens: pickBoolean(resolvedOptions.combineReasoningTokens) ?? hasNonEmptyEnv("OPENCODE_COMBINE_REASONING_TOKENS"), logLevel: pickString(resolvedOptions.logLevel), endpoint: pickString(resolvedOptions.endpoint) ?? process.env["OPENCODE_OTLP_ENDPOINT"] ?? "http://localhost:4317", diff --git a/tests/handlers/session.test.ts b/tests/handlers/session.test.ts index 85e7e3a..10a8fdd 100644 --- a/tests/handlers/session.test.ts +++ b/tests/handlers/session.test.ts @@ -1,4 +1,4 @@ -import { describe, test, expect } from "bun:test" +import { describe, test, expect } from "bun:test" import { SpanStatusCode } from "@opentelemetry/api" import { finalizeSession, @@ -62,7 +62,7 @@ describe("handlePromptEnqueued", () => { describe("handleExecutionStarted", () => { test("starts a run span and attaches the pending prompt", () => { - process.env.OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT = "true" + process.env.OPENCODE_CAPTURE_CONTENT = "true" try { const { ctx, tracer } = makeCtx() handleSessionCreated(evt("session.created", { sessionID: "ses_1" }), ctx) @@ -75,7 +75,7 @@ describe("handleExecutionStarted", () => { expect(ctx.tracing.runSpans.has("ses_1")).toBe(true) expect(ctx.tracing.activePrompts.get("ses_1")!.text).toBe("hello") } finally { - delete process.env.OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT + delete process.env.OPENCODE_CAPTURE_CONTENT } }) @@ -138,7 +138,7 @@ describe("handleExecutionStarted", () => { }) test("retains a prompt queued during the current execution for the next run", () => { - process.env.OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT = "true" + process.env.OPENCODE_CAPTURE_CONTENT = "true" try { const { ctx, tracer } = makeCtx() handleSessionCreated(evt("session.created", { sessionID: "ses_1", projectID: "p" }), ctx) @@ -155,12 +155,12 @@ describe("handleExecutionStarted", () => { handleExecutionStarted(evt("session.execution.started", { sessionID: "ses_1" }), ctx) expect(tracer.spans.at(-1)?.attributes["input.value"]).toBe("second") } finally { - delete process.env.OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT + delete process.env.OPENCODE_CAPTURE_CONTENT } }) test("applies a steering prompt to the current run, not the next run", () => { - process.env.OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT = "true" + process.env.OPENCODE_CAPTURE_CONTENT = "true" try { const { ctx, tracer } = makeCtx() handleSessionCreated(evt("session.created", { sessionID: "ses_1", projectID: "p" }), ctx) @@ -180,7 +180,7 @@ describe("handleExecutionStarted", () => { handleExecutionStarted(evt("session.execution.started", { sessionID: "ses_1" }), ctx) expect(tracer.spans.at(-1)?.attributes["input.value"]).toBe("second") } finally { - delete process.env.OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT + delete process.env.OPENCODE_CAPTURE_CONTENT } }) }) diff --git a/tests/handlers/step.test.ts b/tests/handlers/step.test.ts index c868406..ee43291 100644 --- a/tests/handlers/step.test.ts +++ b/tests/handlers/step.test.ts @@ -1,4 +1,4 @@ -import { describe, test, expect } from "bun:test" +import { describe, test, expect } from "bun:test" import { SpanStatusCode } from "@opentelemetry/api" import { handleStepEnded, handleStepFailed, handleStepStarted, handleTextEnded } from "../../src/handlers/step.ts" import { handleExecutionStarted, handleSessionCreated } from "../../src/handlers/session.ts" @@ -35,7 +35,7 @@ describe("handleStepStarted", () => { describe("handleStepEnded", () => { test("records token, cost, cache, message, and model metrics and ends the span", () => { - process.env.OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT = "true" + process.env.OPENCODE_CAPTURE_CONTENT = "true" try { const { ctx, tracer, counters, logger } = makeCtx() seedSession(ctx) @@ -60,11 +60,11 @@ describe("handleStepEnded", () => { expect(llm.attributes["output.value"]).toBe("hello\nworld") expect(llm.attributes["llm.output_messages"]).toBe('[{"role":"assistant","content":"hello\\nworld"}]') } finally { - delete process.env.OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT + delete process.env.OPENCODE_CAPTURE_CONTENT } }) - test("omits prompt and output content unless OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT is enabled", () => { + test("omits prompt and output content unless OPENCODE_CAPTURE_CONTENT is enabled", () => { const { ctx, tracer } = makeCtx() seedSession(ctx) ctx.tracing.activePrompts.set("ses_1", { text: "secret prompt", startMs: 1 }) diff --git a/tests/handlers/tool.test.ts b/tests/handlers/tool.test.ts index 765317b..058e645 100644 --- a/tests/handlers/tool.test.ts +++ b/tests/handlers/tool.test.ts @@ -1,4 +1,4 @@ -import { describe, test, expect } from "bun:test" +import { describe, test, expect } from "bun:test" import { SpanStatusCode } from "@opentelemetry/api" import { handleToolCalled, handleToolFailed, handleToolInputStarted, handleToolSuccess } from "../../src/handlers/tool.ts" import { handleExecutionStarted, handleSessionCreated } from "../../src/handlers/session.ts" @@ -42,7 +42,7 @@ describe("handleToolInputStarted", () => { describe("handleToolCalled", () => { test("attaches parameters and detects a local git commit", () => { - process.env.OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT = "true" + process.env.OPENCODE_CAPTURE_CONTENT = "true" try { const { ctx, counters, logger, tracer } = makeCtx() seed(ctx) @@ -56,7 +56,7 @@ describe("handleToolCalled", () => { expect(span.attributes["input.value"]).toContain("git commit") expect(span.attributes["tool.provider_executed"]).toBe(false) } finally { - delete process.env.OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT + delete process.env.OPENCODE_CAPTURE_CONTENT } }) @@ -81,7 +81,7 @@ describe("handleToolCalled", () => { describe("handleToolSuccess", () => { test("records a locally executed tool", () => { - process.env.OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT = "true" + process.env.OPENCODE_CAPTURE_CONTENT = "true" try { const { ctx, histograms, logger, tracer } = makeCtx() seed(ctx) @@ -100,7 +100,7 @@ describe("handleToolSuccess", () => { expect(span.status.code).toBe(SpanStatusCode.OK) expect(span.attributes["output.value"]).toBe("file body") } finally { - delete process.env.OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT + delete process.env.OPENCODE_CAPTURE_CONTENT } }) diff --git a/tests/helpers.ts b/tests/helpers.ts index 41238a7..e8f80d2 100644 --- a/tests/helpers.ts +++ b/tests/helpers.ts @@ -246,7 +246,7 @@ export function makeCtx( disabledMetrics: new Set(disabledMetrics), disabledTraces: new Set(disabledTraces), combineReasoningTokens, - captureContent: ["true", "1"].includes((process.env["OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT"] ?? "").toLowerCase()), + captureContent: !!process.env["OPENCODE_CAPTURE_CONTENT"], tracer: tracer as unknown as Tracer, tracePrefix: "opencode.", rootContext: () => ROOT_CONTEXT, diff --git a/tests/integration/fixtures.ts b/tests/integration/fixtures.ts index 7d7eb12..14e49b7 100644 --- a/tests/integration/fixtures.ts +++ b/tests/integration/fixtures.ts @@ -85,8 +85,7 @@ export async function runScenario(scenario: "text" | "read", disabledTraces = "" OPENCODE_OTLP_ENDPOINT: String(receiver.url), OPENCODE_OTLP_PROTOCOL: "http/json", OPENCODE_DISABLE_TRACES: disabledTraces, - // The scenarios assert captured span content, which is opt-in. - OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT: "true", + OPENCODE_CAPTURE_CONTENT: "true", }) const git = Bun.spawnSync(["git", "init", "--quiet", directory], { env }) if (git.exitCode !== 0) throw new Error(git.stderr.toString()) From b116d739710ee41e76a6290385a41ca3c494b99f Mon Sep 17 00:00:00 2001 From: enternal111 <48542794+enternal111@users.noreply.github.com> Date: Sun, 15 Nov 2026 15:08:32 +0800 Subject: [PATCH 04/10] fix: bound tool parameter serialization to 64 KB Cap the JSON.stringify of tool input to 64 KB, matching the bound the step handler already applies to streamed text. --- src/handlers/tool.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/handlers/tool.ts b/src/handlers/tool.ts index 6b94d1b..5b37302 100644 --- a/src/handlers/tool.ts +++ b/src/handlers/tool.ts @@ -247,7 +247,8 @@ function contentText(content: readonly ToolContent[] | undefined): string { function safeJson(value: unknown): string { try { - return JSON.stringify(value) ?? "" + const text = JSON.stringify(value) ?? "" + return text.length <= 64_000 ? text : text.slice(0, 64_000) } catch { return "" } From bf9b4e3960f91b047237de97dede1dcc4ddadaf2 Mon Sep 17 00:00:00 2001 From: enternal111 <48542794+enternal111@users.noreply.github.com> Date: Sun, 15 Nov 2026 15:22:08 +0800 Subject: [PATCH 05/10] fix: keep truncated tool arguments as valid JSON strings Re-encode the truncated text so span attributes always carry valid JSON, and remove the remaining test comment. --- src/handlers/tool.ts | 3 ++- tests/integration/opencode.test.ts | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/handlers/tool.ts b/src/handlers/tool.ts index 5b37302..0169059 100644 --- a/src/handlers/tool.ts +++ b/src/handlers/tool.ts @@ -248,7 +248,8 @@ function contentText(content: readonly ToolContent[] | undefined): string { function safeJson(value: unknown): string { try { const text = JSON.stringify(value) ?? "" - return text.length <= 64_000 ? text : text.slice(0, 64_000) + if (text.length <= 64_000) return text + return JSON.stringify(text.slice(0, 64_000)) } catch { return "" } diff --git a/tests/integration/opencode.test.ts b/tests/integration/opencode.test.ts index f03e1c4..21f9d96 100644 --- a/tests/integration/opencode.test.ts +++ b/tests/integration/opencode.test.ts @@ -2,7 +2,6 @@ import { beforeAll, describe, expect, test } from "bun:test" import { runScenario } from "./fixtures.ts" import { attributes, metricPoints, objects, signalRecords } from "./otlp.ts" -// These scenarios assert captured span content, which is opt-in. process.env.OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT = "true" test("exports real OpenCode session telemetry over OTLP/JSON", async () => { From cf7de4e0a0994e5d8a344efdd5f111d9c2544f04 Mon Sep 17 00:00:00 2001 From: enternal111 <48542794+enternal111@users.noreply.github.com> Date: Mon, 16 Nov 2026 15:46:03 +0800 Subject: [PATCH 06/10] fix: redact error messages and bound captured content when content capture is off --- src/handlers/session.ts | 5 +++-- src/handlers/step.ts | 8 +++++--- src/handlers/tool.ts | 29 ++++++++++++++++------------- src/util.ts | 6 ++++++ 4 files changed, 30 insertions(+), 18 deletions(-) diff --git a/src/handlers/session.ts b/src/handlers/session.ts index 8301b87..73c30dc 100644 --- a/src/handlers/session.ts +++ b/src/handlers/session.ts @@ -18,6 +18,7 @@ import { isTraceEnabled, markSeen, modelRef, + redactErrorMessage, resolveSubagentTraceContext, setBoundedMap, totalTokens, @@ -258,8 +259,8 @@ export function handleExecutionEnded( } if (outcome.type === "failed") { const message = errorSummary(outcome.error) - span.setStatus({ code: SpanStatusCode.ERROR, message }) - span.setAttribute("error", message) + span.setStatus({ code: SpanStatusCode.ERROR, message: ctx.captureContent ? message : redactErrorMessage(message) }) + span.setAttribute("error", ctx.captureContent ? message : redactErrorMessage(message)) } else if (outcome.type === "interrupted") { span.setAttribute("session.interrupted_reason", outcome.reason) span.setStatus({ code: SpanStatusCode.OK }) diff --git a/src/handlers/step.ts b/src/handlers/step.ts index e61e9eb..7c86a23 100644 --- a/src/handlers/step.ts +++ b/src/handlers/step.ts @@ -32,6 +32,7 @@ import { genAiProviderName, isMetricEnabled, isTraceEnabled, + redactErrorMessage, markSeen, resolveRunContext, setBoundedMap, @@ -90,7 +91,8 @@ export function handleStepStarted(e: EventOf<"session.step.started">, ctx: Handl } if (!isTraceEnabled("llm", ctx)) return - const promptText = ctx.tracing.activePrompts.get(d.sessionID)?.text + const rawPrompt = ctx.tracing.activePrompts.get(d.sessionID)?.text + const promptText = rawPrompt !== undefined ? rawPrompt.slice(0, 64_000) : undefined const attributes = { [OPENINFERENCE_SPAN_KIND]: OpenInferenceSpanKind.LLM, [SESSION_ID]: d.sessionID, @@ -266,7 +268,7 @@ export function handleStepFailed(e: EventOf<"session.step.failed">, ctx: Handler ...(d.cost !== undefined ? { [LLM_COST_TOTAL]: d.cost, cost_usd: d.cost } : {}), ...(output && ctx.captureContent ? { [OUTPUT_VALUE]: output, [OUTPUT_MIME_TYPE]: MimeType.TEXT } : {}), }) - span.setStatus({ code: SpanStatusCode.ERROR, message: error }) + span.setStatus({ code: SpanStatusCode.ERROR, message: ctx.captureContent ? error : redactErrorMessage(error) }) span.end(e.created) } cleanupStep(d.assistantMessageID, d.sessionID, ctx) @@ -284,7 +286,7 @@ export function handleStepFailed(e: EventOf<"session.step.failed">, ctx: Handler provider: providerID, "gen_ai.provider.name": genAiProviderName(providerID), ...agentAttrs(agent, agentType), - error, + error: ctx.captureContent ? error : redactErrorMessage(error), duration_ms: durationMs, ...ctx.commonAttrs, }, diff --git a/src/handlers/tool.ts b/src/handlers/tool.ts index 0169059..dc9c957 100644 --- a/src/handlers/tool.ts +++ b/src/handlers/tool.ts @@ -21,6 +21,7 @@ import { getSessionAgentMeta, isMetricEnabled, isTraceEnabled, + redactErrorMessage, resolveStepContext, setBoundedMap, } from "../util.ts" @@ -79,7 +80,7 @@ export function handleToolCalled(e: EventOf<"session.tool.called">, ctx: Handler } const { agentName, agentType } = getSessionAgentMeta(d.sessionID, ctx) - const inputJson = safeJson(d.input) + const inputJson = ctx.captureContent ? safeJson(d.input) : "" ctx.tracing.toolSpans.get(d.id)?.setAttributes({ ...(ctx.captureContent ? { [TOOL_PARAMETERS]: inputJson, [INPUT_VALUE]: inputJson, [INPUT_MIME_TYPE]: MimeType.JSON } : {}), [AGENT_NAME]: agentName, @@ -143,15 +144,15 @@ export function handleToolProgress(e: EventOf<"session.tool.progress">, ctx: Han /** Ends a successful tool call: records duration, sets output attributes, and emits `tool_result`. */ export function handleToolSuccess(e: EventOf<"session.tool.success">, ctx: HandlerContext) { linkSubagent(e.data.id, e.data.metadata?.["sessionID"], ctx) - const output = contentText(e.data.content) - finishTool(e.data.id, e.data.sessionID, e.created, true, output, undefined, ctx) + const { text, fullLength } = contentText(e.data.content) + finishTool(e.data.id, e.data.sessionID, e.created, true, text, undefined, ctx, fullLength) } /** Ends a failed tool call: records duration, sets error attributes, and emits `tool_result`. */ export function handleToolFailed(e: EventOf<"session.tool.failed">, ctx: HandlerContext) { linkSubagent(e.data.id, e.data.metadata?.["sessionID"], ctx) - const output = contentText(e.data.content) - finishTool(e.data.id, e.data.sessionID, e.created, false, output, errorSummary(e.data.error), ctx) + const { text, fullLength } = contentText(e.data.content) + finishTool(e.data.id, e.data.sessionID, e.created, false, text, errorSummary(e.data.error), ctx, fullLength) } function finishTool( @@ -162,6 +163,7 @@ function finishTool( output: string, error: string | undefined, ctx: HandlerContext, + fullOutputLength = 0, ) { const meta = ctx.tracing.toolMeta.get(callID) ctx.tracing.toolMeta.delete(callID) @@ -170,7 +172,7 @@ function finishTool( const start = observedExecution ? meta.startMs : endMs const durationMs = Math.max(0, endMs - start) const { agentName, agentType } = getSessionAgentMeta(sessionID, ctx) - const sizeBytes = output ? Buffer.byteLength(output, "utf8") : 0 + const sizeBytes = fullOutputLength > 0 ? fullOutputLength : (output ? output.length : 0) if (success && observedExecution && meta.command && SHELL_TOOL_RE.test(tool) && GIT_COMMIT_RE.test(meta.command)) { if (isMetricEnabled("commit.count", ctx)) { @@ -210,8 +212,8 @@ function finishTool( span.setAttribute("tool.result_size_bytes", sizeBytes) span.setStatus({ code: SpanStatusCode.OK }) } else { - span.setAttribute("tool.error", error ?? "unknown") - span.setStatus({ code: SpanStatusCode.ERROR, message: error ?? "unknown" }) + span.setAttribute("tool.error", ctx.captureContent ? (error ?? "unknown") : redactErrorMessage(error ?? "unknown")) + span.setStatus({ code: SpanStatusCode.ERROR, message: ctx.captureContent ? error : redactErrorMessage(error ?? "unknown") }) } span.end(endMs) } @@ -237,12 +239,13 @@ function finishTool( }) } -function contentText(content: readonly ToolContent[] | undefined): string { - if (!content) return "" - return content - .map((part) => (part.type === "text" ? part.text : part.name ?? part.uri)) - .filter(Boolean) +function contentText(content: readonly ToolContent[] | undefined): { text: string; fullLength: number } { + if (!content) return { text: "", fullLength: 0 } + const full = content + .filter((part) => part.type === "text") + .map((part) => part.text) .join("\n") + return { text: full.length <= 64_000 ? full : full.slice(0, 64_000), fullLength: Buffer.byteLength(full, "utf8") } } function safeJson(value: unknown): string { diff --git a/src/util.ts b/src/util.ts index 9a45d3b..16eb268 100644 --- a/src/util.ts +++ b/src/util.ts @@ -34,6 +34,12 @@ export function errorSummary(err: StructuredError | undefined): string { return `${err.type}: ${err.message}` } +export function redactErrorMessage(message: string): string { + return message + .replace(/\b(?:for|with|query|search(?:ing)?\s+for)\s+.+$/i, " for [redacted]") + .slice(0, 128) +} + /** Returns the canonical OTel GenAI provider name, preserving unknown provider IDs. */ export function genAiProviderName(providerID: string): string { return GEN_AI_PROVIDER_NAMES[providerID] ?? providerID From 57fffba91bbf9bc41ad419b1eaf98885e78a38e0 Mon Sep 17 00:00:00 2001 From: enternal111 <48542794+enternal111@users.noreply.github.com> Date: Mon, 16 Nov 2026 20:31:02 +0800 Subject: [PATCH 07/10] fix: remove leftover dead env var from the capture-content rename --- tests/integration/opencode.test.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/integration/opencode.test.ts b/tests/integration/opencode.test.ts index 21f9d96..31ac8c3 100644 --- a/tests/integration/opencode.test.ts +++ b/tests/integration/opencode.test.ts @@ -2,8 +2,6 @@ import { beforeAll, describe, expect, test } from "bun:test" import { runScenario } from "./fixtures.ts" import { attributes, metricPoints, objects, signalRecords } from "./otlp.ts" -process.env.OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT = "true" - test("exports real OpenCode session telemetry over OTLP/JSON", async () => { const { received, requests, result } = await runScenario("text") expect(requests).toHaveLength(1) From 83bf0a7323c0509fd29532145369ae69a2148b54 Mon Sep 17 00:00:00 2001 From: enternal111 <48542794+enternal111@users.noreply.github.com> Date: Tue, 17 Nov 2026 15:12:51 +0800 Subject: [PATCH 08/10] fix: fail-closed error redaction in logs and bound the session span prompt --- src/handlers/session.ts | 7 ++++--- src/handlers/tool.ts | 2 +- src/util.ts | 6 +++--- tests/handlers/session.test.ts | 2 +- tests/handlers/step.test.ts | 18 +++++++++++++++++- tests/handlers/tool.test.ts | 4 ++-- 6 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/handlers/session.ts b/src/handlers/session.ts index 73c30dc..c0d2447 100644 --- a/src/handlers/session.ts +++ b/src/handlers/session.ts @@ -192,7 +192,7 @@ export function handleExecutionStarted(e: EventOf<"session.execution.started">, const parentCtx = isSubagent && totals?.parentID ? resolveSubagentTraceContext(sessionID, totals.parentID, totals.agent, ctx) : ctx.rootContext() - const promptText = pendingPrompt?.text ?? "" + const promptText = (pendingPrompt?.text ?? "").slice(0, 64_000) const span = ctx.tracer.startSpan( `${ctx.tracePrefix}session`, @@ -274,6 +274,7 @@ export function handleExecutionEnded( if (outcome.type === "failed") { const message = errorSummary(outcome.error) + const safeMessage = ctx.captureContent ? message : redactErrorMessage(message) ctx.emitLog({ severityNumber: SeverityNumber.ERROR, severityText: "ERROR", @@ -283,12 +284,12 @@ export function handleExecutionEnded( attributes: { "event.name": "session.error", "session.id": sessionID, - error: message, + error: safeMessage, ...agentAttrs(totals?.agent ?? "unknown", totals?.agentType ?? "unknown"), ...ctx.commonAttrs, }, }) - void ctx.log("error", "otel: session.error", { sessionID, error: message }) + void ctx.log("error", "otel: session.error", { sessionID, error: safeMessage }) } } diff --git a/src/handlers/tool.ts b/src/handlers/tool.ts index dc9c957..1d46109 100644 --- a/src/handlers/tool.ts +++ b/src/handlers/tool.ts @@ -233,7 +233,7 @@ function finishTool( ...agentAttrs(agentName, agentType), success, duration_ms: durationMs, - ...(success ? { tool_result_size_bytes: sizeBytes } : { error: error ?? "unknown" }), + ...(success ? { tool_result_size_bytes: sizeBytes } : { error: ctx.captureContent ? error ?? "unknown" : redactErrorMessage(error ?? "unknown") }), ...ctx.commonAttrs, }, }) diff --git a/src/util.ts b/src/util.ts index 16eb268..3e2cf0b 100644 --- a/src/util.ts +++ b/src/util.ts @@ -34,10 +34,10 @@ export function errorSummary(err: StructuredError | undefined): string { return `${err.type}: ${err.message}` } +/** Masks the free-text portion of an error summary, keeping only the structured type prefix. */ export function redactErrorMessage(message: string): string { - return message - .replace(/\b(?:for|with|query|search(?:ing)?\s+for)\s+.+$/i, " for [redacted]") - .slice(0, 128) + const sep = message.indexOf(": ") + return sep >= 0 && sep <= 64 ? `${message.slice(0, sep)}: [redacted]` : "[redacted]" } /** Returns the canonical OTel GenAI provider name, preserving unknown provider IDs. */ diff --git a/tests/handlers/session.test.ts b/tests/handlers/session.test.ts index 10a8fdd..858d8d4 100644 --- a/tests/handlers/session.test.ts +++ b/tests/handlers/session.test.ts @@ -209,7 +209,7 @@ describe("handleExecutionEnded", () => { ) expect(tracer.spans[0]!.status.code).toBe(SpanStatusCode.ERROR) expect(logger.records.at(-1)!.body).toBe("session.error") - expect(logger.records.at(-1)!.attributes?.["error"]).toBe("ProviderError: boom") + expect(logger.records.at(-1)!.attributes?.["error"]).toBe("ProviderError: [redacted]") }) }) diff --git a/tests/handlers/step.test.ts b/tests/handlers/step.test.ts index ee43291..e3756a8 100644 --- a/tests/handlers/step.test.ts +++ b/tests/handlers/step.test.ts @@ -138,11 +138,27 @@ describe("handleStepFailed", () => { expect(llm.attributes["llm.token_count.total"]).toBe(15) expect(llm.attributes["llm.cost.total"]).toBe(0.1) expect(logger.records.at(-1)!.body).toBe("api_error") - expect(logger.records.at(-1)!.attributes?.["error"]).toBe("ProviderError: boom") + expect(logger.records.at(-1)!.attributes?.["error"]).toBe("ProviderError: [redacted]") expect(counters.message.calls).toHaveLength(1) expect(counters.modelUsage.calls).toHaveLength(1) }) + test("keeps the raw error when content capture is enabled", () => { + process.env.OPENCODE_CAPTURE_CONTENT = "true" + try { + const { ctx, logger } = makeCtx() + seedSession(ctx) + handleStepStarted(stepStarted(), ctx) + handleStepFailed( + evt("session.step.failed", { sessionID: "ses_1", assistantMessageID: "msg_1", error: { type: "ProviderError", message: "boom" }, cost: 0.1, tokens: tokens() }), + ctx, + ) + expect(logger.records.at(-1)!.attributes?.["error"]).toBe("ProviderError: boom") + } finally { + delete process.env.OPENCODE_CAPTURE_CONTENT + } + }) + test("folds reasoning into output for failed steps when combineReasoningTokens is enabled", () => { const { ctx, tracer, counters } = makeCtx("proj_test", [], [], true, {}, true) seedSession(ctx) diff --git a/tests/handlers/tool.test.ts b/tests/handlers/tool.test.ts index 058e645..c0c2c1e 100644 --- a/tests/handlers/tool.test.ts +++ b/tests/handlers/tool.test.ts @@ -140,7 +140,7 @@ describe("handleToolFailed", () => { expect(histograms.tool.calls[0]!.value).toBe(200) expect(histograms.tool.calls[0]!.attrs["success"]).toBe(false) expect(logger.records.at(-1)!.attributes?.["duration_ms"]).toBe(200) - expect(logger.records.at(-1)!.attributes?.["error"]).toBe("tool.execution: nope") + expect(logger.records.at(-1)!.attributes?.["error"]).toBe("tool.execution: [redacted]") const span = tracer.spans.find((s) => s.name === "opencode.tool.bash")! expect(span.ended).toBe(true) expect(span.status.code).toBe(SpanStatusCode.ERROR) @@ -165,7 +165,7 @@ describe("handleToolFailed", () => { handleToolFailed(failed("call_1", { type: "permission.rejected", message: "denied" }, local, 1500), ctx) expect(histograms.tool.calls[0]!.value).toBe(500) expect(histograms.tool.calls[0]!.attrs["success"]).toBe(false) - expect(tracer.spans.find((s) => s.name === "opencode.tool.shell")!.attributes["tool.error"]).toBe("permission.rejected: denied") + expect(tracer.spans.find((s) => s.name === "opencode.tool.shell")!.attributes["tool.error"]).toBe("permission.rejected: [redacted]") }) test("does not record malformed input that was never called", () => { From ad8bea6a4e80f4601920e612115c7d38921fbc1d Mon Sep 17 00:00:00 2001 From: enternal111 <48542794+enternal111@users.noreply.github.com> Date: Tue, 17 Nov 2026 15:41:42 +0800 Subject: [PATCH 09/10] fix: clamp oversized tool inputs while preserving JSON shape --- src/handlers/tool.ts | 33 ++++++++++++++++++++++++++++++--- tests/handlers/tool.test.ts | 26 ++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/src/handlers/tool.ts b/src/handlers/tool.ts index 1d46109..2c80c2a 100644 --- a/src/handlers/tool.ts +++ b/src/handlers/tool.ts @@ -248,11 +248,38 @@ function contentText(content: readonly ToolContent[] | undefined): { text: strin return { text: full.length <= 64_000 ? full : full.slice(0, 64_000), fullLength: Buffer.byteLength(full, "utf8") } } +const MAX_JSON_CHARS = 64_000 +const MAX_JSON_DEPTH = 12 +const JSON_LIMITS = { string: 4_096, array: 100, object: 200 } as const +const JSON_LIMITS_TIGHT = { string: 256, array: 20, object: 50 } as const + +type JsonLimits = { string: number; array: number; object: number } + +function clampJson(value: unknown, limits: JsonLimits, depth: number): unknown { + if (typeof value === "string") return value.length > limits.string ? `${value.slice(0, limits.string)}…[truncated]` : value + if (Array.isArray(value)) { + const items = value.slice(0, limits.array).map((item) => clampJson(item, limits, depth + 1)) + if (value.length > limits.array) items.push(`…[truncated ${value.length - limits.array} items]`) + return items + } + if (value !== null && typeof value === "object") { + if (depth >= MAX_JSON_DEPTH) return "…[truncated]" + const entries = Object.entries(value as Record) + const clamped: Record = {} + for (const [key, item] of entries.slice(0, limits.object)) clamped[key] = clampJson(item, limits, depth + 1) + if (entries.length > limits.object) clamped["…truncated_keys"] = entries.length - limits.object + return clamped + } + return value +} + function safeJson(value: unknown): string { try { - const text = JSON.stringify(value) ?? "" - if (text.length <= 64_000) return text - return JSON.stringify(text.slice(0, 64_000)) + let text = JSON.stringify(value) ?? "" + if (text.length > MAX_JSON_CHARS) text = JSON.stringify(clampJson(value, JSON_LIMITS, 0)) ?? "" + if (text.length > MAX_JSON_CHARS) text = JSON.stringify(clampJson(value, JSON_LIMITS_TIGHT, 0)) ?? "" + if (text.length > MAX_JSON_CHARS) text = JSON.stringify({ "…truncated_input": true, preview: text.slice(0, MAX_JSON_CHARS - 200) }) + return text } catch { return "" } diff --git a/tests/handlers/tool.test.ts b/tests/handlers/tool.test.ts index c0c2c1e..dc665bc 100644 --- a/tests/handlers/tool.test.ts +++ b/tests/handlers/tool.test.ts @@ -40,6 +40,32 @@ describe("handleToolInputStarted", () => { }) }) +describe("handleToolCalled oversized inputs", () => { + test("keeps tool.parameters parseable with the original keys when clamped", () => { + process.env.OPENCODE_CAPTURE_CONTENT = "true" + try { + const { ctx, tracer } = makeCtx() + seed(ctx) + handleToolInputStarted(inputStarted("call_1", "bash"), ctx) + const input = { + command: "x".repeat(200_000), + files: Array.from({ length: 500 }, (_, i) => `file-${i}.txt`), + } + handleToolCalled(called("call_1", input, local), ctx) + const span = tracer.spans.find((s) => s.name === "opencode.tool.bash")! + const text = span.attributes["tool.parameters"] as string + expect(text.length).toBeLessThanOrEqual(64_000) + const parsed = JSON.parse(text) as Record + expect(Object.keys(parsed)).toContain("command") + expect(Object.keys(parsed)).toContain("files") + expect(typeof parsed["command"]).toBe("string") + expect(Array.isArray(parsed["files"])).toBe(true) + } finally { + delete process.env.OPENCODE_CAPTURE_CONTENT + } + }) +}) + describe("handleToolCalled", () => { test("attaches parameters and detects a local git commit", () => { process.env.OPENCODE_CAPTURE_CONTENT = "true" From ab3bd453651f8fb1b419dd54049b893c0107e0a9 Mon Sep 17 00:00:00 2001 From: enternal111 <48542794+enternal111@users.noreply.github.com> Date: Tue, 17 Nov 2026 16:05:20 +0800 Subject: [PATCH 10/10] fix: remove unrequested comment --- src/util.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/util.ts b/src/util.ts index 3e2cf0b..d2ca836 100644 --- a/src/util.ts +++ b/src/util.ts @@ -34,7 +34,6 @@ export function errorSummary(err: StructuredError | undefined): string { return `${err.type}: ${err.message}` } -/** Masks the free-text portion of an error summary, keeping only the structured type prefix. */ export function redactErrorMessage(message: string): string { const sep = message.indexOf(": ") return sep >= 0 && sep <= 64 ? `${message.slice(0, sep)}: [redacted]` : "[redacted]"