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/package.json b/package.json index 18fa578..cc57791 100644 --- a/package.json +++ b/package.json @@ -86,5 +86,5 @@ }, "type": "module", "types": "dist/index.d.ts", - "version": "2.1.0" -} + "version": "2.1.0-contentgate.23" +} \ No newline at end of file diff --git a/src/config.ts b/src/config.ts index a0425a9..fe45452 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,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) ?? 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/src/handlers/chat-headers.ts b/src/handlers/chat-headers.ts index f544a5b..8a06b9f 100644 --- a/src/handlers/chat-headers.ts +++ b/src/handlers/chat-headers.ts @@ -3,6 +3,7 @@ import { AGENT_NAME, INPUT_MIME_TYPE, INPUT_VALUE, LLM_INPUT_MESSAGES, LLM_MODEL import type { HandlerContext } from "../types.ts" import { injectTraceContext } from "../trace-context.ts" import { genAiProviderName, isTraceEnabled, resolveRunContext, setBoundedMap } from "../util.ts" +import { ensureRunSpan } from "./session.ts" /** The `session.hook("model.request", ...)` event shape consumed by this handler. */ export type ModelRequestEvent = { @@ -30,6 +31,11 @@ function textParts(parts: readonly unknown[]): string { .slice(0, 1_000) } +/** How long a primary model request waits for queued step events before trusting the recorded active LLM span. */ +const ACTIVE_LLM_SETTLE_MS = 200 +/** Granularity of the active-LLM settle loop. */ +const ACTIVE_LLM_SETTLE_TICK_MS = 15 + /** Stores a bounded text-only preview of the primary model-visible request. */ export function captureModelContext(event: ModelVisibleContextEvent, ctx: HandlerContext): void { const entries = [ @@ -66,13 +72,49 @@ export function applyModelContext( /** Injects the active LLM span's W3C trace context into outbound model requests. */ export async function handleModelRequest(event: ModelRequestEvent, ctx: HandlerContext): Promise { - if (event.kind !== "primary") return const providerID = event.model.providerID const propagate = ctx.tracePropagationProviders.has(providerID) || ctx.tracePropagationProviders.has("*") if (!propagate && !ctx.tracing.modelContexts.has(event.sessionID)) return - if (!isTraceEnabled("llm", ctx)) return await ctx.tracing.eventQueue + if (event.kind !== "primary") { + if (propagate && isTraceEnabled("session", ctx)) { + const dispatch = ctx.tracing.subagentParents.get(event.sessionID) + if (dispatch) { + injectTraceContext(dispatch.spanContext, event.headers) + } else if (ctx.tracing.activeExecutions.has(event.sessionID)) { + ensureRunSpan(event.sessionID, Date.now(), ctx) + const runContext = ctx.tracing.runSpanContexts.get(event.sessionID) + if (runContext) injectTraceContext(runContext, event.headers) + } else { + const parentID = ctx.tracing.sessionIdentity.get(event.sessionID)?.parentID + const parentContext = parentID ? ctx.tracing.runSpanContexts.get(parentID) : undefined + const fallback = parentContext ?? ctx.tracing.runSpanContexts.get(event.sessionID) + if (fallback) injectTraceContext(fallback, event.headers) + } + } + return + } + if (!isTraceEnabled("llm", ctx)) return + ensureRunSpan(event.sessionID, Date.now(), ctx) let active = ctx.tracing.activeLlm.get(event.sessionID) + if (active) { + // The next step's request can fire before session.step.ended of the previous step is + // delivered (opencode publishes session.step.started only after a forked snapshot that + // overlaps the provider request, and the durable bus lags the request loop), so the + // recorded active LLM span may already belong to a finished step. Wait briefly for the + // queued step events to land: if the entry is cleared or replaced we propagate the fresh + // context instead, and if nothing changes we are looking at a genuine in-flight retry. + const deadline = Date.now() + ACTIVE_LLM_SETTLE_MS + while (Date.now() < deadline) { + await new Promise((resolve) => setTimeout(resolve, ACTIVE_LLM_SETTLE_TICK_MS)) + await ctx.tracing.eventQueue + const current = ctx.tracing.activeLlm.get(event.sessionID) + if (!current || current.spanContext.spanId !== active.spanContext.spanId) { + active = current + break + } + } + } if (!active) { const span = ctx.tracer.startSpan( `${ctx.tracePrefix}llm`, diff --git a/src/handlers/session.ts b/src/handlers/session.ts index 3d9c65a..dfd6a4b 100644 --- a/src/handlers/session.ts +++ b/src/handlers/session.ts @@ -18,6 +18,7 @@ import { isTraceEnabled, markSeen, modelRef, + redactErrorMessage, resolveSubagentTraceContext, setBoundedMap, totalTokens, @@ -176,6 +177,33 @@ export function handleAgentSelected(e: EventOf<"session.agent.selected">, ctx: H } /** Starts the root run span for a single execution (user turn), keyed by session id. */ +export function ensureRunSpan(sessionID: string, at: number, ctx: HandlerContext): void { + if (ctx.tracing.runSpans.has(sessionID)) return + if (!isTraceEnabled("session", ctx)) return + const identity = ctx.tracing.sessionIdentity.get(sessionID) + if (identity?.agentType === "subagent") return + const startMs = ctx.tracing.activePrompts.get(sessionID)?.startMs + ?? ctx.tracing.pendingPrompts.get(sessionID)?.[0]?.startMs + ?? at + const span = ctx.tracer.startSpan( + `${ctx.tracePrefix}session`, + { + startTime: startMs, + attributes: { + [OPENINFERENCE_SPAN_KIND]: OpenInferenceSpanKind.AGENT, + [SESSION_ID]: sessionID, + [AGENT_NAME]: identity?.agent ?? "unknown", + "agent.type": identity?.agentType ?? "primary", + "session.is_subagent": false, + ...ctx.commonAttrs, + }, + }, + ctx.rootContext(), + ) + setBoundedMap(ctx.tracing.runSpans, sessionID, span) + setBoundedMap(ctx.tracing.runSpanContexts, sessionID, span.spanContext()) +} + export function handleExecutionStarted(e: EventOf<"session.execution.started">, ctx: HandlerContext) { const sessionID = e.data.sessionID markSeen(ctx.tracing.activeExecutions, sessionID) @@ -188,10 +216,29 @@ export function handleExecutionStarted(e: EventOf<"session.execution.started">, if (!isTraceEnabled("session", ctx)) return const isSubagent = totals?.agentType === "subagent" + const promptText = pendingPrompt?.text ?? "" + const promptAttrs = promptText && ctx.captureContent + ? { + [INPUT_VALUE]: promptText, + [INPUT_MIME_TYPE]: MimeType.TEXT, + [LLM_INPUT_MESSAGES]: JSON.stringify([{ role: "user", content: promptText }]), + } + : {} + + const existing = ctx.tracing.runSpans.get(sessionID) + if (existing) { + existing.setAttributes({ + [AGENT_NAME]: totals?.agent ?? "unknown", + "agent.type": totals?.agentType ?? "primary", + ...promptAttrs, + }) + setBoundedMap(ctx.tracing.runSpanContexts, sessionID, existing.spanContext()) + return + } + const parentCtx = isSubagent && totals?.parentID ? resolveSubagentTraceContext(sessionID, totals.parentID, totals.agent, ctx) : ctx.rootContext() - const promptText = pendingPrompt?.text ?? "" const span = ctx.tracer.startSpan( `${ctx.tracePrefix}session`, @@ -203,13 +250,7 @@ export function handleExecutionStarted(e: EventOf<"session.execution.started">, [AGENT_NAME]: totals?.agent ?? "unknown", "agent.type": totals?.agentType ?? "primary", "session.is_subagent": isSubagent, - ...(promptText - ? { - [INPUT_VALUE]: promptText, - [INPUT_MIME_TYPE]: MimeType.TEXT, - [LLM_INPUT_MESSAGES]: JSON.stringify([{ role: "user", content: promptText }]), - } - : {}), + ...promptAttrs, ...ctx.commonAttrs, }, }, @@ -258,8 +299,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 }) @@ -337,6 +378,12 @@ export function finalizeSession(sessionID: string, ctx: HandlerContext) { const totals = ctx.tracing.sessionTotals.get(sessionID) if (!totals) { sweepExecution(sessionID, ctx) + const danglingRun = ctx.tracing.runSpans.get(sessionID) + if (danglingRun) { + danglingRun.end() + ctx.tracing.runSpans.delete(sessionID) + ctx.tracing.runSpanContexts.delete(sessionID) + } return } const identity = ctx.tracing.sessionIdentity.get(sessionID) @@ -356,6 +403,12 @@ export function finalizeSession(sessionID: string, ctx: HandlerContext) { ctx.tracing.modelContexts.delete(sessionID) ctx.tracing.activePrompts.delete(sessionID) ctx.tracing.activeExecutions.delete(sessionID) + const danglingRun = ctx.tracing.runSpans.get(sessionID) + if (danglingRun) { + danglingRun.end() + ctx.tracing.runSpans.delete(sessionID) + ctx.tracing.runSpanContexts.delete(sessionID) + } const attrs = { ...ctx.commonAttrs, "session.id": sessionID } const durationMs = Date.now() - totals.startMs diff --git a/src/handlers/step.ts b/src/handlers/step.ts index 3bf155c..6cdd3b4 100644 --- a/src/handlers/step.ts +++ b/src/handlers/step.ts @@ -24,7 +24,7 @@ import { SESSION_ID, } from "@arizeai/openinference-semantic-conventions" import type { EventOf, HandlerContext, SessionTotals } from "../types.ts" -import { ensureSession } from "./session.ts" +import { ensureRunSpan, ensureSession } from "./session.ts" import { applyModelContext } from "./chat-headers.ts" import { agentAttrs, @@ -32,6 +32,7 @@ import { genAiProviderName, isMetricEnabled, isTraceEnabled, + redactErrorMessage, markSeen, resolveRunContext, setBoundedMap, @@ -90,7 +91,10 @@ export function handleStepStarted(e: EventOf<"session.step.started">, ctx: Handl } if (!isTraceEnabled("llm", ctx)) return - const promptText = ctx.tracing.activePrompts.get(d.sessionID)?.text + ensureRunSpan(d.sessionID, d.started ?? e.created, ctx) + const rawPrompt = ctx.tracing.activePrompts.get(d.sessionID)?.text + ?? (ctx.tracing.activeExecutions.has(d.sessionID) ? undefined : ctx.tracing.pendingPrompts.get(d.sessionID)?.[0]?.text) + const promptText = rawPrompt !== undefined ? rawPrompt.slice(0, 64_000) : undefined const attributes = { [OPENINFERENCE_SPAN_KIND]: OpenInferenceSpanKind.LLM, [SESSION_ID]: d.sessionID, @@ -100,7 +104,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 +199,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,9 +268,9 @@ 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.setStatus({ code: SpanStatusCode.ERROR, message: ctx.captureContent ? error : redactErrorMessage(error) }) span.end(e.created) } cleanupStep(d.assistantMessageID, d.sessionID, ctx) @@ -284,7 +288,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 07fe8f6..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,11 +80,9 @@ 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({ - [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), @@ -145,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( @@ -164,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) @@ -172,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)) { @@ -205,15 +205,15 @@ 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) { 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) } @@ -239,17 +239,20 @@ 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 { try { - return JSON.stringify(value) ?? "" + const text = JSON.stringify(value) ?? "" + if (text.length <= 64_000) return text + return JSON.stringify(text.slice(0, 64_000)) } catch { return "" } 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/src/util.ts b/src/util.ts index 9a45d3b..6698c41 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 @@ -129,7 +135,12 @@ export function resolveRunContext(sessionID: string, ctx: HandlerContext): Conte const span = ctx.tracing.runSpans.get(sessionID) if (span) return trace.setSpan(base, span) const spanContext = ctx.tracing.runSpanContexts.get(sessionID) - return spanContext ? trace.setSpanContext(base, spanContext) : base + if (spanContext) return trace.setSpanContext(base, spanContext) + const dispatch = ctx.tracing.subagentParents.get(sessionID) + if (dispatch) return trace.setSpanContext(base, dispatch.spanContext) + const parentID = ctx.tracing.sessionIdentity.get(sessionID)?.parentID + if (parentID && parentID !== sessionID) return resolveRunContext(parentID, ctx) + return base } /** Resolves the trace context for a step span, falling back to its run context. */ diff --git a/tests/handlers/session.test.ts b/tests/handlers/session.test.ts index e49e046..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,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.OPENCODE_CAPTURE_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.OPENCODE_CAPTURE_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.OPENCODE_CAPTURE_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.OPENCODE_CAPTURE_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.OPENCODE_CAPTURE_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.OPENCODE_CAPTURE_CONTENT + } }) }) diff --git a/tests/handlers/step.test.ts b/tests/handlers/step.test.ts index 14a367b..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,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.OPENCODE_CAPTURE_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.OPENCODE_CAPTURE_CONTENT + } + }) + + 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 }) 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..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,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.OPENCODE_CAPTURE_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.OPENCODE_CAPTURE_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.OPENCODE_CAPTURE_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.OPENCODE_CAPTURE_CONTENT + } }) test("records a provider-executed tool", () => { diff --git a/tests/helpers.ts b/tests/helpers.ts index 2af8128..e8f80d2 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: !!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 e8341e9..14e49b7 100644 --- a/tests/integration/fixtures.ts +++ b/tests/integration/fixtures.ts @@ -85,6 +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, + OPENCODE_CAPTURE_CONTENT: "true", }) const git = Bun.spawnSync(["git", "init", "--quiet", directory], { env }) if (git.exitCode !== 0) throw new Error(git.stderr.toString()) diff --git a/tests/integration/opencode.test.ts b/tests/integration/opencode.test.ts index 31ac8c3..21f9d96 100644 --- a/tests/integration/opencode.test.ts +++ b/tests/integration/opencode.test.ts @@ -2,6 +2,8 @@ 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)