Skip to content

Commit c90fc6a

Browse files
authored
feat(opencode): add OTLP observability support (#21387)
1 parent bc1840b commit c90fc6a

3 files changed

Lines changed: 39 additions & 1 deletion

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Layer } from "effect"
2+
import { FetchHttpClient } from "effect/unstable/http"
3+
import { Otlp } from "effect/unstable/observability"
4+
import { Flag } from "@/flag/flag"
5+
import { CHANNEL, VERSION } from "@/installation/meta"
6+
7+
export namespace Observability {
8+
export const enabled = !!Flag.OTEL_EXPORTER_OTLP_ENDPOINT
9+
10+
export const layer = !Flag.OTEL_EXPORTER_OTLP_ENDPOINT
11+
? Layer.empty
12+
: Otlp.layerJson({
13+
baseUrl: Flag.OTEL_EXPORTER_OTLP_ENDPOINT,
14+
loggerMergeWithExisting: false,
15+
resource: {
16+
serviceName: "opencode",
17+
serviceVersion: VERSION,
18+
attributes: {
19+
"deployment.environment.name": CHANNEL === "local" ? "local" : CHANNEL,
20+
"opencode.client": Flag.OPENCODE_CLIENT,
21+
},
22+
},
23+
headers: Flag.OTEL_EXPORTER_OTLP_HEADERS
24+
? Flag.OTEL_EXPORTER_OTLP_HEADERS.split(",").reduce(
25+
(acc, x) => {
26+
const [key, value] = x.split("=")
27+
acc[key] = value
28+
return acc
29+
},
30+
{} as Record<string, string>,
31+
)
32+
: undefined,
33+
}).pipe(Layer.provide(FetchHttpClient.layer))
34+
}

packages/opencode/src/effect/run-service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as ServiceMap from "effect/ServiceMap"
33
import { Instance } from "@/project/instance"
44
import { Context } from "@/util/context"
55
import { InstanceRef } from "./instance-ref"
6+
import { Observability } from "./oltp"
67

78
export const memoMap = Layer.makeMemoMapUnsafe()
89

@@ -18,7 +19,7 @@ function attach<A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R>
1819

1920
export function makeRuntime<I, S, E>(service: ServiceMap.Service<I, S>, layer: Layer.Layer<I, E>) {
2021
let rt: ManagedRuntime.ManagedRuntime<I, E> | undefined
21-
const getRuntime = () => (rt ??= ManagedRuntime.make(layer, { memoMap }))
22+
const getRuntime = () => (rt ??= ManagedRuntime.make(Layer.merge(layer, Observability.layer), { memoMap }))
2223

2324
return {
2425
runSync: <A, Err>(fn: (svc: S) => Effect.Effect<A, Err, I>) => getRuntime().runSync(attach(service.use(fn))),

packages/opencode/src/flag/flag.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ function falsy(key: string) {
1111
}
1212

1313
export namespace Flag {
14+
export const OTEL_EXPORTER_OTLP_ENDPOINT = process.env["OTEL_EXPORTER_OTLP_ENDPOINT"]
15+
export const OTEL_EXPORTER_OTLP_HEADERS = process.env["OTEL_EXPORTER_OTLP_HEADERS"]
16+
1417
export const OPENCODE_AUTO_SHARE = truthy("OPENCODE_AUTO_SHARE")
1518
export const OPENCODE_AUTO_HEAP_SNAPSHOT = truthy("OPENCODE_AUTO_HEAP_SNAPSHOT")
1619
export const OPENCODE_GIT_BASH_PATH = process.env["OPENCODE_GIT_BASH_PATH"]

0 commit comments

Comments
 (0)