| @@ -8,17 +8,27 @@ · 7 unmodified lines |
| 8 | 8 | // `@microlabs/otel-cf-workers`' `instrument(...)` installs in `server.ts`. |
| 9 | 9 | // Flushing is handled by `instrument()` via `ctx.waitUntil` at request end. |
| 10 | 10 | // |
| 11 | | −// - `DoTelemetryLive` (Durable Object path): provisions its own |
| 12 | | −// `WebSdk`-backed tracer via `Effect`. The DO runs in a separate isolate |
| 13 | | −// and we deliberately avoid `instrumentDO` (it wraps DO methods in a way |
| 14 | | −// that breaks `this` binding on `WorkerTransport`'s stream primitives — |
| 15 | | −// every MCP request 500s with "Illegal invocation"). The DO uses a |
| 16 | | −// `SimpleSpanProcessor` so spans export immediately; there's no |
| 17 | | −// `ctx.waitUntil` to rely on for batching. Gated inside a |
| 18 | | −// `Layer.unwrapEffect` so the `AXIOM_TOKEN` check happens at |
| 19 | | −// layer-provide time — `server` is now a lazy Proxy, so this is just |
| 20 | | −// defensive against the token genuinely being unset (dev / missing |
| 21 | | −// secret). |
| 11 | +// - `DoTelemetryLive` (Durable Object path): the DO runs in a separate |
| 12 | +// isolate and we deliberately avoid `instrumentDO` (it wraps DO methods |
| 13 | +// in a way that breaks `this` binding on `WorkerTransport`'s stream |
| 14 | +// primitives — every MCP request 500s with "Illegal invocation"). |
| 15 | +// |
| 16 | +// We install a `WebTracerProvider` once per isolate as the global |
| 17 | +// provider (lazy on first `DoTelemetryLive` provide, not at module |
| 18 | +// load — `env` from `cloudflare:workers` is reliably populated at |
| 19 | +// request time but we keep the lazy gate as a defensive cheap no-op). |
| 20 | +// Once installed, the provider lives for the entire isolate lifetime, |
| 21 | +// so deferred MCP SDK callbacks — which fire after the request Effect |
| 22 | +// has resolved — still hit a live `SimpleSpanProcessor` + exporter. |
| 23 | +// |
| 24 | +// Previously the WebSdk layer was scoped per-request: when the outer |
| 25 | +// `Effect.runPromise(...)` resolved, the layer's scope closed and |
| 26 | +// `processor.shutdown()` ran. Engine / runtime spans created from |
| 27 | +// deferred SDK callbacks (which captured the old runtime + tracer) |
| 28 | +// then silently failed to export, even though they showed up in |
| 29 | +// `Effect.currentSpan` traces during execution. The DO has been |
| 30 | +// missing every `executor.code.exec.*` and `executor.runtime.*` span |
| 31 | +// since `DoTelemetryLive` first started shipping spans. |
| 22 | 32 | // --------------------------------------------------------------------------- |
| 23 | 33 | |
| 24 | 34 | // Subpath imports — the barrel `@effect/opentelemetry` re-exports `NodeSdk`, |
| @@ -28,7 +38,7 @@ · 3 unmodified lines |
| 28 | 38 | // unused NodeSdk; vitest does not. |
| 29 | 39 | import * as Resource from "@effect/opentelemetry/Resource"; |
| 30 | 40 | import * as OtelTracer from "@effect/opentelemetry/Tracer"; |
| 31 | | −import * as WebSdk from "@effect/opentelemetry/WebSdk"; |
| 41 | +import { trace } from "@opentelemetry/api"; |
| 32 | 42 | // Force the browser platform entry — the package's conditional export would |
| 33 | 43 | // otherwise resolve to the Node build, which uses `https.request` / `node:http`. |
| 34 | 44 | // Under workerd + unenv's nodejs_compat, `https.request` isn't implemented |
| @@ -36,28 +46,66 @@ import * as WebSdk from "@effect/opentelemetry/WebSdk"; · 1 unmodified line |
| 36 | 46 | // time) and every DO span fails to ship. The browser build uses `fetch()`, |
| 37 | 47 | // which workerd does support. |
| 38 | 48 | import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http/build/esm/platform/browser/index.js"; |
| 49 | +import { resourceFromAttributes } from "@opentelemetry/resources"; |
| 39 | 50 | import { SimpleSpanProcessor } from "@opentelemetry/sdk-trace-base"; |
| 51 | +import { WebTracerProvider } from "@opentelemetry/sdk-trace-web"; |
| 52 | +import { |
| 53 | + ATTR_SERVICE_NAME, |
| 54 | + ATTR_SERVICE_VERSION, |
| 55 | +} from "@opentelemetry/semantic-conventions"; |
| 40 | 56 | import { Effect, Layer } from "effect"; |
| 41 | 57 | |
| 42 | 58 | import { server } from "../env"; |
| 43 | 59 | |
| 60 | +const SERVICE_NAME = "executor-cloud"; |
| 61 | +const SERVICE_VERSION = "1.0.0"; |
| 62 | + |
| 44 | 63 | export const TelemetryLive: Layer.Layer<never> = OtelTracer.layerGlobal.pipe( |
| 45 | | − Layer.provide(Resource.layer({ serviceName: "executor-cloud", serviceVersion: "1.0.0" })), |
| 64 | + Layer.provide(Resource.layer({ serviceName: SERVICE_NAME, serviceVersion: SERVICE_VERSION })), |
| 46 | 65 | ); |
| 47 | 66 | |
| 67 | +// Module-scope: one provider per DO isolate, never shut down. The provider |
| 68 | +// holds the SimpleSpanProcessor + OTLP exporter, so any tracer reference |
| 69 | +// the engine/runtime spans hold (via captured Effect runtimes) keeps |
| 70 | +// finding a live exporter even after the request Effect has resolved. |
| 71 | +let installed = false; |
| 72 | +const ensureGlobalTracerProvider = (): boolean => { |
| 73 | + if (installed) return true; |
| 74 | + if (!server.AXIOM_TOKEN) return false; |
| 75 | + const provider = new WebTracerProvider({ |
| 76 | + resource: resourceFromAttributes({ |
| 77 | + [ATTR_SERVICE_NAME]: SERVICE_NAME, |
| 78 | + [ATTR_SERVICE_VERSION]: SERVICE_VERSION, |
| 79 | + }), |
| 80 | + spanProcessors: [ |
| 81 | + new SimpleSpanProcessor( |
| 82 | + new OTLPTraceExporter({ |
| 83 | + url: server.AXIOM_TRACES_URL, |
| 84 | + headers: { |
| 85 | + Authorization: `Bearer ${server.AXIOM_TOKEN}`, |
| 86 | + "X-Axiom-Dataset": server.AXIOM_DATASET, |
| 87 | + }, |
| 88 | + }), |
| 89 | + ), |
| 90 | + ], |
| 91 | + }); |
| 92 | + // Skip `provider.register()` — its StackContextManager / W3C propagator |
| 93 | + // setup wires the global OTel context API, but Effect's tracer goes |
| 94 | + // through `OtelTracer.layerGlobal` which only needs the global provider, |
| 95 | + // not the OTel context machinery. |
| 96 | + trace.setGlobalTracerProvider(provider); |
| 97 | + installed = true; |
| 98 | + return true; |
| 99 | +}; |
| 100 | + |
| 48 | 101 | export const DoTelemetryLive: Layer.Layer<never> = Layer.unwrapEffect( |
| 49 | | − Effect.sync(() => { |
| 50 | | − if (!server.AXIOM_TOKEN) return Layer.empty; |
| 51 | | − const exporter = new OTLPTraceExporter({ |
| 52 | | − url: server.AXIOM_TRACES_URL, |
| 53 | | − headers: { |
| 54 | | − Authorization: `Bearer ${server.AXIOM_TOKEN}`, |
| 55 | | − "X-Axiom-Dataset": server.AXIOM_DATASET, |
| 56 | | − }, |
| 57 | | − }); |
| 58 | | − return WebSdk.layer(() => ({ |
| 59 | | − resource: { serviceName: "executor-cloud", serviceVersion: "1.0.0" }, |
| 60 | | − spanProcessor: new SimpleSpanProcessor(exporter), |
| 61 | | − })); |
| 62 | | − }), |
| 102 | + Effect.sync(() => |
| 103 | + ensureGlobalTracerProvider() |
| 104 | + ? OtelTracer.layerGlobal.pipe( |
| 105 | + Layer.provide( |
| 106 | + Resource.layer({ serviceName: SERVICE_NAME, serviceVersion: SERVICE_VERSION }), |
| 107 | + ), |
| 108 | + ) |
| 109 | + : Layer.empty, |
| 110 | + ), |
| 63 | 111 | ); |