Skip to content
Draft
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
54 changes: 53 additions & 1 deletion bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions packages/opencode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@
"@opencode-ai/sdk": "workspace:*",
"@opencode-ai/util": "workspace:*",
"@openrouter/ai-sdk-provider": "2.4.2",
"@opentelemetry/api": "1.9.1",
"@opentelemetry/exporter-trace-otlp-http": "0.214.0",
"@opentelemetry/resources": "2.6.1",
"@opentelemetry/sdk-trace-base": "2.6.1",
"@opentelemetry/semantic-conventions": "1.40.0",
"@opentui/core": "0.1.97",
"@opentui/solid": "0.1.97",
"@parcel/watcher": "2.5.1",
Expand Down
5 changes: 4 additions & 1 deletion packages/opencode/src/agent/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import PROMPT_COMPACTION from "./prompt/compaction.txt"
import PROMPT_EXPLORE from "./prompt/explore.txt"
import PROMPT_SUMMARY from "./prompt/summary.txt"
import PROMPT_TITLE from "./prompt/title.txt"
import { Flag } from "@/flag/flag"
import { Permission } from "@/permission"
import { mergeDeep, pipe, sortBy, values } from "remeda"
import { Global } from "@/global"
Expand Down Expand Up @@ -347,7 +348,9 @@ export namespace Agent {

const params = {
experimental_telemetry: {
isEnabled: cfg.experimental?.openTelemetry,
isEnabled: cfg.experimental?.openTelemetry || !!Flag.OTEL_EXPORTER_OTLP_ENDPOINT,
recordInputs: true,
recordOutputs: true,
metadata: {
userId: cfg.username ?? "unknown",
},
Expand Down
1 change: 1 addition & 0 deletions packages/opencode/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import "./otel-bridge.js"
import yargs from "yargs"
import { hideBin } from "yargs/helpers"
import { RunCommand } from "./cli/cmd/run"
Expand Down
55 changes: 55 additions & 0 deletions packages/opencode/src/otel-bridge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Registers a global OTel tracer provider so the AI SDK's
* `experimental_telemetry` spans are exported alongside Effect's own spans.
*
* Effect's Otlp.layerJson (in effect/oltp.ts) handles Effect-service tracing
* but does NOT register a global provider — the AI SDK only talks to the
* global one. This bridge fills the gap with a lightweight BasicTracerProvider.
*
* Import this module as the FIRST import in the entry point so the provider
* is registered before any AI SDK code runs.
*/
import { Flag } from "@/flag/flag"

const endpoint = Flag.OTEL_EXPORTER_OTLP_ENDPOINT
if (endpoint) {
const { trace } = await import("@opentelemetry/api")
const { BasicTracerProvider, BatchSpanProcessor } = await import("@opentelemetry/sdk-trace-base")
const { OTLPTraceExporter } = await import("@opentelemetry/exporter-trace-otlp-http")
const { resourceFromAttributes } = await import("@opentelemetry/resources")
const { ATTR_SERVICE_NAME, ATTR_SERVICE_VERSION } = await import("@opentelemetry/semantic-conventions")

const { CHANNEL, VERSION } = await import("@/installation/meta")

const provider = new BasicTracerProvider({
resource: resourceFromAttributes({
[ATTR_SERVICE_NAME]: "opencode",
[ATTR_SERVICE_VERSION]: VERSION,
"deployment.environment.name": CHANNEL === "local" ? "local" : CHANNEL,
}),
spanProcessors: [
new BatchSpanProcessor(
new OTLPTraceExporter({
url: `${endpoint.replace(/\/$/, "")}/v1/traces`,
headers: Flag.OTEL_EXPORTER_OTLP_HEADERS
? Flag.OTEL_EXPORTER_OTLP_HEADERS.split(",").reduce(
(acc, x) => {
const [key, ...rest] = x.split("=")
acc[key] = rest.join("=")
return acc
},
{} as Record<string, string>,
)
: undefined,
}),
),
],
})

trace.setGlobalTracerProvider(provider)

const shutdown = () => provider.shutdown().catch(() => {})
process.on("beforeExit", shutdown)
process.on("SIGINT", shutdown)
process.on("SIGTERM", shutdown)
}
4 changes: 3 additions & 1 deletion packages/opencode/src/session/llm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,9 @@ export namespace LLM {
],
}),
experimental_telemetry: {
isEnabled: cfg.experimental?.openTelemetry,
isEnabled: cfg.experimental?.openTelemetry || !!Flag.OTEL_EXPORTER_OTLP_ENDPOINT,
recordInputs: true,
recordOutputs: true,
metadata: {
userId: cfg.username ?? "unknown",
sessionId: input.sessionID,
Expand Down
Loading