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
4 changes: 4 additions & 0 deletions bun.lock

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

1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
"@opentelemetry/api": "1.9.0",
"@opentelemetry/context-async-hooks": "2.6.1",
"@opentelemetry/exporter-trace-otlp-http": "0.214.0",
"@opentelemetry/exporter-trace-otlp-proto": "0.214.0",
"@opentelemetry/sdk-trace-base": "2.6.1",
"@parcel/watcher": "2.5.1",
"@silvia-odwyer/photon-node": "0.3.4",
Expand Down
12 changes: 12 additions & 0 deletions packages/core/src/flag/flag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ export const Flag = {
OTEL_EXPORTER_OTLP_ENDPOINT: process.env["OTEL_EXPORTER_OTLP_ENDPOINT"],
OTEL_EXPORTER_OTLP_HEADERS: process.env["OTEL_EXPORTER_OTLP_HEADERS"],

// Evaluated at access time because tests and embedding runtimes can set OTLP
// protocol env vars after this module has loaded.
get OTEL_EXPORTER_OTLP_PROTOCOL() {
return process.env["OTEL_EXPORTER_OTLP_PROTOCOL"]
},
get OTEL_EXPORTER_OTLP_TRACES_PROTOCOL() {
return process.env["OTEL_EXPORTER_OTLP_TRACES_PROTOCOL"]
},
get OTEL_EXPORTER_OTLP_LOGS_PROTOCOL() {
return process.env["OTEL_EXPORTER_OTLP_LOGS_PROTOCOL"]
},

OPENCODE_AUTO_HEAP_SNAPSHOT: truthy("OPENCODE_AUTO_HEAP_SNAPSHOT"),
OPENCODE_GIT_BASH_PATH: process.env["OPENCODE_GIT_BASH_PATH"],
OPENCODE_CONFIG: process.env["OPENCODE_CONFIG"],
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/observability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ export * as Observability from "./observability"
import { NodeFileSystem } from "@effect/platform-node"
import { Effect, Layer, Logger, References } from "effect"
import { FetchHttpClient } from "effect/unstable/http"
import { OtlpSerialization } from "effect/unstable/observability"
import { Logging } from "./observability/logging"
import { Otlp } from "./observability/otlp"

export const layer = Layer.unwrap(
Effect.gen(function* () {
const logs = Logger.layer([...Logging.loggers(), ...Otlp.loggers()], { mergeWithExisting: false }).pipe(
Layer.provide(NodeFileSystem.layer),
Layer.provide(OtlpSerialization.layerJson),
Layer.provide(Otlp.serializationLayer()),
Layer.provide(FetchHttpClient.layer),
Layer.orDie,
Layer.merge(Layer.succeed(References.MinimumLogLevel, Logging.minimumLogLevel())),
Expand Down
30 changes: 28 additions & 2 deletions packages/core/src/observability/otlp.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { Layer } from "effect"
import { OtlpLogger } from "effect/unstable/observability"
import { OtlpLogger, OtlpSerialization } from "effect/unstable/observability"
import { Flag } from "../flag/flag"
import { InstallationChannel, InstallationVersion } from "../installation/version"
import { runID } from "./shared"

const endpoint = Flag.OTEL_EXPORTER_OTLP_ENDPOINT

export type OtlpProtocol = "http/json" | "http/protobuf"
export type OtlpSignal = "logs" | "traces"

const defaultProtocol: OtlpProtocol = "http/json"

const headers = Flag.OTEL_EXPORTER_OTLP_HEADERS
? Flag.OTEL_EXPORTER_OTLP_HEADERS.split(",").reduce(
(acc, entry) => {
Expand All @@ -17,6 +22,24 @@ const headers = Flag.OTEL_EXPORTER_OTLP_HEADERS
)
: undefined

function protocolValue(signal: OtlpSignal) {
return (
(signal === "logs" ? Flag.OTEL_EXPORTER_OTLP_LOGS_PROTOCOL : Flag.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL) ??
Flag.OTEL_EXPORTER_OTLP_PROTOCOL
)
}

export function protocol(signal: OtlpSignal): OtlpProtocol {
const value = protocolValue(signal)
if (value === "http/json") return value
if (value === "http/protobuf") return value
return defaultProtocol
}

export function serializationLayer() {
return protocol("logs") === "http/protobuf" ? OtlpSerialization.layerProtobuf : OtlpSerialization.layerJson
}

function resourceAttributes() {
const value = process.env.OTEL_RESOURCE_ATTRIBUTES
if (!value) return {}
Expand Down Expand Up @@ -55,7 +78,10 @@ export function loggers() {
export async function tracingLayer() {
if (!endpoint) return Layer.empty
const NodeSdk = await import("@effect/opentelemetry/NodeSdk")
const OTLP = await import("@opentelemetry/exporter-trace-otlp-http")
const OTLP =
protocol("traces") === "http/protobuf"
? await import("@opentelemetry/exporter-trace-otlp-proto")
: await import("@opentelemetry/exporter-trace-otlp-http")
const SdkBase = await import("@opentelemetry/sdk-trace-base")
const { AsyncLocalStorageContextManager } = await import("@opentelemetry/context-async-hooks")
const { context } = await import("@opentelemetry/api")
Expand Down
52 changes: 51 additions & 1 deletion packages/core/test/effect/observability.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,73 @@
import { afterEach, describe, expect, test } from "bun:test"
import { NodeFileSystem } from "@effect/platform-node"
import { Effect, Layer, Logger } from "effect"
import { OtlpSerialization } from "effect/unstable/observability"
import fs from "fs/promises"
import os from "os"
import path from "path"
import { fileLogger } from "../../src/observability/logging"
import { resource } from "../../src/observability/otlp"
import { protocol, resource, serializationLayer } from "../../src/observability/otlp"

const otelResourceAttributes = process.env.OTEL_RESOURCE_ATTRIBUTES
const otelProtocol = process.env.OTEL_EXPORTER_OTLP_PROTOCOL
const otelTracesProtocol = process.env.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL
const otelLogsProtocol = process.env.OTEL_EXPORTER_OTLP_LOGS_PROTOCOL
const opencodeClient = process.env.OPENCODE_CLIENT

afterEach(() => {
if (otelResourceAttributes === undefined) delete process.env.OTEL_RESOURCE_ATTRIBUTES
else process.env.OTEL_RESOURCE_ATTRIBUTES = otelResourceAttributes
if (otelProtocol === undefined) delete process.env.OTEL_EXPORTER_OTLP_PROTOCOL
else process.env.OTEL_EXPORTER_OTLP_PROTOCOL = otelProtocol
if (otelTracesProtocol === undefined) delete process.env.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL
else process.env.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL = otelTracesProtocol
if (otelLogsProtocol === undefined) delete process.env.OTEL_EXPORTER_OTLP_LOGS_PROTOCOL
else process.env.OTEL_EXPORTER_OTLP_LOGS_PROTOCOL = otelLogsProtocol

if (opencodeClient === undefined) delete process.env.OPENCODE_CLIENT
else process.env.OPENCODE_CLIENT = opencodeClient
})

describe("protocol", () => {
test("defaults to json", () => {
delete process.env.OTEL_EXPORTER_OTLP_PROTOCOL
delete process.env.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL
delete process.env.OTEL_EXPORTER_OTLP_LOGS_PROTOCOL

expect(protocol("logs")).toBe("http/json")
expect(protocol("traces")).toBe("http/json")
expect(serializationLayer()).toBe(OtlpSerialization.layerJson)
})

test("uses general protobuf protocol for logs and traces", () => {
process.env.OTEL_EXPORTER_OTLP_PROTOCOL = "http/protobuf"
delete process.env.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL
delete process.env.OTEL_EXPORTER_OTLP_LOGS_PROTOCOL

expect(protocol("logs")).toBe("http/protobuf")
expect(protocol("traces")).toBe("http/protobuf")
expect(serializationLayer()).toBe(OtlpSerialization.layerProtobuf)
})

test("signal-specific protocol overrides the general protocol", () => {
process.env.OTEL_EXPORTER_OTLP_PROTOCOL = "http/protobuf"
process.env.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL = "http/json"
process.env.OTEL_EXPORTER_OTLP_LOGS_PROTOCOL = "http/protobuf"

expect(protocol("logs")).toBe("http/protobuf")
expect(protocol("traces")).toBe("http/json")
expect(serializationLayer()).toBe(OtlpSerialization.layerProtobuf)
})

test("unsupported protocol falls back to json", () => {
process.env.OTEL_EXPORTER_OTLP_PROTOCOL = "grpc"
delete process.env.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL
delete process.env.OTEL_EXPORTER_OTLP_LOGS_PROTOCOL

expect(protocol("logs")).toBe("http/json")
})
})

describe("resource", () => {
test("parses and decodes OTEL resource attributes", () => {
process.env.OTEL_RESOURCE_ATTRIBUTES =
Expand Down
1 change: 1 addition & 0 deletions packages/opencode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
"@opentelemetry/api": "1.9.0",
"@opentelemetry/context-async-hooks": "2.6.1",
"@opentelemetry/exporter-trace-otlp-http": "0.214.0",
"@opentelemetry/exporter-trace-otlp-proto": "0.214.0",
"@opentelemetry/sdk-trace-base": "2.6.1",
"@opentelemetry/sdk-trace-node": "2.6.1",
"@opentui/core": "catalog:",
Expand Down
3 changes: 3 additions & 0 deletions packages/opencode/src/control-plane/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,9 @@ export const layer = Layer.effect(
OPENCODE_EXPERIMENTAL_WORKSPACES: "true",
OTEL_EXPORTER_OTLP_HEADERS: process.env.OTEL_EXPORTER_OTLP_HEADERS,
OTEL_EXPORTER_OTLP_ENDPOINT: process.env.OTEL_EXPORTER_OTLP_ENDPOINT,
OTEL_EXPORTER_OTLP_PROTOCOL: process.env.OTEL_EXPORTER_OTLP_PROTOCOL,
OTEL_EXPORTER_OTLP_TRACES_PROTOCOL: process.env.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL,
OTEL_EXPORTER_OTLP_LOGS_PROTOCOL: process.env.OTEL_EXPORTER_OTLP_LOGS_PROTOCOL,
OTEL_RESOURCE_ATTRIBUTES: process.env.OTEL_RESOURCE_ATTRIBUTES,
}

Expand Down
9 changes: 9 additions & 0 deletions packages/opencode/test/control-plane/workspace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ const originalEnv = {
OPENCODE_EXPERIMENTAL_WORKSPACES: process.env.OPENCODE_EXPERIMENTAL_WORKSPACES,
OTEL_EXPORTER_OTLP_HEADERS: process.env.OTEL_EXPORTER_OTLP_HEADERS,
OTEL_EXPORTER_OTLP_ENDPOINT: process.env.OTEL_EXPORTER_OTLP_ENDPOINT,
OTEL_EXPORTER_OTLP_PROTOCOL: process.env.OTEL_EXPORTER_OTLP_PROTOCOL,
OTEL_EXPORTER_OTLP_TRACES_PROTOCOL: process.env.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL,
OTEL_EXPORTER_OTLP_LOGS_PROTOCOL: process.env.OTEL_EXPORTER_OTLP_LOGS_PROTOCOL,
OTEL_RESOURCE_ATTRIBUTES: process.env.OTEL_RESOURCE_ATTRIBUTES,
}

Expand Down Expand Up @@ -428,6 +431,9 @@ describe("workspace CRUD", () => {
process.env.OPENCODE_AUTH_CONTENT = JSON.stringify({ test: { type: "api", key: "secret" } })
process.env.OTEL_EXPORTER_OTLP_HEADERS = "authorization=otel"
process.env.OTEL_EXPORTER_OTLP_ENDPOINT = "https://otel.test"
process.env.OTEL_EXPORTER_OTLP_PROTOCOL = "http/protobuf"
process.env.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL = "http/json"
process.env.OTEL_EXPORTER_OTLP_LOGS_PROTOCOL = "http/protobuf"
process.env.OTEL_RESOURCE_ATTRIBUTES = "service.name=opencode-test"

const workspaceID = WorkspaceV2.ID.ascending("wrk_create_local")
Expand Down Expand Up @@ -491,6 +497,9 @@ describe("workspace CRUD", () => {
expect(recorded.calls.create[0].env.OPENCODE_EXPERIMENTAL_WORKSPACES).toBe("true")
expect(recorded.calls.create[0].env.OTEL_EXPORTER_OTLP_HEADERS).toBe("authorization=otel")
expect(recorded.calls.create[0].env.OTEL_EXPORTER_OTLP_ENDPOINT).toBe("https://otel.test")
expect(recorded.calls.create[0].env.OTEL_EXPORTER_OTLP_PROTOCOL).toBe("http/protobuf")
expect(recorded.calls.create[0].env.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL).toBe("http/json")
expect(recorded.calls.create[0].env.OTEL_EXPORTER_OTLP_LOGS_PROTOCOL).toBe("http/protobuf")
expect(recorded.calls.create[0].env.OTEL_RESOURCE_ATTRIBUTES).toBe("service.name=opencode-test")
expect((yield* workspace.status()).find((item) => item.workspaceID === workspaceID)?.status).toBe("connected")

Expand Down
Loading