The best way to control Claude Code, Codex and more from any computer or phone
fix(server): each websocket RPC starts its own trace so Tempo keeps its spans (#36863)
Julius Marminge committed
Nov 14, 2026 at 10:40 UTC
5d1352d48a892ac4b5cdee511f43c179f78d6cc5
2 files changed
+62
-8
apps/server/src/observability/RpcInstrumentation.test.ts
new
+41
@@ -0,0 +1,41 @@
1
+import { assert, it } from "@effect/vitest";
2
+import { WS_METHODS } from "@t3tools/contracts";
3
+import * as Effect from "effect/Effect";
4
+import * as Option from "effect/Option";
5
+import type * as Tracer from "effect/Tracer";
6
+import * as Headers from "effect/http/Headers";
7
+import * as Rpc from "effect/rpc/Rpc";
8
+import { RequestId } from "effect/rpc/RpcMessage";
9
+
10
+import { RpcInstrumentation, rpcInstrumentationLayer } from "./RpcInstrumentation.ts";
11
+
12
+it.effect("starts each RPC in its own trace, linked to the connection span", () =>
13
+ Effect.gen(function* () {
14
+ const middleware = yield* RpcInstrumentation;
15
+ let rpcSpan: Tracer.Span | undefined;
16
+ const handler = Effect.gen(function* () {
17
+ rpcSpan = yield* Effect.currentSpan;
18
+ }).pipe(Effect.orDie, Effect.asVoid);
19
+
20
+ const connection = yield* Effect.gen(function* () {
21
+ const span = yield* Effect.currentSpan;
22
+ yield* middleware(handler as never, {
23
+ client: new Rpc.ServerClient(0),
24
+ requestId: RequestId(1),
25
+ rpc: Rpc.make(WS_METHODS.reviewGetDiffPreview),
26
+ payload: {},
27
+ headers: Headers.empty,
28
+ });
29
+ return span;
30
+ }).pipe(Effect.withSpan("GET /ws"), Effect.orDie);
31
+
32
+ assert.isDefined(rpcSpan);
33
+ assert.equal(rpcSpan!.name, "ws.rpc.review.getDiffPreview");
34
+ assert.isTrue(Option.isNone(rpcSpan!.parent));
35
+ assert.notEqual(rpcSpan!.traceId, connection.traceId);
36
+ assert.deepEqual(
37
+ rpcSpan!.links.map((link) => link.span.spanId),
38
+ [connection.spanId],
39
+ );
40
+ }).pipe(Effect.provide(rpcInstrumentationLayer)),
41
+);
apps/server/src/observability/RpcInstrumentation.ts
+21
-8
@@ -1,6 +1,7 @@
1
import { ORCHESTRATION_V2_WS_METHODS, WS_METHODS, type WsRpcGroup } from "@t3tools/contracts";
2
import * as Effect from "effect/Effect";
3
import * as Layer from "effect/Layer";
4
+import * as Option from "effect/Option";
5
import * as References from "effect/References";
6
import type * as RpcGroup from "effect/rpc/RpcGroup";
7
import * as RpcMiddleware from "effect/rpc/RpcMiddleware";
@@ -259,13 +260,25 @@ export const rpcInstrumentationLayer = Layer.succeed(RpcInstrumentation)((effect
260
if (RPC_METHODS_WITH_TRACING_DISABLED.has(method)) {
261
return measured.pipe(Effect.provideService(References.TracerEnabled, false));
262
}
262
- return measured.pipe(
263
- Effect.withSpan(`${RPC_SPAN_PREFIX}.${method}`, {
264
- attributes: {
265
- ...DEFAULT_RPC_SPAN_ATTRIBUTES,
266
- "rpc.method": method,
267
- "rpc.aggregate": RPC_AGGREGATES[method as WsRpcMethod],
268
- },
269
- }),
263
+ // The ambient span is the socket's `GET /ws` request, open for the whole connection. Parenting
264
+ // every call to it grows one trace past the collector's per-trace limit, which then drops it.
265
+ return Effect.currentParentSpan.pipe(
266
+ Effect.option,
267
+ Effect.flatMap((connection) =>
268
+ measured.pipe(
269
+ Effect.withSpan(`${RPC_SPAN_PREFIX}.${method}`, {
270
+ root: true,
271
+ links: Option.match(connection, {
272
+ onNone: () => [],
273
+ onSome: (span) => [{ span, attributes: {} }],
274
+ }),
275
+ attributes: {
276
+ ...DEFAULT_RPC_SPAN_ATTRIBUTES,
277
+ "rpc.method": method,
278
+ "rpc.aggregate": RPC_AGGREGATES[method as WsRpcMethod],
279
+ },
280
+ }),
281
+ ),
282
+ ),
283
);
284
});