Skip to content

Commit 0ac1d4d

Browse files
committed
fix: use lazy attributes in sendTurn metrics to capture resolved provider
The sendTurn method eagerly evaluated input.modelSelection?.provider for metric attributes before the effect ran, recording the requested provider instead of the actual routed adapter provider. When modelSelection was omitted, metrics incorrectly recorded provider as 'unknown'. This changes sendTurn to use the same mutable variable pattern as all other provider operations (interruptTurn, respondToRequest, etc.) and extends WithMetricsOptions.attributes to accept a lazy callback so both the counter and timer capture the resolved provider after routing.
1 parent 5fada58 commit 0ac1d4d

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

apps/server/src/observability/Metrics.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ export const increment = (
9191
export interface WithMetricsOptions {
9292
readonly counter?: Metric.Metric<number, unknown>;
9393
readonly timer?: Metric.Metric<Duration.Duration, unknown>;
94-
readonly attributes?: Readonly<Record<string, unknown>>;
94+
readonly attributes?:
95+
| Readonly<Record<string, unknown>>
96+
| (() => Readonly<Record<string, unknown>>);
9597
readonly outcomeAttributes?: (
9698
outcome: ReturnType<typeof outcomeFromExit>,
9799
) => Readonly<Record<string, unknown>>;
@@ -105,7 +107,8 @@ const withMetricsImpl = <A, E, R>(
105107
const startedAt = Date.now();
106108
const exit = yield* Effect.exit(effect);
107109
const duration = Duration.millis(Math.max(0, Date.now() - startedAt));
108-
const baseAttributes = options.attributes ?? {};
110+
const baseAttributes =
111+
typeof options.attributes === "function" ? options.attributes() : (options.attributes ?? {});
109112

110113
if (options.timer) {
111114
yield* Metric.update(

apps/server/src/provider/Layers/ProviderService.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -407,12 +407,16 @@ const makeProviderService = Effect.fn("makeProviderService")(function* (
407407
"provider.interaction_mode": input.interactionMode,
408408
"provider.attachment_count": input.attachments.length,
409409
});
410+
let metricProvider = "unknown";
411+
let metricModel = input.modelSelection?.model;
410412
return yield* Effect.gen(function* () {
411413
const routed = yield* resolveRoutableSession({
412414
threadId: input.threadId,
413415
operation: "ProviderService.sendTurn",
414416
allowRecovery: true,
415417
});
418+
metricProvider = routed.adapter.provider;
419+
metricModel = input.modelSelection?.model;
416420
yield* Effect.annotateCurrentSpan({
417421
"provider.kind": routed.adapter.provider,
418422
...(input.modelSelection?.model ? { "provider.model": input.modelSelection.model } : {}),
@@ -442,13 +446,14 @@ const makeProviderService = Effect.fn("makeProviderService")(function* (
442446
withMetrics({
443447
counter: providerTurnsTotal,
444448
timer: providerTurnDuration,
445-
attributes: providerTurnMetricAttributes({
446-
provider: input.modelSelection?.provider ?? "unknown",
447-
model: input.modelSelection?.model,
448-
extra: {
449-
operation: "send",
450-
},
451-
}),
449+
attributes: () =>
450+
providerTurnMetricAttributes({
451+
provider: metricProvider,
452+
model: metricModel,
453+
extra: {
454+
operation: "send",
455+
},
456+
}),
452457
}),
453458
);
454459
});

0 commit comments

Comments
 (0)