The official Serverless & Worker SDK for Senzor APM.
Designed specifically for Cloudflare Workers, Cloudflare Pages, Nitro, and modern Edge runtimes.
- Zero-Config Auto-Instrumentation: Automatically captures all global
fetchcalls. - Context Propagation: Uses
AsyncLocalStorage(vianodejs_compat) to track requests across async boundaries without manual passing. - Lightweight: < 5KB gzip, zero external dependencies.
- Non-Blocking: Uses
ctx.waitUntilto flush data without adding latency to user responses. - W3C Trace Context: Automatically injects
traceparentheaders into outgoing requests for distributed tracing.
npm install @senzops/apm-workerAdd the nodejs_compat flag to your wrangler.toml. This is required for the SDK to track context across async fetch calls.
# wrangler.toml
compatibility_flags = [ "nodejs_compat" ]
compatibility_date = "2024-09-23"Initialize Senzor in the global scope and wrap your fetch handler.
import { Senzor } from "@senzops/apm-worker";
Senzor.init({
apiKey: "sz_apm_...",
});
export default {
fetch: Senzor.worker(async (request, env, ctx) => {
return new Response("Hello World!");
}),
};For Nitro (standalone) or Nuxt, use a server plugin to instrument the entire application globally without wrapping individual handlers.
Create server/plugins/senzor.ts:
import { Senzor } from "@senzops/apm-worker";
export default defineNitroPlugin((nitroApp) => {
// 1. Initialize
// Use process.env or runtime config if available
Senzor.init({
apiKey: process.env.SENZOR_API_KEY || "sz_apm_...",
});
// 2. Register Global Instrumentation
Senzor.nitroPlugin(nitroApp);
});Ensure nodejs_compat is enabled in your nitro.config.ts:
// nitro.config.ts
export default defineNitroConfig({
cloudflare: {
wrangler: {
compatibility_flags: ["nodejs_compat"],
},
},
});That's it! Every request to your Nitro app is now traced, and any fetch calls made within your API routes will automatically appear as spans in the trace.
For security, do not commit your API key.
- Run
npx wrangler secret put SENZOR_API_KEY. - Update your worker to initialize lazily or use build-time variables.
If your Worker calls other services (like a backend API), this SDK automatically adds the traceparent header.
Ensure your backend services (Node.js, Python, Go) are configured to extract this header to see a connected trace from Edge -> Backend.
Any uncaught exception thrown in your handler is automatically captured, logged, and reported to Senzor with the stack trace.