Cloudflare Worker with Alchemy and Effect
Trace an Effect Worker deployed with Alchemy into Maple, and upload its source maps so error stacks on the issue page show your original file, line and function.
This guide sets up a Cloudflare Worker written with Effect and deployed with Alchemy so that:
- every request is traced into Maple, with its logs and errors, and
- the Worker’s source maps are uploaded on each deploy, so a stack frame like
file:///bundle/shows as your own file, function and line on the issue page.index-D3yLFpFl.js:1874:31887
If your Worker does not use Alchemy or Effect, follow Cloudflare Worker with Wrangler instead.
Prerequisites
-
An Alchemy stack that deploys the Worker, with
alchemyand@alchemy.run/cloudflare2.0.0-beta.84 or later andeffect4. Earlier betas have noBundle.outputDirectory, which step 3 uses. This guide was written against 2.0.0-beta.87. -
@maple-dev/alchemy0.8.0 or later:npm install @maple-dev/alchemypnpm add @maple-dev/alchemybun add @maple-dev/alchemy -
A Maple API key from Settings → Keys, set as
MAPLE_API_KEYwhere you runalchemy deploy. It needs the Source maps write scope (source_maps:write) for the upload, plus the scopes in Alchemy authentication for the ingest key. An org admin’s key with full access covers both. For an EU organization, also setMAPLE_API_URL=for the deploy and passhttps://api.eu.maple.dev endpoint: "https://ingest.eu.maple.dev"toTelemetry. -
A release id for each deploy that both the Worker and the upload can read. This guide uses the git commit, which GitHub Actions sets as
GITHUB_SHA.
1. Trace the Worker
Register Maple.providers() in the stack and provide Telemetry on the Worker. This is the Alchemy quick start with one addition: the COMMIT_SHA binding, covered in step 2.
// src/maple.ts
import * as Maple from "@maple-dev/alchemy"
export const Ingest = Maple.IngestKeys("ingest")
// src/worker.ts
import * as Cloudflare from "@alchemy.run/cloudflare"
import { Telemetry } from "@maple-dev/alchemy/telemetry"
import { Config, Effect } from "effect"
import { HttpServerResponse } from "effect/http"
import { Ingest } from "./maple.ts"
export const SERVICE_NAME = "api"
// Plan-time only: the deployed bundle folds __ALCHEMY_RUNTIME__ to true and skips the env read.
const props = Effect.gen(function* () {
if (globalThis.__ALCHEMY_RUNTIME__) return { main: import.meta.url }
const release = yield* Config.String("GITHUB_SHA")
return { main: import.meta.url, env: { COMMIT_SHA: release } }
})
export default Cloudflare.Worker(
"api",
props,
Effect.gen(function* () {
const fetch = Effect.succeed(HttpServerResponse.text("Hello from Maple"))
return { fetch }
}).pipe(Effect.provide(Telemetry({ serviceName: SERVICE_NAME, ingestKey: Ingest }))),
)
Telemetry records a server span for each request, attaches Effect logs and spans to it, and flushes after the response. A failure in the handler becomes an exception event with exception.stacktrace on the span, which is what Maple turns into an issue. See Effect SDK on Cloudflare Workers for every SDK option.
2. Report the release as service.version
Maple finds a Worker’s map by service, release and path. The release you upload under must equal the service.version on the Worker’s telemetry.
The SDK sets service.version from the first of:
- The
serviceVersionoption. service.versioninattributesor in theOTEL_RESOURCE_ATTRIBUTESenv var.- A commit SHA in the Worker’s env:
COMMIT_SHA,RAILWAY_GIT_COMMIT_SHA,VERCEL_GIT_COMMIT_SHA,CF_PAGES_COMMIT_SHAorRENDER_GIT_COMMIT.
The Worker above binds COMMIT_SHA from GITHUB_SHA at deploy time, so its spans carry the commit as service.version. The upload in the next step uses the same GITHUB_SHA. If you use your own release ids instead of commits, bind that value as COMMIT_SHA and upload under the same value. A deploy without GITHUB_SHA fails on the Config.String read. Set it for local deploys too, for example export GITHUB_ before alchemy deploy.
3. Upload the Worker’s source maps
Alchemy bundles the Worker with source maps on and hidden: each .map file sits beside its JS file and no sourceMappingURL comment ships. It writes the bundle to ., and Bundle.outputDirectory returns that path. Point Maple.SourceMaps at it:
// alchemy.run.ts
import * as Alchemy from "alchemy"
import * as Bundle from "alchemy/Bundle"
import { Stack } from "alchemy/Stack"
import * as Cloudflare from "@alchemy.run/cloudflare"
import * as Maple from "@maple-dev/alchemy"
import { Effect, Layer } from "effect"
import Api, { SERVICE_NAME } from "./src/worker.ts"
export default Alchemy.Stack(
"my-app",
{
providers: Layer.mergeAll(Cloudflare.providers(), Maple.providers()),
state: Cloudflare.state(),
},
Effect.gen(function* () {
const api = yield* Api
const stack = yield* Stack
const dir = yield* Bundle.outputDirectory({ stack: stack.name, stage: stack.stage, fqn: "api" })
yield* Maple.SourceMaps("api-sourcemaps", {
dir,
service: SERVICE_NAME,
release: process.env.GITHUB_SHA,
matchBy: "release",
after: api.versionId,
})
return { url: api.url }
}),
)
fqnis the Worker’s id, the first argument toCloudflare.Worker, when the Worker is declared at the top level of the stack as above. For a Worker inside a namespace it is the namespace path and the id joined with/.serviceis theserviceNameyou passed toTelemetry.matchBy: "release"is required here. Worker telemetry reports no debug ids, so the maps are matched by service, release and path.after: api.versionIdorders the upload after the Worker’s build and deploy.
Each JS file uploads under its path inside the bundle directory, such as index-D3yLFpFl.js. Maple matches a frame whose path ends with the uploaded path. On one of Maple’s own Workers, production frames read file:///bundle/, so no urlPrefix is set. Check yours in the issue’s Raw stack.
Deploy from CI with the key in the environment. GitHub Actions sets GITHUB_SHA itself. --yes approves the plan, which a deploy without a terminal needs:
- run: npx alchemy deploy --stage prod --yes
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
MAPLE_API_KEY: ${{ secrets.MAPLE_API_KEY }}
The resource uploads only when the maps, service, release, prefix or matchBy changed. A deploy that skips an unchanged build finds no maps on disk, logs a warning and uploads nothing. The next deploy that builds uploads them. Removing the resource deletes no maps, because errors from earlier releases still need them.
Maple uploads the maps of one of its own Workers this way. We have not yet confirmed a mapped stack from that Worker in production, so run the Verify steps after your first deploy.
Keep each map under 10 MiB
Maple refuses a map over 10 MiB, and Maple.SourceMaps skips it with a warning. A Worker that bundles many dependencies can pass that size, mostly from sourcesContent, the copy of every source file inside the map. Leave it out:
return {
main: import.meta.url,
env: { COMMIT_SHA: release },
build: { output: { sourcemapExcludeSources: true } },
}
Frames still map to your file, function and line. The issue page then shows no code around the line, because the map no longer carries it. On one of Maple’s own Workers this took the main chunk’s map from 12.8 MB to 2.7 MB.
Verify
- Deploy, then check the log for
Uploaded N source map(.s) for api - List the maps:
GET /(see Upload with the API).v2/ source_ maps?service=api&version=< commit> - Make the Worker fail in a way that becomes an issue, for example a handler that dies with an error and answers 500.
- Open the issue in Errors. The Stack trace shows Source mapped (or Partly source mapped), with your original file, function and line on each mapped frame. Raw shows the stack as the Worker sent it.
Troubleshooting
Skipped the source map for …: it is over 10 MiB. Setbuild.on the Worker, as above.output. sourcemapExcludeSources: true - A skipped map that says
matchBy: "release"needs a release.releasewas empty. With the props above a missingGITHUB_SHAfails the deploy earlier, on theConfig.Stringread, so this only appears if the Worker gets its release another way. No source maps in …. The Worker’s build did not run in this deploy, orfqndoes not name the Worker. Check that.exists after a deploy that rebuilt the Worker.alchemy/ bundles/ < stack>- < stage>- < worker id> - Frames stay minified. The
service.versionon the error does not equal the uploaded release, orservice.namedoes not equalservice. Open the error’s span in Traces and compare its resource attributes with theGET /v2/source_mapslisting. A Worker deployed withoutCOMMIT_SHAreports noservice.versionat all. If both match, switch the stack to Raw and check that each frame’s path ends with an uploaded path. - No mapped frames because there is no issue. Maps apply to stacks on the issue page. An error that never became an issue, such as a 4xx span with no exception attached, has nothing to map. Throw or fail with an error so the span carries an
exceptionevent. - Frames map but show no code. The map has no
sourcesContent, which is expected withsourcemapExcludeSources: true.
Next steps
- Source maps: how frames find maps, limits and retention.
- Alchemy: alerts, dashboards and keys next to the Worker.
- Effect SDK on Cloudflare Workers: every SDK option.
- Guide: Effect and Alchemy on Cloudflare Workers, with Workers Observability destinations. For a plain Worker, see Cloudflare Workers tracing with OpenTelemetry.
- Errors and issues