@devries-cameron20 / t3code Mirror · read-only / commits / ab47250ec1
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 23, 00:30 UTC

feat(relay): record the Cloudflare edge each request entered through (#22080)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 19, 2026 at 19:27 UTC ab47250ec1e690d16f2349af622b0f5391503346
2 files changed +53 -2
infra/relay/src/http/Api.test.ts
+34
@@ -1196,6 +1196,40 @@ describe("relay request tracing", () => {
1196 }),
1197 );
1198
1199 + it.effect("records the Cloudflare edge a request entered through on its server span", () =>
1200 + Effect.gen(function* () {
1201 + const spans: Array<Tracer.NativeSpan> = [];
1202 + const tracer = Tracer.make({
1203 + span: (options) => {
1204 + const span = new Tracer.NativeSpan(options);
1205 + spans.push(span);
1206 + return span;
1207 + },
1208 + });
1209 + const source = new Request("https://relay.test/v1/environments/env-1/connect", {
1210 + method: "POST",
1211 + });
1212 + Object.defineProperty(source, "cf", {
1213 + value: { colo: "JNB", country: "ZA", continent: "AF", asn: 37_100 },
1214 + });
1215 +
1216 + yield* traceRelayHttpRequestWith(
1217 + Effect.succeed(HttpServerResponse.empty({ status: 204 })),
1218 + Layer.succeed(Tracer.Tracer, tracer),
1219 + ).pipe(
1220 + Effect.provideService(
1221 + HttpServerRequest.HttpServerRequest,
1222 + HttpServerRequest.fromWeb(source),
1223 + ),
1224 + );
1225 +
1226 + const server = spans.find((span) => span.kind === "server");
1227 + expect(server?.attributes.get("relay.edge.colo")).toBe("JNB");
1228 + expect(server?.attributes.get("relay.edge.country")).toBe("ZA");
1229 + expect(server?.attributes.get("relay.edge.continent")).toBe("AF");
1230 + }),
1231 + );
1232 +
1233 it.effect("records one server span inside the worker's disabled HTTP tracer", () =>
1234 Effect.gen(function* () {
1235 const spans: Array<Tracer.NativeSpan> = [];
infra/relay/src/http/Api.ts
+19 -2
@@ -207,6 +207,21 @@ const relayRequestDeadline = <E, R>(
207
208 const SENDER_TRACE_HEADER = /^(traceparent|tracestate|b3|x-b3-.*)$/i;
209
210 +// Where the request entered Cloudflare's network. The Worker runs near its
211 +// database, not at this edge, so these are the only way to split relay
212 +// latency by where users are.
213 +const annotateRelayEdgeLocation = Effect.gen(function* () {
214 + const request = yield* HttpServerRequest.HttpServerRequest;
215 + const cf: unknown = request.source instanceof Request ? Reflect.get(request.source, "cf") : null;
216 + if (typeof cf !== "object" || cf === null) return;
217 + const attributes: Record<string, string> = {};
218 + for (const key of ["colo", "country", "continent"] as const) {
219 + const value: unknown = Reflect.get(cf, key);
220 + if (typeof value === "string" && value) attributes[`relay.edge.${key}`] = value;
221 + }
222 + yield* Effect.annotateCurrentSpan(attributes);
223 +});
224 +
225 // The method-named server span is created below; this wrapper must not add another span.
226 export const traceRelayHttpRequest = Effect.fnUntraced(function* <E, R>(
227 httpEffect: Effect.Effect<
@@ -216,7 +231,8 @@ export const traceRelayHttpRequest = Effect.fnUntraced(function* <E, R>(
231 >,
232 ) {
233 const request = yield* HttpServerRequest.HttpServerRequest;
219 - const traced = appendRelayTraceContextResponseHeader.pipe(
234 + const traced = annotateRelayEdgeLocation.pipe(
235 + Effect.andThen(appendRelayTraceContextResponseHeader),
236 Effect.andThen(relayRequestDeadline(httpEffect)),
237 );
238 if (!isRelayHookPath(request.url)) {
@@ -238,7 +254,8 @@ export const traceRelayHttpRequest = Effect.fnUntraced(function* <E, R>(
254 ),
255 });
256 return yield* HttpMiddleware.tracer(
241 - appendRelayTraceContextResponseHeader.pipe(
257 + annotateRelayEdgeLocation.pipe(
258 + Effect.andThen(appendRelayTraceContextResponseHeader),
259 Effect.andThen(
260 relayRequestDeadline(
261 httpEffect.pipe(Effect.provideService(HttpServerRequest.HttpServerRequest, request)),