Skip to content
Maple Docs
Open app

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/index-D3yLFpFl.js:1874:31887 shows as your own file, function and line on the issue page.

If your Worker does not use Alchemy or Effect, follow Cloudflare Worker with Wrangler instead.

Prerequisites

  • An Alchemy stack that deploys the Worker, with alchemy and @alchemy.run/cloudflare 2.0.0-beta.84 or later and effect 4. Earlier betas have no Bundle.outputDirectory, which step 3 uses. This guide was written against 2.0.0-beta.87.

  • @maple-dev/alchemy 0.8.0 or later:

    npm install @maple-dev/alchemy
    pnpm add @maple-dev/alchemy
    bun add @maple-dev/alchemy
  • A Maple API key from Settings → Keys, set as MAPLE_API_KEY where you run alchemy 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 set MAPLE_API_URL=https://api.eu.maple.dev for the deploy and pass endpoint: "https://ingest.eu.maple.dev" to Telemetry.

  • 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:

  1. The serviceVersion option.
  2. service.version in attributes or in the OTEL_RESOURCE_ATTRIBUTES env var.
  3. A commit SHA in the Worker’s env: COMMIT_SHA, RAILWAY_GIT_COMMIT_SHA, VERCEL_GIT_COMMIT_SHA, CF_PAGES_COMMIT_SHA or RENDER_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_SHA=$(git rev-parse HEAD) 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 .alchemy/bundles/<stack>-<stage>-<worker id>, 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 }
	}),
)
  • fqn is the Worker’s id, the first argument to Cloudflare.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 /.
  • service is the serviceName you passed to Telemetry.
  • matchBy: "release" is required here. Worker telemetry reports no debug ids, so the maps are matched by service, release and path.
  • after: api.versionId orders 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/scheduled-D3yLFpFl.js:1874:31887, 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

  1. Deploy, then check the log for Uploaded N source map(s) for api.
  2. List the maps: GET /v2/source_maps?service=api&version=<commit> (see Upload with the API).
  3. Make the Worker fail in a way that becomes an issue, for example a handler that dies with an error and answers 500.
  4. 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. Set build.output.sourcemapExcludeSources: true on the Worker, as above.
  • A skipped map that says matchBy: "release" needs a release. release was empty. With the props above a missing GITHUB_SHA fails the deploy earlier, on the Config.String read, 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, or fqn does not name the Worker. Check that .alchemy/bundles/<stack>-<stage>-<worker id> exists after a deploy that rebuilt the Worker.
  • Frames stay minified. The service.version on the error does not equal the uploaded release, or service.name does not equal service. Open the error’s span in Traces and compare its resource attributes with the GET /v2/source_maps listing. A Worker deployed without COMMIT_SHA reports no service.version at 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 exception event.
  • Frames map but show no code. The map has no sourcesContent, which is expected with sourcemapExcludeSources: true.

Next steps