Skip to content

Commit 49cba3b

Browse files
fix(relay): preserve hook operation span annotations
1 parent 9e9a307 commit 49cba3b

5 files changed

Lines changed: 61 additions & 8 deletions

File tree

‎infra/relay/src/hooks/HookForwarder.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,29 @@ describe("HookForwarder", () => {
700700
.find((span) => span.name === "relay.hooks.forward")
701701
?.attributes.has("relay.hook.upstream_outcome"),
702702
).toBe(false);
703+
704+
for (const [holdWebhooksWhileOffline, inboxCapacity, outcome] of [
705+
[false, Infinity, "environment_unavailable"],
706+
[true, Infinity, "held"],
707+
[true, 0, "inbox_full"],
708+
] as const) {
709+
spans.length = 0;
710+
yield* forward(
711+
makeHarness({
712+
links: [{ ...managedLink, holdWebhooksWhileOffline }],
713+
inboxCapacity,
714+
execute: (request) =>
715+
Effect.succeed(
716+
HttpClientResponse.fromWeb(request, new Response("", { status: 503 })),
717+
),
718+
}),
719+
);
720+
expect(
721+
spans
722+
.find((span) => span.name === "relay.hooks.forward")
723+
?.attributes.get("relay.hook.outcome"),
724+
).toBe(outcome);
725+
}
703726
}),
704727
);
705728

‎infra/relay/src/hooks/HookForwarder.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -356,10 +356,8 @@ const make = Effect.gen(function* () {
356356
headers: { ...forwardedHeaders(request.headers), [RELAY_HOOK_DELIVERY_HEADER]: proof },
357357
body: body.success,
358358
};
359-
const holdOrFail = Effect.fn("HookForwarder.holdOrFail")(function* (
360-
status: 503 | 504,
361-
error: string,
362-
) {
359+
// Keep offline outcomes on the enclosing relay.hooks.forward span.
360+
const holdOrFail = Effect.fnUntraced(function* (status: 503 | 504, error: string) {
363361
if (!endpoint.holdWhileOffline) {
364362
yield* outcome(error);
365363
return errorResponse(status, error);

‎infra/relay/src/hooks/HookInboxObject.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,9 @@ export const layer = HookInboxObject.make(
9494
if (current === null || current > at) yield* state.storage.setAlarm(at);
9595
});
9696

97+
// withInboxSpan supplies each method's root span; avoid a second wrapper span.
9798
return {
98-
hold: Effect.fn("HookInboxObject.hold")(
99+
hold: Effect.fnUntraced(
99100
function* (hook: HookInboxStore.HeldHook, baseUrl: string) {
100101
const dueAt = yield* run(HookInboxStore.hold(hook, baseUrl));
101102
yield* Effect.annotateCurrentSpan({ "relay.inbox.stored": dueAt !== null });
@@ -105,7 +106,7 @@ export const layer = HookInboxObject.make(
105106
},
106107
withInboxSpan("relay.inbox.hold", inboxId),
107108
),
108-
wake: Effect.fn("HookInboxObject.wake")(
109+
wake: Effect.fnUntraced(
109110
function* (baseUrl: string) {
110111
const pending = yield* run(HookInboxStore.wake(baseUrl));
111112
yield* Effect.annotateCurrentSpan({ "relay.inbox.pending": pending });
@@ -114,7 +115,7 @@ export const layer = HookInboxObject.make(
114115
},
115116
withInboxSpan("relay.inbox.wake", inboxId),
116117
),
117-
clear: Effect.fn("HookInboxObject.clear")(
118+
clear: Effect.fnUntraced(
118119
function* () {
119120
yield* run(HookInboxStore.clear);
120121
yield* state.storage.deleteAlarm();

‎infra/relay/src/hooks/HookInboxStore.test.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as Effect from "effect/Effect";
88
import * as Fiber from "effect/Fiber";
99
import * as SqlClient from "effect/sql/SqlClient";
1010
import * as TestClock from "effect/testing/TestClock";
11+
import * as Tracer from "effect/Tracer";
1112

1213
import * as HookInboxStore from "./HookInboxStore.ts";
1314

@@ -44,6 +45,35 @@ const withInbox = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
4445
);
4546

4647
describe("HookInboxStore", () => {
48+
it.effect.each(["delivered", "unreachable"] as const)(
49+
"keeps %s delivery results on the operation span",
50+
(outcome) =>
51+
withInbox(
52+
Effect.gen(function* () {
53+
yield* HookInboxStore.hold(yield* hook("traced"), BASE_URL);
54+
const spans: Array<Tracer.NativeSpan> = [];
55+
const tracer = Tracer.make({
56+
span(options) {
57+
const span = new Tracer.NativeSpan(options);
58+
spans.push(span);
59+
return span;
60+
},
61+
});
62+
yield* HookInboxStore.deliverDue(deliverer(() => outcome).send).pipe(
63+
Effect.withTracer(tracer),
64+
);
65+
const run = spans.find((span) => span.name === "HookInboxStore.deliverDue");
66+
expect(run?.attributes.get("relay.inbox.run_result")).toBe(
67+
outcome === "delivered" ? "drained" : "unreachable",
68+
);
69+
expect(run?.attributes.get("relay.inbox.sent")).toBe(1);
70+
expect(run?.attributes.get("relay.inbox.delivered")).toBe(
71+
outcome === "delivered" ? 1 : 0,
72+
);
73+
}),
74+
),
75+
);
76+
4777
it.effect.each(["delivered", "busy", "unreachable"] as const)(
4878
"stops a prefetched batch cleared during a %s send",
4979
(outcome) =>

‎infra/relay/src/hooks/HookInboxStore.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,8 @@ export const deliverDue = Effect.fn("HookInboxStore.deliverDue")(function* <R>(
278278
let longestWaitMs = 0;
279279
const busyHooks = new Set<string>();
280280

281-
const annotateRun = Effect.fn("HookInboxStore.annotateRun")(function* (result: string) {
281+
// Delivery results and counts belong to the enclosing deliverDue span.
282+
const annotateRun = Effect.fnUntraced(function* (result: string) {
282283
const backlog = yield* backlogSize;
283284
yield* Effect.annotateCurrentSpan({
284285
"relay.inbox.run_result": result,

0 commit comments

Comments
 (0)