Automatically trace Cap'n Web RPC calls with Workers custom spans

Abierto
#269 2 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 2 días

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
5/5
Tiempo estimado
Más de una semana
Aptitud para principiantes
35/100
Tipo de issue
Nueva funcionalidad
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
typescript
Área
api

Línea de trabajo

Start by tracing the common local dispatch path used by the HTTP batch and WebSocket transports, and review the Agents SDK Cloudflare tracing adapter at packages/agents/src/observability/tracing/cloudflare.ts. Determine how method and property calls can create Workers custom spans, remain active through asynchronous results, and fall back to a no-op runtime. Done means both transports are covered without application instrumentation and existing cross-runtime behavior is preserved.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

enhancement

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?
Lenguaje dominante
TypeScript
Estrellas
4k
Forks
151
Merge medio
16 h 46 min
PR fusionados (30 d)
2

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de cloudflare/capnweb

Todos los issues de cloudflare/capnweb

Issues similares

Más issues de TypeScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.