Skip to content

Commit 38c9a93

Browse files
committed
fix(server): orchestrator records command count and duration again
Signed-off-by: Yordis Prieto <[email protected]>
1 parent a68e6e9 commit 38c9a93

5 files changed

Lines changed: 112 additions & 4 deletions

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ 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 result.",
20+
});
21+
22+
export const orchestrationCommandDuration = Metric.timer("t3_orchestration_command_duration", {
23+
description:
24+
"Orchestration command dispatch duration while holding its dispatch lock, excluding lock wait.",
25+
});
26+
1827
export const orchestrationEffectClaimsTotal = Metric.counter(
1928
"t3_orchestration_effect_claims_total",
2029
{

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ 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";
@@ -1367,3 +1368,40 @@ it.effect("explicit settle clears snooze atomically and stays idempotent after r
13671368
assert.deepInclude((yield* projections.getThreadProjection(threadId)).thread, active);
13681369
}).pipe(Effect.provide(layerTest)),
13691370
);
1371+
1372+
it.effect("records a command and its duration for a dispatched command", () =>
1373+
Effect.gen(function* () {
1374+
const orchestrator = yield* Orchestrator.OrchestratorV2;
1375+
yield* orchestrator.dispatch({
1376+
type: "thread.create",
1377+
commandId: CommandId.make("create-dispatch-metrics"),
1378+
threadId: ThreadId.make("thread:dispatch-metrics"),
1379+
projectId: ProjectId.make("project:dispatch-metrics"),
1380+
title: "Dispatch metrics",
1381+
modelSelection,
1382+
runtimeMode: "full-access",
1383+
interactionMode: "default",
1384+
branch: null,
1385+
worktreePath: null,
1386+
createdBy: "user",
1387+
creationSource: "web",
1388+
});
1389+
1390+
const snapshots = yield* Metric.snapshot;
1391+
assert.isTrue(
1392+
snapshots.some(
1393+
(snapshot) =>
1394+
snapshot.id === "t3_orchestration_commands_total" &&
1395+
snapshot.attributes?.commandType === "thread.create" &&
1396+
snapshot.attributes?.outcome === "success",
1397+
),
1398+
);
1399+
assert.isTrue(
1400+
snapshots.some(
1401+
(snapshot) =>
1402+
snapshot.id === "t3_orchestration_command_duration" &&
1403+
snapshot.attributes?.commandType === "thread.create",
1404+
),
1405+
);
1406+
}).pipe(Effect.provide(layerTest)),
1407+
);

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ import { ProviderAdapterRegistryV2 } from "./ProviderAdapterRegistry.ts";
126126
import * as ProviderContinuationRequests from "@t3tools/provider-core/server/ProviderContinuationRequests";
127127
import { makeProviderFailure } from "@t3tools/provider-core/server/failure";
128128
import * as RunExecutionService from "./RunExecutionService.ts";
129+
import {
130+
orchestrationCommandDuration,
131+
orchestrationCommandsTotal,
132+
withMetrics,
133+
} from "../observability/Metrics.ts";
129134
import { ProviderSessionManagerV2 } from "./ProviderSessionManager.ts";
130135
import { ProviderSwitchServiceV2 } from "./ProviderSwitchService.ts";
131136
import { isAutomaticCompletionRun, queuedRunsInDeliveryOrder } from "./QueuedRunOrder.ts";
@@ -11866,7 +11871,13 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1186611871
const dispatchWithReceipt = Effect.fnUntraced(function* (command: OrchestrationV2ServerCommand) {
1186711872
const dispatch = threadDispatch.withLock(
1186811873
commandThreadId(command),
11869-
dispatchWithReceiptEffect(command),
11874+
dispatchWithReceiptEffect(command).pipe(
11875+
withMetrics({
11876+
counter: orchestrationCommandsTotal,
11877+
timer: orchestrationCommandDuration,
11878+
attributes: { commandType: command.type },
11879+
}),
11880+
),
1187011881
);
1187111882
if (
1187211883
command.type !== "message.dispatch" ||

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ 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";
@@ -203,6 +204,36 @@ it.layer(layerTest)("ProjectService", (it) => {
203204
}),
204205
);
205206

207+
it.effect("records a command metric for a committed project command", () =>
208+
Effect.gen(function* () {
209+
const service = yield* ProjectService.ProjectService;
210+
yield* TestClock.setTime(Date.parse("2026-06-20T10:00:00.000Z"));
211+
yield* service.create({
212+
commandId: CommandId.make("command:metrics:create"),
213+
projectId: ProjectId.make("project:metrics"),
214+
title: "Metrics",
215+
workspaceRoot: "/work/metrics",
216+
});
217+
218+
const snapshots = yield* Metric.snapshot;
219+
assert.isTrue(
220+
snapshots.some(
221+
(snapshot) =>
222+
snapshot.id === "t3_orchestration_commands_total" &&
223+
snapshot.attributes?.commandType === "project.create" &&
224+
snapshot.attributes?.outcome === "success",
225+
),
226+
);
227+
assert.isTrue(
228+
snapshots.some(
229+
(snapshot) =>
230+
snapshot.id === "t3_orchestration_command_duration" &&
231+
snapshot.attributes?.commandType === "project.create",
232+
),
233+
);
234+
}),
235+
);
236+
206237
it.effect("auto-bootstraps a workspace exactly once", () =>
207238
Effect.gen(function* () {
208239
const service = yield* ProjectService.ProjectService;

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ import * as Option from "effect/Option";
1717
import * as Result from "effect/Result";
1818
import * as Schema from "effect/Schema";
1919

20+
import {
21+
orchestrationCommandDuration,
22+
orchestrationCommandsTotal,
23+
withMetrics,
24+
} from "../observability/Metrics.ts";
2025
import * as EventSink from "../orchestration-v2/EventSink.ts";
2126
import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
2227
import * as LegacyV1ThreadImporter from "../orchestration-v2/legacy/LegacyV1ThreadImporter.ts";
@@ -210,7 +215,7 @@ export const make = Effect.gen(function* () {
210215
),
211216
);
212217

213-
const commit = Effect.fn("ProjectService.commit")(function* (command: ProjectCommand) {
218+
const commitEffect = Effect.fn("ProjectService.commit")(function* (command: ProjectCommand) {
214219
const { projectId } = command;
215220
const dispatchError = (cause: unknown) =>
216221
new ProjectOperationError({ operation: "dispatch-project-command", projectId, cause });
@@ -253,12 +258,18 @@ export const make = Effect.gen(function* () {
253258
error: encodeProjectCommandRejection(planned.failure),
254259
});
255260
});
261+
const timedPlanAndCommit = planAndCommit.pipe(
262+
withMetrics({
263+
timer: orchestrationCommandDuration,
264+
attributes: { commandType: command.type },
265+
}),
266+
);
256267
const receipt = yield* projectLocks
257268
.withLock(
258269
projectId,
259270
workspaceRoot === undefined
260-
? planAndCommit
261-
: workspaceLocks.withLock(workspaceRoot, planAndCommit),
271+
? timedPlanAndCommit
272+
: workspaceLocks.withLock(workspaceRoot, timedPlanAndCommit),
262273
)
263274
.pipe(Effect.mapError(dispatchError));
264275
if (receipt.projectId !== projectId || receipt.commandType !== command.type) {
@@ -286,6 +297,14 @@ export const make = Effect.gen(function* () {
286297
}
287298
});
288299

300+
const commit = (command: ProjectCommand) =>
301+
commitEffect(command).pipe(
302+
withMetrics({
303+
counter: orchestrationCommandsTotal,
304+
attributes: { commandType: command.type },
305+
}),
306+
);
307+
289308
const readCommitted = Effect.fn("ProjectService.readCommitted")(function* (projectId: ProjectId) {
290309
const row = yield* readRow(projectId, { includeDeleted: true });
291310
if (Option.isNone(row)) {

0 commit comments

Comments
 (0)