Problem
Cap'n Web RPC calls do not currently produce a span per method call. Workers automatically traces native Workers RPC calls, but calls using Cap'n Web's HTTP batch or WebSocket transports only have the surrounding runtime spans unless the application instruments every method itself.
Workers now exposes a custom spans API that libraries can use for this:
https://developers.cloudflare.com/workers/observability/traces/custom-spans/
What users have to do today
After enabling observability.traces.enabled, every RPC method that should appear in a trace must be wrapped manually:
import { tracing } from "cloudflare:workers";
import { RpcTarget } from "capnweb";
class Api extends RpcTarget {
getUser(id: string) {
return tracing.enterSpan("capnweb.rpc.getUser", async (span) => {
span.setAttribute("user.id", id);
return loadUser(id);
});
}
}
This is repetitive, easy to omit, and requires application code to know about a runtime-specific API. It also makes consistent span naming and attributes difficult across Cap'n Web applications.
Desired experience
Ideally, users enable Workers tracing and write no Cap'n Web-specific tracing code:
import { RpcTarget } from "capnweb";
class Api extends RpcTarget {
getUser(id: string) {
return loadUser(id);
}
}
Cap'n Web could automatically create a server-side custom span around each incoming method call or property operation. The span should remain active until an asynchronous result settles so nested runtime operations such as fetch(), KV, D1, and user-created spans become children. Errors should still end the span correctly.
The behavior should degrade to a no-op on runtimes that do not expose the Workers tracing API, preserving Cap'n Web's cross-runtime support. The Agents SDK's Cloudflare tracing adapter is a useful example: it imports the cloudflare:workers namespace, detects tracing.startActiveSpan, and falls back to a no-op runtime without changing application behavior:
https://github.com/cloudflare/agents/blob/c076e4c9ff6cfb72931085226edfd3ee7965ac48/packages/agents/src/observability/tracing/cloudflare.ts
Design questions
- Should automatic spans be enabled by default whenever Workers tracing is enabled, or exposed through a session option?
- What span name and attributes should identify the method/property path and transport?
- How should spans be bounded for long-lived WebSocket sessions, where calls arrive after the upgrade handler has returned?
- Can the implementation instrument the common local dispatch path so HTTP batch and WebSocket transports behave consistently?
Problem
Cap'n Web RPC calls do not currently produce a span per method call. Workers automatically traces native Workers RPC calls, but calls using Cap'n Web's HTTP batch or WebSocket transports only have the surrounding runtime spans unless the application instruments every method itself.
Workers now exposes a custom spans API that libraries can use for this:
https://developers.cloudflare.com/workers/observability/traces/custom-spans/
What users have to do today
After enabling
observability.traces.enabled, every RPC method that should appear in a trace must be wrapped manually:This is repetitive, easy to omit, and requires application code to know about a runtime-specific API. It also makes consistent span naming and attributes difficult across Cap'n Web applications.
Desired experience
Ideally, users enable Workers tracing and write no Cap'n Web-specific tracing code:
Cap'n Web could automatically create a server-side custom span around each incoming method call or property operation. The span should remain active until an asynchronous result settles so nested runtime operations such as
fetch(), KV, D1, and user-created spans become children. Errors should still end the span correctly.The behavior should degrade to a no-op on runtimes that do not expose the Workers tracing API, preserving Cap'n Web's cross-runtime support. The Agents SDK's Cloudflare tracing adapter is a useful example: it imports the
cloudflare:workersnamespace, detectstracing.startActiveSpan, and falls back to a no-op runtime without changing application behavior:https://github.com/cloudflare/agents/blob/c076e4c9ff6cfb72931085226edfd3ee7965ac48/packages/agents/src/observability/tracing/cloudflare.ts
Design questions