Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand All @@ -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)

Expand Down Expand Up @@ -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` |
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,5 @@
},
"type": "module",
"types": "dist/index.d.ts",
"version": "2.1.0"
}
"version": "2.1.0-contentgate.23"
}
3 changes: 3 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type PluginConfig = {
logsEnabled: boolean
capturePromptInLogs: boolean
captureModelContext: boolean
captureContent: boolean
combineReasoningTokens: boolean
logLevel: string | undefined
endpoint: string
Expand Down Expand Up @@ -62,6 +63,7 @@ export type OtelPluginOptions = {
logsEnabled?: boolean
capturePromptInLogs?: boolean
captureModelContext?: boolean
captureContent?: boolean
combineReasoningTokens?: boolean
logLevel?: string
endpoint?: string
Expand Down Expand Up @@ -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",
Expand Down
46 changes: 44 additions & 2 deletions src/handlers/chat-headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -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<void> {
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)
Comment thread
enternal111 marked this conversation as resolved.
Resolved
} 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) {
Comment on lines +108 to +112

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Skip the settle delay for an already-current step span

When session.step.started for the same call has already been consumed before model.request, active is already that call's span and there is no queued transition to change its ID. This loop still sleeps in 15 ms increments until the 200 ms deadline, delaying every request in this ordering before it reaches the provider, including retries. Only wait when a previous-step transition is actually pending instead of polling an unchanged current span.

Useful? React with 👍 / 👎.

active = current
break
}
}
}
if (!active) {
const span = ctx.tracer.startSpan(
`${ctx.tracePrefix}llm`,
Expand Down
73 changes: 63 additions & 10 deletions src/handlers/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
isTraceEnabled,
markSeen,
modelRef,
redactErrorMessage,
resolveSubagentTraceContext,
setBoundedMap,
totalTokens,
Expand Down Expand Up @@ -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)
Expand All @@ -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,
Comment thread
enternal111 marked this conversation as resolved.
Resolved
})
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`,
Expand All @@ -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,
},
},
Expand Down Expand Up @@ -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))
Comment thread
enternal111 marked this conversation as resolved.
Resolved
} else if (outcome.type === "interrupted") {
span.setAttribute("session.interrupted_reason", outcome.reason)
span.setStatus({ code: SpanStatusCode.OK })
Expand Down Expand Up @@ -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) {
Comment thread
enternal111 marked this conversation as resolved.
Resolved
danglingRun.end()
ctx.tracing.runSpans.delete(sessionID)
ctx.tracing.runSpanContexts.delete(sessionID)
}
return
}
const identity = ctx.tracing.sessionIdentity.get(sessionID)
Expand All @@ -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
Expand Down
18 changes: 11 additions & 7 deletions src/handlers/step.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ 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,
errorSummary,
genAiProviderName,
isMetricEnabled,
isTraceEnabled,
redactErrorMessage,
markSeen,
resolveRunContext,
setBoundedMap,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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 }]),
Expand Down Expand Up @@ -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 } : {}),
Comment thread
enternal111 marked this conversation as resolved.
Resolved
})
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)
Expand All @@ -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,
},
Expand Down
Loading
Loading