Defer provider metric attributes until routing resolves

- Allow withMetrics attributes to be computed lazily
- Record sendTurn metrics with the resolved provider
This commit is contained in:
Julius Marminge committed 2026-04-02 19:19:19 -07:00
1 parent 5fada58185
commit 8cc42335ed
4 files changed
+94 -9

No files matched your search

@@ -70,4 +70,42 @@ describe("withMetrics", () => {
);
}),
);
it.effect("evaluates attributes lazily after the wrapped effect runs", () =>
Effect.gen(function* () {
const counter = Metric.counter("with_metrics_lazy_total");
const timer = Metric.timer("with_metrics_lazy_duration");
let provider = "unknown";
yield* Effect.sync(() => {
provider = "codex";
}).pipe(
withMetrics({
counter,
timer,
attributes: () => ({
provider,
operation: "lazy",
}),
}),
);
const snapshots = yield* Metric.snapshot;
assert.equal(
hasMetricSnapshot(snapshots, "with_metrics_lazy_total", {
provider: "codex",
operation: "lazy",
outcome: "success",
}),
true,
);
assert.equal(
hasMetricSnapshot(snapshots, "with_metrics_lazy_duration", {
provider: "codex",
operation: "lazy",
}),
true,
);
}),
);
});
+5 -2
View File
@@ -91,7 +91,9 @@ export const increment = (
export interface WithMetricsOptions {
readonly counter?: Metric.Metric<number, unknown>;
readonly timer?: Metric.Metric<Duration.Duration, unknown>;
readonly attributes?: Readonly<Record<string, unknown>>;
readonly attributes?:
| Readonly<Record<string, unknown>>
| (() => Readonly<Record<string, unknown>>);
readonly outcomeAttributes?: (
outcome: ReturnType<typeof outcomeFromExit>,
) => Readonly<Record<string, unknown>>;
@@ -105,7 +107,8 @@ const withMetricsImpl = <A, E, R>(
const startedAt = Date.now();
const exit = yield* Effect.exit(effect);
const duration = Duration.millis(Math.max(0, Date.now() - startedAt));
const baseAttributes = options.attributes ?? {};
const baseAttributes =
typeof options.attributes === "function" ? options.attributes() : (options.attributes ?? {});
if (options.timer) {
yield* Metric.update(
@@ -1146,6 +1146,45 @@ fanout.layer("ProviderServiceLive fanout", (it) => {
);
}),
);
it.effect(
"records sendTurn metrics with the resolved provider when modelSelection is omitted",
() =>
Effect.gen(function* () {
const provider = yield* ProviderService;
const session = yield* provider.startSession(asThreadId("thread-send-metrics"), {
provider: "claudeAgent",
threadId: asThreadId("thread-send-metrics"),
cwd: "/tmp/project-send-metrics",
runtimeMode: "full-access",
});
yield* provider.sendTurn({
threadId: session.threadId,
input: "hello",
attachments: [],
});
const snapshots = yield* Metric.snapshot;
assert.equal(
hasMetricSnapshot(snapshots, "t3_provider_turns_total", {
provider: "claudeAgent",
operation: "send",
outcome: "success",
}),
true,
);
assert.equal(
hasMetricSnapshot(snapshots, "t3_provider_turn_duration", {
provider: "claudeAgent",
operation: "send",
}),
true,
);
}),
);
});
const validation = makeProviderServiceLayer();
@@ -407,12 +407,16 @@ const makeProviderService = Effect.fn("makeProviderService")(function* (
"provider.interaction_mode": input.interactionMode,
"provider.attachment_count": input.attachments.length,
});
let metricProvider = "unknown";
let metricModel = input.modelSelection?.model;
return yield* Effect.gen(function* () {
const routed = yield* resolveRoutableSession({
threadId: input.threadId,
operation: "ProviderService.sendTurn",
allowRecovery: true,
});
metricProvider = routed.adapter.provider;
metricModel = input.modelSelection?.model;
yield* Effect.annotateCurrentSpan({
"provider.kind": routed.adapter.provider,
...(input.modelSelection?.model ? { "provider.model": input.modelSelection.model } : {}),
@@ -442,13 +446,14 @@ const makeProviderService = Effect.fn("makeProviderService")(function* (
withMetrics({
counter: providerTurnsTotal,
timer: providerTurnDuration,
attributes: providerTurnMetricAttributes({
provider: input.modelSelection?.provider ?? "unknown",
model: input.modelSelection?.model,
extra: {
operation: "send",
},
}),
attributes: () =>
providerTurnMetricAttributes({
provider: metricProvider,
model: metricModel,
extra: {
operation: "send",
},
}),
}),
);
});