Skip to content

Commit 1b1d50c

Browse files
committed
fix(server): meter checkpoint git commands so their dashboards stop reading empty
Signed-off-by: Yordis Prieto <[email protected]>
1 parent a68e6e9 commit 1b1d50c

2 files changed

Lines changed: 139 additions & 2 deletions

File tree

‎apps/server/src/vcs/GitVcsDriver.test.ts‎

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@ import * as Layer from "effect/Layer";
1212
import * as Option from "effect/Option";
1313
import * as Path from "effect/Path";
1414
import * as PlatformError from "effect/PlatformError";
15+
import { ChildProcessSpawner } from "effect/process";
16+
import * as Metric from "effect/Metric";
1517
import { assert, it } from "@effect/vitest";
1618

1719
import { CheckpointRef, GitCommandError, VcsProcessExitError } from "@t3tools/contracts";
1820
import * as HostProcess from "@t3tools/shared/HostProcess";
1921
import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
2022
import * as ServerConfig from "../config.ts";
23+
import { gitCommandDuration, gitCommandsTotal } from "../observability/Metrics.ts";
2124
import * as CheckpointStore from "../checkpointing/CheckpointStore.ts";
2225
import * as ProcessRunner from "../processRunner.ts";
2326
import * as GitVcsDriver from "./GitVcsDriver.ts";
@@ -1017,3 +1020,124 @@ it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
10171020
assert.strictEqual(detectionError._tag, "VcsProcessSpawnError");
10181021
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
10191022
);
1023+
1024+
const gitCommandCount = (operation: string, outcome: string) =>
1025+
Metric.value(
1026+
Metric.withAttributes(gitCommandsTotal, [
1027+
["operation", operation],
1028+
["outcome", outcome],
1029+
]),
1030+
).pipe(Effect.map((state) => state.count));
1031+
1032+
const gitCommandDurationCount = (operation: string) =>
1033+
Metric.value(Metric.withAttributes(gitCommandDuration, [["operation", operation]])).pipe(
1034+
Effect.map((state) => state.count),
1035+
);
1036+
1037+
const makeMeteredDriver = (exitCode: number, failure?: VcsProcessExitError) =>
1038+
GitVcsDriver.makeVcsDriverShape().pipe(
1039+
Effect.provideService(VcsProcess.VcsProcess, {
1040+
run: () =>
1041+
failure === undefined
1042+
? Effect.succeed({
1043+
exitCode: ChildProcessSpawner.ExitCode(exitCode),
1044+
stdout: "",
1045+
stderr: "",
1046+
stdoutTruncated: false,
1047+
stderrTruncated: false,
1048+
})
1049+
: Effect.fail(failure),
1050+
}),
1051+
);
1052+
1053+
it.effect("GitVcsDriver meters each git command once under its operation", () =>
1054+
Effect.gen(function* () {
1055+
const operation = "GitVcsDriver.checkpoints.test.metered";
1056+
const driver = yield* makeMeteredDriver(0);
1057+
1058+
yield* driver.execute({ operation, cwd: "/repo", args: ["status"] });
1059+
yield* driver.execute({ operation, cwd: "/repo", args: ["status"] });
1060+
1061+
assert.strictEqual(yield* gitCommandCount(operation, "success"), 2);
1062+
assert.strictEqual(yield* gitCommandCount(operation, "failure"), 0);
1063+
assert.strictEqual(yield* gitCommandDurationCount(operation), 2);
1064+
}).pipe(Effect.provide(NodeServices.layer)),
1065+
);
1066+
1067+
it.effect("GitVcsDriver counts an allowed non-zero exit as a success", () =>
1068+
Effect.gen(function* () {
1069+
const operation = "GitVcsDriver.checkpoints.test.allowedNonZero";
1070+
const driver = yield* makeMeteredDriver(1);
1071+
1072+
const result = yield* driver.execute({
1073+
operation,
1074+
cwd: "/repo",
1075+
args: ["diff", "--quiet"],
1076+
allowNonZeroExit: true,
1077+
});
1078+
1079+
assert.strictEqual(result.exitCode, 1);
1080+
assert.strictEqual(yield* gitCommandCount(operation, "success"), 1);
1081+
assert.strictEqual(yield* gitCommandCount(operation, "failure"), 0);
1082+
}).pipe(Effect.provide(NodeServices.layer)),
1083+
);
1084+
1085+
it.effect("GitVcsDriver counts a failed git command as a failure", () =>
1086+
Effect.gen(function* () {
1087+
const operation = "GitVcsDriver.checkpoints.test.failed";
1088+
const driver = yield* makeMeteredDriver(
1089+
128,
1090+
new VcsProcessExitError({
1091+
operation,
1092+
command: "git",
1093+
cwd: "/repo",
1094+
exitCode: 128,
1095+
detail: "fatal",
1096+
}),
1097+
);
1098+
1099+
const error = yield* driver
1100+
.execute({ operation, cwd: "/repo", args: ["rev-parse", "HEAD"] })
1101+
.pipe(Effect.flip);
1102+
1103+
assert.strictEqual(error._tag, "VcsProcessExitError");
1104+
assert.strictEqual(yield* gitCommandCount(operation, "failure"), 1);
1105+
assert.strictEqual(yield* gitCommandCount(operation, "success"), 0);
1106+
assert.strictEqual(yield* gitCommandDurationCount(operation), 1);
1107+
}).pipe(Effect.provide(NodeServices.layer)),
1108+
);
1109+
1110+
it.effect("checkpoint capture meters every git process it starts, including streamed ones", () =>
1111+
Effect.gen(function* () {
1112+
const fileSystem = yield* FileSystem.FileSystem;
1113+
const liveProcess = yield* VcsProcess.VcsProcess;
1114+
const operation = VcsProcess.CHECKPOINT_CAPTURE_OPERATION;
1115+
let started = 0;
1116+
let streamed = 0;
1117+
const driver = yield* GitVcsDriver.makeVcsDriverShape();
1118+
const captureDriver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
1119+
Effect.provideService(VcsProcess.VcsProcess, {
1120+
run: (input) => {
1121+
if (input.operation === operation) {
1122+
started++;
1123+
if (input.onStdoutChunk !== undefined) streamed++;
1124+
}
1125+
return liveProcess.run(input);
1126+
},
1127+
}),
1128+
);
1129+
const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-checkpoint-metrics-" });
1130+
const { checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
1131+
const before =
1132+
(yield* gitCommandCount(operation, "success")) +
1133+
(yield* gitCommandCount(operation, "failure"));
1134+
1135+
yield* captureDriver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
1136+
1137+
const after =
1138+
(yield* gitCommandCount(operation, "success")) +
1139+
(yield* gitCommandCount(operation, "failure"));
1140+
assert.isTrue(streamed > 0);
1141+
assert.strictEqual(after - before, started);
1142+
}).pipe(Effect.scoped, Effect.provide(layerGitContract)),
1143+
);

‎apps/server/src/vcs/GitVcsDriver.ts‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {
3535
} from "@t3tools/contracts";
3636
import { isMissingExecutable } from "@t3tools/source-control-core/server/discovery";
3737
import * as HostProcess from "@t3tools/shared/HostProcess";
38+
import { gitCommandDuration, gitCommandsTotal, withMetrics } from "../observability/Metrics.ts";
3839
import {
3940
makeGitVcsDriverCore,
4041
PATCH_RENDER_PREFIX_ARGS,
@@ -503,6 +504,18 @@ function parseGitRemoteVerboseOutput(
503504
return remotes;
504505
}
505506

507+
const runGitProcess = (
508+
process: VcsProcess.VcsProcess["Service"],
509+
input: VcsProcess.VcsProcessInput,
510+
) =>
511+
process.run(input).pipe(
512+
withMetrics({
513+
counter: gitCommandsTotal,
514+
timer: gitCommandDuration,
515+
attributes: { operation: input.operation },
516+
}),
517+
);
518+
506519
const gitCommand = (
507520
process: VcsProcess.VcsProcess["Service"],
508521
operation: string,
@@ -518,7 +531,7 @@ const gitCommand = (
518531
readonly appendTruncationMarker?: boolean;
519532
},
520533
) =>
521-
process.run({
534+
runGitProcess(process, {
522535
operation,
523536
command: "git",
524537
args: ["-C", cwd, ...args],
@@ -903,7 +916,7 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
903916
let skipped = false;
904917
let skippedRecord: number[] = [];
905918
const skippedPaths: string[] = [];
906-
yield* vcsProcess.run({
919+
yield* runGitProcess(vcsProcess, {
907920
operation,
908921
command: "git",
909922
cwd: input.cwd,

0 commit comments

Comments
 (0)