Workers and other runtimes
Run the Node.js SDK on Cloudflare Workers, edge runtimes and browsers, and on the Node.js main thread. Covers flush with waitUntil, AsyncLocalStorage labels, compression and shutdown behavior.
| Runtime | Behavior |
|---|---|
| Node.js 22 or newer | Everything described in these docs. zstd needs Node.js 22.15 or newer; older 22.x versions use gzip. |
| Cloudflare Workers, edge runtimes, browsers | No worker thread: serialization runs in the same isolate, in short slices. Compression uses CompressionStream('gzip'). No automatic labels. |
| Node.js main thread | Set worker_thread: false, or use redact_hook, error_details or a custom fetch: functions cannot move to another thread. |
Cloudflare Workers
import { create_inspector } from '@inspector-rest/sdk';
import instrument_axios from '@inspector-rest/sdk/axios';
import axios from 'axios';
export default {
async fetch(request, env, ctx) {
const inspector = create_inspector({ server_key: env.INSPECTOR_REST_KEY, service: 'edge-api' });
instrument_axios(axios, inspector);
const response = await handle(request);
// Timers do not run after the response is sent, so flush explicitly.
ctx.waitUntil(inspector.flush());
return response;
},
};- Create the inspector once per isolate where you can (module scope), and call
flush()at the end of each request. - With the
nodejs_compatflag,with_labelsusesAsyncLocalStorage, so concurrent requests do not see each other’s labels. - Runtimes without a file system add no automatic labels (
server_host,app_package,app_commit).
Labels and async context
with_labels uses AsyncLocalStorage where it exists (Node.js, and Workers with nodejs_compat). The labels follow the async call tree. Where it is unavailable, with_labels applies to the synchronous part of the callback only. See Labels.
Shutdown on Node.js
The SDK flushes on beforeExit and on SIGTERM and SIGINT. The signal is re-raised afterwards unless your application has its own handler. In a short script, await inspector.close() at the end guarantees delivery of everything, including requests that were still running (reported as in progress).
Serverless functions that freeze the process after the response (AWS Lambda and similar) should await inspector.flush() before returning.