TypeScript
fix(server): orchestrator records command count and duration again (#23966)
Yordis Prieto1646a471409d
diff --git a/apps/server/src/observability/Metrics.ts b/apps/server/src/observability/Metrics.tsindex be92b47a5bd..a16a364cc4b 100644--- a/apps/server/src/observability/Metrics.ts+++ b/apps/server/src/observability/Metrics.ts@@ -15,6 +15,14 @@ export const rpcRequestDuration = Metric.timer("t3_rpc_request_duration", {description: "RPC request handling duration.",});+export const orchestrationCommandsTotal = Metric.counter("t3_orchestration_commands_total", {+ description: "Total orchestration commands dispatched, by outcome.",+});++export const orchestrationCommandDuration = Metric.timer("t3_orchestration_command_duration", {+ description: "Orchestration command handling duration, excluding thread dispatch lock wait.",+});+export const orchestrationEffectClaimsTotal = Metric.counter("t3_orchestration_effect_claims_total",{@@ -186,3 +194,14 @@ export const withMetrics: {): <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;<A, E, R>(effect: Effect.Effect<A, E, R>, options: WithMetricsOptions): Effect.Effect<A, E, R>;} = dual(2, withMetricsImpl);++// Shaped as an `Effect.fn` combinator: the command is the instrumented function's argument.+export const withCommandMetrics = <A, E, R>(+ effect: Effect.Effect<A, E, R>,+ command: { readonly type: string },+): Effect.Effect<A, E, R> =>+ withMetrics(effect, {+ counter: orchestrationCommandsTotal,+ timer: orchestrationCommandDuration,+ attributes: { commandType: command.type },+ });diff --git a/apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts b/apps/server/src/orchestration-v2/Orchestrator.control-reads.test.tsindex 3b1a8ec1cec..c36dfc365af 100644--- a/apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts+++ b/apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts@@ -21,10 +21,15 @@ import {import * as DateTime from "effect/DateTime";import * as Effect from "effect/Effect";import * as Layer from "effect/Layer";+import * as Metric from "effect/Metric";import * as Result from "effect/Result";import * as Stream from "effect/Stream";import * as TestClock from "effect/testing/TestClock";import * as SqlClient from "effect/sql/SqlClient";+import {+ orchestrationCommandDuration,+ orchestrationCommandsTotal,+} from "../observability/Metrics.ts";import * as SqlitePersistence from "../persistence/Sqlite.ts";import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts";import * as DelegatedTaskQuestionReactor from "./DelegatedTaskQuestionReactor.ts";@@ -1368,3 +1373,38 @@ it.effect("explicit settle clears snooze atomically and stays idempotent after rassert.deepInclude((yield* projections.getThreadProjection(threadId)).thread, active);}).pipe(Effect.provide(layerTest)),);++it.effect("dispatch records the command outcome and its duration", () =>+ Effect.gen(function* () {+ const orchestrator = yield* Orchestrator.OrchestratorV2;+ const successes = Metric.value(+ Metric.withAttributes(orchestrationCommandsTotal, {+ commandType: "thread.create",+ outcome: "success",+ }),+ );+ const durations = Metric.value(+ Metric.withAttributes(orchestrationCommandDuration, { commandType: "thread.create" }),+ );+ const successesBefore = (yield* successes).count;+ const durationsBefore = (yield* durations).count;++ yield* orchestrator.dispatch({+ type: "thread.create",+ commandId: CommandId.make("create-dispatch-metrics"),+ threadId: ThreadId.make("thread:dispatch-metrics"),+ projectId: ProjectId.make("project:dispatch-metrics"),+ title: "Dispatch metrics",+ modelSelection,+ runtimeMode: "full-access",+ interactionMode: "default",+ branch: null,+ worktreePath: null,+ createdBy: "user",+ creationSource: "web",+ });++ assert.strictEqual((yield* successes).count - successesBefore, 1);+ assert.strictEqual((yield* durations).count - durationsBefore, 1);+ }).pipe(Effect.provide(layerTest)),+);diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.tsindex 0670eb23e45..aae5d1b68db 100644--- a/apps/server/src/orchestration-v2/Orchestrator.ts+++ b/apps/server/src/orchestration-v2/Orchestrator.ts@@ -118,6 +118,7 @@ import {type GuestShareCommand,} from "./GuestShareDecider.ts";import * as EffectOutbox from "./EffectOutbox.ts";+import { withCommandMetrics } from "../observability/Metrics.ts";import type { OrchestrationEffectRequestV2, PendingOrchestrationEffectV2 } from "./EffectOutbox.ts";import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";import * as ThreadCommandExecutor from "./ThreadCommandExecutor.ts";@@ -12116,7 +12117,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functiostoredEvents: committed.storedEvents,...(committed.committed ? {} : { replayed: true as const }),} satisfies OrchestratorV2DispatchResult;- });+ }, withCommandMetrics);// dispatchWithReceiptEffect owns the dispatch span; this wrapper selects the thread locks.const dispatchWithReceipt = Effect.fnUntraced(function* (command: OrchestrationV2ServerCommand) {diff --git a/apps/server/src/project/ProjectService.test.ts b/apps/server/src/project/ProjectService.test.tsindex 9347acef12c..5ea7871e94e 100644--- a/apps/server/src/project/ProjectService.test.ts+++ b/apps/server/src/project/ProjectService.test.ts@@ -5,12 +5,17 @@ import * as Deferred from "effect/Deferred";import * as Effect from "effect/Effect";import * as Fiber from "effect/Fiber";import * as Layer from "effect/Layer";+import * as Metric from "effect/Metric";import * as Option from "effect/Option";import * as Ref from "effect/Ref";import { TestClock } from "effect/testing";import * as SqlClient from "effect/sql/SqlClient";import * as ServerConfig from "../config.ts";+import {+ orchestrationCommandDuration,+ orchestrationCommandsTotal,+} from "../observability/Metrics.ts";import * as EventSink from "../orchestration-v2/EventSink.ts";import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";import * as LegacyV1ThreadImporter from "../orchestration-v2/legacy/LegacyV1ThreadImporter.ts";@@ -203,6 +208,33 @@ it.layer(layerTest)("ProjectService", (it) => {}),);+ it.effect("records the outcome and duration of a committed project command", () =>+ Effect.gen(function* () {+ const service = yield* ProjectService.ProjectService;+ const successes = Metric.value(+ Metric.withAttributes(orchestrationCommandsTotal, {+ commandType: "project.create",+ outcome: "success",+ }),+ );+ const durations = Metric.value(+ Metric.withAttributes(orchestrationCommandDuration, { commandType: "project.create" }),+ );+ const successesBefore = (yield* successes).count;+ const durationsBefore = (yield* durations).count;++ yield* service.create({+ commandId: CommandId.make("command:metrics:create"),+ projectId: ProjectId.make("project:metrics"),+ title: "Metrics",+ workspaceRoot: "/work/metrics",+ });++ assert.strictEqual((yield* successes).count - successesBefore, 1);+ assert.strictEqual((yield* durations).count - durationsBefore, 1);+ }),+ );+it.effect("auto-bootstraps a workspace exactly once", () =>Effect.gen(function* () {const service = yield* ProjectService.ProjectService;diff --git a/apps/server/src/project/ProjectService.ts b/apps/server/src/project/ProjectService.tsindex b2c65dd17ae..5c646e1ea7b 100644--- a/apps/server/src/project/ProjectService.ts+++ b/apps/server/src/project/ProjectService.ts@@ -17,6 +17,7 @@ import * as Option from "effect/Option";import * as Result from "effect/Result";import * as Schema from "effect/Schema";+import { withCommandMetrics } from "../observability/Metrics.ts";import * as EventSink from "../orchestration-v2/EventSink.ts";import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";import * as LegacyV1ThreadImporter from "../orchestration-v2/legacy/LegacyV1ThreadImporter.ts";@@ -283,7 +284,7 @@ export const make = Effect.gen(function* () {rejection ?? receipt.error ?? "The command was previously rejected.",);}- });+ }, withCommandMetrics);const readCommitted = Effect.fn("ProjectService.readCommitted")(function* (projectId: ProjectId) {const row = yield* readRow(projectId, { includeDeleted: true });diff --git a/docs/operations/observability.md b/docs/operations/observability.mdindex a9d03758ebf..7ed00910e3a 100644--- a/docs/operations/observability.md+++ b/docs/operations/observability.md@@ -393,12 +393,14 @@ Traces are best for one request. Metrics are best for trends.Good metric families to watch:- `t3_rpc_request_duration`+- `t3_orchestration_command_duration`- `t3_provider_turn_duration` (how long the provider adapter takes to start a turn, not the turn's run time)- `t3_git_command_duration`Counters tell you volume and failure rate:- `t3_rpc_requests_total`+- `t3_orchestration_commands_total`- `t3_provider_turns_total`- `t3_git_commands_total`