feat(effect-sdk): opt-in native Cloudflare tracer for the Workers preset - #731
Open
Makisuo wants to merge 1 commit into
Open
feat(effect-sdk): opt-in native Cloudflare tracer for the Workers preset#731Makisuo wants to merge 1 commit into
Makisuo wants to merge 1 commit into
Conversation
Add `tracer: "native"` to `MapleCloudflareSDK.make`. Every sampled Effect span is mirrored onto `tracing.startActiveSpan` from `cloudflare:workers`, so it lands in the same trace as Cloudflare's own fetch/KV/R2/D1 spans and is exported by the Worker's ObservabilityDestination: no in-isolate buffer, no `ctx.waitUntil` flush, no ingest key in the Worker. That also makes it usable from Durable Object and Workflow isolates, where the flush is unreliable. Each mirrored span captures `AsyncLocalStorage.snapshot()` inside its callback, children open inside the parent's snapshot, and the tracer's `context` hook runs every fiber step inside the current span's snapshot, so spans opened after a yield still nest correctly and runtime spans attach under the right Effect span. Cloudflare's `isTraced` cascades into Effect's `sampled`. Cloudflare spans carry only scalar attributes, so a failed exit is recorded as `exception.type` / `exception.message` / `exception.stacktrace` / `error.type` instead of an `exception` event; non-scalar attributes, events and links stay Effect-local. The exit classification shared with the OTLP buffer tracer moves to `shared/span-exit.ts` so both paths agree on what is an error. Logs stay with Workers Logs in this mode (no OTLP logger): their Effect trace ids would never match the Cloudflare trace ids on the exported spans. `cloudflare:workers` and `node:async_hooks` are imported dynamically by a variable specifier and narrowed by type guards; when either API is absent the layer logs one notice and keeps spans Effect-local. The default mode and the api/alerting workers are unchanged.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds an experimental
tracer: "native"mode to@maple-dev/effect-sdk/cloudflare. Every sampled Effect span is mirrored ontotracing.startActiveSpanfromcloudflare:workers, so it joins the same trace as Cloudflare's auto-instrumented fetch/KV/R2/D1 spans and is exported by the customer's ObservabilityDestination. No in-isolate buffer, noctx.waitUntil(flush), no ingest key in the Worker. The default OTLP mode and the api/alerting workers are unchanged.Why
The OTLP buffer flush is unreliable from Durable Object and Workflow isolates and requires an ingest key in the Worker. Cloudflare's own tracing (compatibility date >= 2026-07-28) can carry the spans out instead.
How
cloudflare/native-tracer.ts: an EffectTracer.makeoverstartActiveSpan. Each mirrored span capturesAsyncLocalStorage.snapshot()inside its callback, children open inside the parent's snapshot, and thecontexthook runs every fiber step inside the current span's snapshot. Cloudflare'sisTracedcascades into Effect'ssampled, so an untraced invocation opens no Cloudflare spans at all.exception.type,exception.message,exception.stacktrace,error.type) since Cloudflare spans have no events. A parallel change teaches the error MV to read those attributes.shared/span-exit.ts: the success / interrupted / ignored / anticipated / failed classification is shared between the OTLP buffer tracer and the native tracer so they cannot disagree. The OTLP tracer's behaviour is unchanged.cloudflare:workersandnode:async_hooksare imported dynamically by a variable specifier and narrowed by type guards (the installed workers-types only knowenterSpan). A missing API fails withNativeTracingUnavailable, which the layer turns into oneEffect.logInfonotice plus Effect-local spans.Reviewer notes
Config.traceroption and the README section are the only public surface changes.Testing
native-tracer.test.ts: a faketracingbacked by a realAsyncLocalStorageasserts parentage across a scheduler hop into a foreign async context, interleaved fibers, attribute forwarding, all five exit classes, theisTracedcascade, dropped names, host resolution failures, and the fallback path.index.test.ts: native mode off Workers builds the layer, makes no fetch calls, resolvesflush, and logs one notice.tsc --noEmit, oxlint and oxfmt clean on the package. tsdown build confirmed the dynamic imports survive unbundled.Need help on this PR? Tag
@codesmith-botwith what you need. Autofix is disabled.