Skip to content

Commit 1646a47

Browse files
yordisjuliusmarmingeclaude
authored
fix(server): orchestrator records command count and duration again (#23966)
Signed-off-by: Yordis Prieto <[email protected]> Co-authored-by: Julius Marminge <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent abd9d9f commit 1646a47

6 files changed

Lines changed: 97 additions & 2 deletions

File tree

‎apps/server/src/observability/Metrics.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ export const rpcRequestDuration = Metric.timer("t3_rpc_request_duration", {
1515
description: "RPC request handling duration.",
1616
});
1717

18+
export const orchestrationCommandsTotal = Metric.counter("t3_orchestration_commands_total", {
19+
description: "Total orchestration commands dispatched, by outcome.",
20+
});
21+
22+
export const orchestrationCommandDuration = Metric.timer("t3_orchestration_command_duration", {
23+
description: "Orchestration command handling duration, excluding thread dispatch lock wait.",
24+
});
25+
1826
export const orchestrationEffectClaimsTotal = Metric.counter(
1927
"t3_orchestration_effect_claims_total",
2028
{
@@ -186,3 +194,14 @@ export const withMetrics: {
186194
): <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
187195
<A, E, R>(effect: Effect.Effect<A, E, R>, options: WithMetricsOptions): Effect.Effect<A, E, R>;
188196
} = dual(2, withMetricsImpl);
197+
198+
// Shaped as an `Effect.fn` combinator: the command is the instrumented function's argument.
199+
export const withCommandMetrics = <A, E, R>(
200+
effect: Effect.Effect<A, E, R>,
201+
command: { readonly type: string },
202+
): Effect.Effect<A, E, R> =>
203+
withMetrics(effect, {
204+
counter: orchestrationCommandsTotal,
205+
timer: orchestrationCommandDuration,
206+
attributes: { commandType: command.type },
207+
});

‎apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,15 @@ import {
2121
import * as DateTime from "effect/DateTime";
2222
import * as Effect from "effect/Effect";
2323
import * as Layer from "effect/Layer";
24+
import * as Metric from "effect/Metric";
2425
import * as Result from "effect/Result";
2526
import * as Stream from "effect/Stream";
2627
import * as TestClock from "effect/testing/TestClock";
2728
import * as SqlClient from "effect/sql/SqlClient";
29+
import {
30+
orchestrationCommandDuration,
31+
orchestrationCommandsTotal,
32+
} from "../observability/Metrics.ts";
2833
import * as SqlitePersistence from "../persistence/Sqlite.ts";
2934
import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts";
3035
import * as DelegatedTaskQuestionReactor from "./DelegatedTaskQuestionReactor.ts";
@@ -1368,3 +1373,38 @@ it.effect("explicit settle clears snooze atomically and stays idempotent after r
13681373
assert.deepInclude((yield* projections.getThreadProjection(threadId)).thread, active);
13691374
}).pipe(Effect.provide(layerTest)),
13701375
);
1376+
1377+
it.effect("dispatch records the command outcome and its duration", () =>
1378+
Effect.gen(function* () {
1379+
const orchestrator = yield* Orchestrator.OrchestratorV2;
1380+
const successes = Metric.value(
1381+
Metric.withAttributes(orchestrationCommandsTotal, {
1382+
commandType: "thread.create",
1383+
outcome: "success",
1384+
}),
1385+
);
1386+
const durations = Metric.value(
1387+
Metric.withAttributes(orchestrationCommandDuration, { commandType: "thread.create" }),
1388+
);
1389+
const successesBefore = (yield* successes).count;
1390+
const durationsBefore = (yield* durations).count;
1391+
1392+
yield* orchestrator.dispatch({
1393+
type: "thread.create",
1394+
commandId: CommandId.make("create-dispatch-metrics"),
1395+
threadId: ThreadId.make("thread:dispatch-metrics"),
1396+
projectId: ProjectId.make("project:dispatch-metrics"),
1397+
title: "Dispatch metrics",
1398+
modelSelection,
1399+
runtimeMode: "full-access",
1400+
interactionMode: "default",
1401+
branch: null,
1402+
worktreePath: null,
1403+
createdBy: "user",
1404+
creationSource: "web",
1405+
});
1406+
1407+
assert.strictEqual((yield* successes).count - successesBefore, 1);
1408+
assert.strictEqual((yield* durations).count - durationsBefore, 1);
1409+
}).pipe(Effect.provide(layerTest)),
1410+
);

‎apps/server/src/orchestration-v2/Orchestrator.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ import {
118118
type GuestShareCommand,
119119
} from "./GuestShareDecider.ts";
120120
import * as EffectOutbox from "./EffectOutbox.ts";
121+
import { withCommandMetrics } from "../observability/Metrics.ts";
121122
import type { OrchestrationEffectRequestV2, PendingOrchestrationEffectV2 } from "./EffectOutbox.ts";
122123
import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
123124
import * as ThreadCommandExecutor from "./ThreadCommandExecutor.ts";
@@ -12116,7 +12117,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1211612117
storedEvents: committed.storedEvents,
1211712118
...(committed.committed ? {} : { replayed: true as const }),
1211812119
} satisfies OrchestratorV2DispatchResult;
12119-
});
12120+
}, withCommandMetrics);
1212012121

1212112122
// dispatchWithReceiptEffect owns the dispatch span; this wrapper selects the thread locks.
1212212123
const dispatchWithReceipt = Effect.fnUntraced(function* (command: OrchestrationV2ServerCommand) {

‎apps/server/src/project/ProjectService.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@ import * as Deferred from "effect/Deferred";
55
import * as Effect from "effect/Effect";
66
import * as Fiber from "effect/Fiber";
77
import * as Layer from "effect/Layer";
8+
import * as Metric from "effect/Metric";
89
import * as Option from "effect/Option";
910
import * as Ref from "effect/Ref";
1011
import { TestClock } from "effect/testing";
1112
import * as SqlClient from "effect/sql/SqlClient";
1213

1314
import * as ServerConfig from "../config.ts";
15+
import {
16+
orchestrationCommandDuration,
17+
orchestrationCommandsTotal,
18+
} from "../observability/Metrics.ts";
1419
import * as EventSink from "../orchestration-v2/EventSink.ts";
1520
import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
1621
import * as LegacyV1ThreadImporter from "../orchestration-v2/legacy/LegacyV1ThreadImporter.ts";
@@ -203,6 +208,33 @@ it.layer(layerTest)("ProjectService", (it) => {
203208
}),
204209
);
205210

211+
it.effect("records the outcome and duration of a committed project command", () =>
212+
Effect.gen(function* () {
213+
const service = yield* ProjectService.ProjectService;
214+
const successes = Metric.value(
215+
Metric.withAttributes(orchestrationCommandsTotal, {
216+
commandType: "project.create",
217+
outcome: "success",
218+
}),
219+
);
220+
const durations = Metric.value(
221+
Metric.withAttributes(orchestrationCommandDuration, { commandType: "project.create" }),
222+
);
223+
const successesBefore = (yield* successes).count;
224+
const durationsBefore = (yield* durations).count;
225+
226+
yield* service.create({
227+
commandId: CommandId.make("command:metrics:create"),
228+
projectId: ProjectId.make("project:metrics"),
229+
title: "Metrics",
230+
workspaceRoot: "/work/metrics",
231+
});
232+
233+
assert.strictEqual((yield* successes).count - successesBefore, 1);
234+
assert.strictEqual((yield* durations).count - durationsBefore, 1);
235+
}),
236+
);
237+
206238
it.effect("auto-bootstraps a workspace exactly once", () =>
207239
Effect.gen(function* () {
208240
const service = yield* ProjectService.ProjectService;

‎apps/server/src/project/ProjectService.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import * as Option from "effect/Option";
1717
import * as Result from "effect/Result";
1818
import * as Schema from "effect/Schema";
1919

20+
import { withCommandMetrics } from "../observability/Metrics.ts";
2021
import * as EventSink from "../orchestration-v2/EventSink.ts";
2122
import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
2223
import * as LegacyV1ThreadImporter from "../orchestration-v2/legacy/LegacyV1ThreadImporter.ts";
@@ -283,7 +284,7 @@ export const make = Effect.gen(function* () {
283284
rejection ?? receipt.error ?? "The command was previously rejected.",
284285
);
285286
}
286-
});
287+
}, withCommandMetrics);
287288

288289
const readCommitted = Effect.fn("ProjectService.readCommitted")(function* (projectId: ProjectId) {
289290
const row = yield* readRow(projectId, { includeDeleted: true });

‎docs/operations/observability.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,12 +393,14 @@ Traces are best for one request. Metrics are best for trends.
393393
Good metric families to watch:
394394

395395
- `t3_rpc_request_duration`
396+
- `t3_orchestration_command_duration`
396397
- `t3_provider_turn_duration` (how long the provider adapter takes to start a turn, not the turn's run time)
397398
- `t3_git_command_duration`
398399

399400
Counters tell you volume and failure rate:
400401

401402
- `t3_rpc_requests_total`
403+
- `t3_orchestration_commands_total`
402404
- `t3_provider_turns_total`
403405
- `t3_git_commands_total`
404406

0 commit comments

Comments
 (0)