Skip to content

Commit 2b1021c

Browse files
authored
perf(codex): trace app server request latency (#28666)
1 parent 8760d06 commit 2b1021c

2 files changed

Lines changed: 113 additions & 10 deletions

File tree

‎packages/effect-codex-app-server/src/protocol.test.ts‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as Schema from "effect/Schema";
77
import * as Sink from "effect/Sink";
88
import * as Stdio from "effect/Stdio";
99
import * as Stream from "effect/Stream";
10+
import * as Tracer from "effect/Tracer";
1011
import * as TestClock from "effect/testing/TestClock";
1112

1213
import * as NodeServices from "@effect/platform-node/NodeServices";
@@ -186,6 +187,95 @@ it.layer(NodeServices.layer)("effect-codex-app-server protocol", (it) => {
186187
}),
187188
);
188189

190+
it.effect(
191+
"correlates concurrent request spans through responses, failures and interruption",
192+
() => {
193+
const spans: Array<Tracer.NativeSpan> = [];
194+
const tracer = Tracer.make({
195+
span: (options) => {
196+
const span = new Tracer.NativeSpan(options);
197+
spans.push(span);
198+
return span;
199+
},
200+
});
201+
return Effect.gen(function* () {
202+
const { stdio, input, output } = yield* makeInMemoryStdio();
203+
const transport = yield* CodexProtocol.makeCodexAppServerPatchedProtocol({ stdio });
204+
const methods = ["model/list", "account/read", "skills/list"];
205+
const requests = [];
206+
for (const method of methods) {
207+
requests.push(
208+
yield* transport.request(method, { secret: "private-payload" }).pipe(Effect.forkScoped),
209+
);
210+
yield* Queue.take(output);
211+
}
212+
yield* Queue.offer(
213+
input,
214+
encodeJsonl({ id: 2, error: { code: -32602, message: "Invalid params" } }),
215+
);
216+
yield* Fiber.await(requests[1]!);
217+
yield* Queue.offer(input, encodeJsonl({ id: 1, result: { secret: "private-result" } }));
218+
yield* Fiber.join(requests[0]!);
219+
yield* Fiber.interrupt(requests[2]!);
220+
// A late response for an interrupted request must not affect the next request.
221+
yield* Queue.offer(input, encodeJsonl({ id: 3, result: {} }));
222+
const next = yield* transport.request("config/read").pipe(Effect.forkScoped);
223+
yield* Queue.take(output);
224+
yield* Queue.offer(input, encodeJsonl({ id: 4, result: {} }));
225+
yield* Fiber.join(next);
226+
for (const [index, method] of [...methods, "config/read"].entries()) {
227+
const request = spans.find(
228+
(span) =>
229+
span.name === "CodexAppServerProtocol.request" &&
230+
span.attributes.get("rpc.request_id") === index + 1,
231+
);
232+
assert.isDefined(request);
233+
assert.equal(request!.attributes.get("rpc.method"), method);
234+
assert.equal(request!.attributes.get("rpc.system"), "jsonrpc");
235+
assert.equal(request!.status._tag, "Ended");
236+
if (request!.status._tag === "Ended") {
237+
assert.equal(
238+
request!.status.exit._tag,
239+
index === 1 || index === 2 ? "Failure" : "Success",
240+
);
241+
}
242+
for (const phase of ["enqueue", "awaitResponse"]) {
243+
const child: Tracer.NativeSpan | undefined = spans.find(
244+
(span) =>
245+
span.name === `CodexAppServerProtocol.request.${phase}` &&
246+
span.parent?._tag === "Some" &&
247+
span.parent.value.spanId === request!.spanId,
248+
);
249+
assert.isDefined(child);
250+
assert.equal(child!.traceId, request!.traceId);
251+
assert.equal(child!.attributes.get("rpc.method"), method);
252+
assert.equal(child!.attributes.get("rpc.request_id"), index + 1);
253+
assert.equal(child!.status._tag, "Ended");
254+
}
255+
}
256+
const failedSend = yield* transport
257+
.request("thread/start", { value: 1n })
258+
.pipe(Effect.exit);
259+
assert.equal(failedSend._tag, "Failure");
260+
const sendSpans = spans.filter((span) => span.attributes.get("rpc.request_id") === 5);
261+
assert.deepEqual(
262+
sendSpans.map((span) => span.name),
263+
["CodexAppServerProtocol.request", "CodexAppServerProtocol.request.enqueue"],
264+
);
265+
for (const span of sendSpans) {
266+
assert.equal(span.attributes.get("rpc.method"), "thread/start");
267+
assert.equal(span.status._tag, "Ended");
268+
if (span.status._tag === "Ended") assert.equal(span.status.exit._tag, "Failure");
269+
}
270+
for (const span of spans) {
271+
const attrs = JSON.stringify([...span.attributes]);
272+
assert.notInclude(attrs, "private-payload");
273+
assert.notInclude(attrs, "private-result");
274+
}
275+
}).pipe(Effect.withTracer(tracer));
276+
},
277+
);
278+
189279
it.effect.each([1, 7, 1024])(
190280
"preserves JSONL framing and UTF-8 across %i-byte input chunks",
191281
(chunkSize) =>

‎packages/effect-codex-app-server/src/protocol.ts‎

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -502,17 +502,30 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa
502502
nextRequestId,
503503
(current) => [current, current + 1] as const,
504504
);
505+
const attributes = {
506+
"rpc.system": "jsonrpc",
507+
"rpc.method": method,
508+
"rpc.request_id": requestId,
509+
};
510+
yield* Effect.annotateCurrentSpan(attributes);
505511
const deferred = yield* Deferred.make<unknown, CodexError.CodexAppServerError>();
506-
yield* Ref.update(pending, (current) =>
507-
new Map(current).set(String(requestId), { deferred, method }),
508-
);
509-
yield* offerOutgoing({
510-
id: requestId,
511-
method,
512-
...(payload !== undefined ? { params: payload } : {}),
513-
}).pipe(Effect.tapError(() => removePending(String(requestId))));
514-
return yield* Deferred.await(deferred).pipe(
515-
Effect.onInterrupt(() => removePending(String(requestId))),
512+
return yield* Effect.acquireUseRelease(
513+
Ref.update(pending, (current) =>
514+
new Map(current).set(String(requestId), { deferred, method }),
515+
),
516+
() =>
517+
Effect.gen(function* () {
518+
yield* offerOutgoing({
519+
id: requestId,
520+
method,
521+
...(payload !== undefined ? { params: payload } : {}),
522+
}).pipe(Effect.withSpan("CodexAppServerProtocol.request.enqueue", { attributes }));
523+
// Includes queue/stdin delivery, Codex work and response routing, not just T3 CPU.
524+
return yield* Deferred.await(deferred).pipe(
525+
Effect.withSpan("CodexAppServerProtocol.request.awaitResponse", { attributes }),
526+
);
527+
}),
528+
() => removePending(String(requestId)),
516529
);
517530
});
518531

0 commit comments

Comments
 (0)