Skip to content

Commit 0abcc09

Browse files
fix(relay): export tunnel cleanup counters to Axiom
The cron job ran without the Axiom tracer, and the reaper only logged its counters to the Worker console. The dry-run rollout step has no durable place to read them. Provide the relay tracer to the cron job and record each sweep's counters as attributes on its span. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 9c524d5 commit 0abcc09

3 files changed

Lines changed: 69 additions & 2 deletions

File tree

‎infra/relay/src/environments/ManagedEndpointReaper.test.ts‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as Effect from "effect/Effect";
44
import * as Layer from "effect/Layer";
55
import * as Redacted from "effect/Redacted";
66
import * as TestClock from "effect/testing/TestClock";
7+
import * as Tracer from "effect/Tracer";
78

89
import * as RelayConfiguration from "../Config.ts";
910
import * as ManagedEndpointAllocations from "./ManagedEndpointAllocations.ts";
@@ -574,6 +575,54 @@ describe("ManagedEndpointReaper", () => {
574575
}).pipe(Effect.provide(state.layer));
575576
});
576577

578+
it.effect("records dry-run counters on the sweep span", () => {
579+
const spans: Array<Tracer.NativeSpan> = [];
580+
const tracer = Tracer.make({
581+
span: (options) => {
582+
const span = new Tracer.NativeSpan(options);
583+
spans.push(span);
584+
return span;
585+
},
586+
});
587+
const expired = [
588+
tunnel({
589+
id: "recoverable",
590+
suffix: "aaaaaaaaaaaaaaaa",
591+
status: "down",
592+
timestamp: "2026-08-25T11:00:00.000Z",
593+
}),
594+
tunnel({
595+
id: "legacy",
596+
suffix: "bbbbbbbbbbbbbbbb",
597+
status: "down",
598+
timestamp: "2026-08-25T11:00:00.000Z",
599+
}),
600+
];
601+
const state = harness({
602+
cleanupMode: "dry-run",
603+
tunnels: expired,
604+
allocations: [
605+
allocation({ tunnelId: "recoverable", recoveryEnabled: true }),
606+
allocation({ tunnelId: "legacy", recoveryEnabled: false }),
607+
],
608+
});
609+
610+
return Effect.gen(function* () {
611+
yield* TestClock.setTime(NOW_MILLIS);
612+
const reaper = yield* ManagedEndpointReaper.ManagedEndpointReaper;
613+
yield* reaper.sweep;
614+
const sweepSpan = spans.find((span) => span.name === "relay.managed_endpoint_reaper.sweep");
615+
expect(Object.fromEntries(sweepSpan?.attributes ?? [])).toMatchObject({
616+
"relay.managed_endpoint_reaper.mode": "dry-run",
617+
"relay.managed_endpoint_reaper.scanned": 2,
618+
"relay.managed_endpoint_reaper.wouldDelete": 1,
619+
"relay.managed_endpoint_reaper.skippedLegacy": 1,
620+
"relay.managed_endpoint_reaper.deleted": 0,
621+
});
622+
expect(state.deleted).toEqual([]);
623+
}).pipe(Effect.provide(state.layer), Effect.withTracer(tracer));
624+
});
625+
577626
it.effect("does no Cloudflare work while cleanup is off", () => {
578627
const state = harness({
579628
cleanupMode: "off",

‎infra/relay/src/environments/ManagedEndpointReaper.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,20 @@ export const make = Effect.gen(function* () {
287287
failed,
288288
truncated,
289289
};
290-
}).pipe(Effect.withSpan("relay.managed_endpoint_reaper.sweep"));
290+
}).pipe(
291+
// Dry-run rollout reads these counters from the exported span.
292+
Effect.tap((result) =>
293+
Effect.annotateCurrentSpan(
294+
Object.fromEntries(
295+
Object.entries(result).map(([key, value]) => [
296+
`relay.managed_endpoint_reaper.${key}`,
297+
value,
298+
]),
299+
),
300+
),
301+
),
302+
Effect.withSpan("relay.managed_endpoint_reaper.sweep"),
303+
);
291304

292305
return ManagedEndpointReaper.of({ sweep });
293306
});

‎infra/relay/src/worker.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,12 @@ export const ApiLive = Api.make(
358358
),
359359
],
360360
{ concurrency: 2, discard: true },
361-
).pipe(Effect.withSpan("relay.cron.prune_expired_state"), Effect.provide(runtimeLayer)),
361+
).pipe(
362+
Effect.withSpan("relay.cron.prune_expired_state"),
363+
Effect.provide(runtimeLayer),
364+
// Export cron spans to Axiom like HTTP spans; the scope flushes them before the run ends.
365+
Effect.provide(relayTraceLayer),
366+
),
362367
);
363368

364369
const fetch = Layer.merge(

0 commit comments

Comments
 (0)