Refine pipeline logging and add retention caps

This commit is contained in:
R.D.
2026-03-31 20:50:38 -04:00
parent fb9f72fc90
commit e00a95bf02
16 changed files with 295 additions and 50 deletions

View File

@@ -70,6 +70,7 @@ test("call logs store a single per-request artifact with pipeline details", asyn
assert.equal(artifact.summary.id, logId);
assert.equal(artifact.summary.requestedModel, "openai/gpt-5");
assert.equal(artifact.pipeline.clientRawRequest.body.raw, true);
assert.equal("sourceRequest" in artifact.pipeline, false);
});
test("call log artifact rotation removes directories older than CALL_LOG_RETENTION_DAYS", async () => {

View File

@@ -0,0 +1,74 @@
import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-call-log-files-"));
const ORIGINAL_DATA_DIR = process.env.DATA_DIR;
const ORIGINAL_RETENTION_DAYS = process.env.CALL_LOG_RETENTION_DAYS;
const ORIGINAL_MAX_ENTRIES = process.env.CALL_LOG_MAX_ENTRIES;
process.env.DATA_DIR = TEST_DATA_DIR;
process.env.CALL_LOG_RETENTION_DAYS = "7";
process.env.CALL_LOG_MAX_ENTRIES = "2";
const { rotateCallLogs } = await import("../../src/lib/usage/callLogs.ts");
const { CALL_LOGS_DIR } = await import("../../src/lib/usage/migrations.ts");
test.after(() => {
if (ORIGINAL_DATA_DIR === undefined) {
delete process.env.DATA_DIR;
} else {
process.env.DATA_DIR = ORIGINAL_DATA_DIR;
}
if (ORIGINAL_RETENTION_DAYS === undefined) {
delete process.env.CALL_LOG_RETENTION_DAYS;
} else {
process.env.CALL_LOG_RETENTION_DAYS = ORIGINAL_RETENTION_DAYS;
}
if (ORIGINAL_MAX_ENTRIES === undefined) {
delete process.env.CALL_LOG_MAX_ENTRIES;
} else {
process.env.CALL_LOG_MAX_ENTRIES = ORIGINAL_MAX_ENTRIES;
}
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true });
});
test("call log file rotation honors both retention days and file count", () => {
assert.ok(CALL_LOGS_DIR, "CALL_LOGS_DIR should resolve for test data dir");
fs.rmSync(CALL_LOGS_DIR, { recursive: true, force: true });
fs.mkdirSync(CALL_LOGS_DIR, { recursive: true });
const oldDir = path.join(CALL_LOGS_DIR, "2026-03-01");
const activeDir = path.join(CALL_LOGS_DIR, "2026-03-31");
fs.mkdirSync(oldDir, { recursive: true });
fs.mkdirSync(activeDir, { recursive: true });
const oldFile = path.join(oldDir, "080000_old_200.json");
const keepA = path.join(activeDir, "090000_keep-a_200.json");
const keepB = path.join(activeDir, "091000_keep-b_200.json");
const keepC = path.join(activeDir, "092000_keep-c_200.json");
for (const file of [oldFile, keepA, keepB, keepC]) {
fs.writeFileSync(file, JSON.stringify({ file }), "utf8");
}
const now = Date.now();
const oneDay = 24 * 60 * 60 * 1000;
fs.utimesSync(oldFile, new Date(now - 10 * oneDay), new Date(now - 10 * oneDay));
fs.utimesSync(oldDir, new Date(now - 10 * oneDay), new Date(now - 10 * oneDay));
fs.utimesSync(keepA, new Date(now - 3 * oneDay), new Date(now - 3 * oneDay));
fs.utimesSync(keepB, new Date(now - 2 * oneDay), new Date(now - 2 * oneDay));
fs.utimesSync(keepC, new Date(now - oneDay), new Date(now - oneDay));
rotateCallLogs();
assert.equal(fs.existsSync(oldDir), false);
assert.equal(fs.existsSync(keepA), false);
assert.equal(fs.existsSync(keepB), true);
assert.equal(fs.existsSync(keepC), true);
});

View File

@@ -0,0 +1,75 @@
import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
const { cleanupOldLogs, cleanupOverflowLogs, getLogConfig } =
await import("../../src/lib/logRotation.ts");
test("getLogConfig reads APP_LOG_* values", () => {
const originalEnv = {
APP_LOG_TO_FILE: process.env.APP_LOG_TO_FILE,
APP_LOG_FILE_PATH: process.env.APP_LOG_FILE_PATH,
APP_LOG_MAX_FILE_SIZE: process.env.APP_LOG_MAX_FILE_SIZE,
APP_LOG_RETENTION_DAYS: process.env.APP_LOG_RETENTION_DAYS,
APP_LOG_MAX_FILES: process.env.APP_LOG_MAX_FILES,
};
process.env.APP_LOG_TO_FILE = "false";
process.env.APP_LOG_FILE_PATH = "/tmp/omniroute-test-app.log";
process.env.APP_LOG_MAX_FILE_SIZE = "64M";
process.env.APP_LOG_RETENTION_DAYS = "14";
process.env.APP_LOG_MAX_FILES = "12";
try {
const config = getLogConfig();
assert.equal(config.logToFile, false);
assert.equal(config.logFilePath, "/tmp/omniroute-test-app.log");
assert.equal(config.maxFileSize, 64 * 1024 * 1024);
assert.equal(config.retentionDays, 14);
assert.equal(config.maxFiles, 12);
} finally {
for (const [key, value] of Object.entries(originalEnv)) {
if (value === undefined) {
delete process.env[key];
} else {
process.env[key] = value;
}
}
}
});
test("app log cleanup honors both retention days and file count", () => {
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-log-rotation-"));
const logFilePath = path.join(tmpDir, "app.log");
fs.writeFileSync(logFilePath, "", "utf8");
const oldFile = path.join(tmpDir, "app.2026-03-01_010101.log");
const keepA = path.join(tmpDir, "app.2026-03-02_010101.log");
const keepB = path.join(tmpDir, "app.2026-03-03_010101.log");
const dropByCount = path.join(tmpDir, "app.2026-03-04_010101.log");
for (const file of [oldFile, keepA, keepB, dropByCount]) {
fs.writeFileSync(file, file, "utf8");
}
const now = Date.now();
const oneDay = 24 * 60 * 60 * 1000;
fs.utimesSync(oldFile, new Date(now - 10 * oneDay), new Date(now - 10 * oneDay));
fs.utimesSync(keepA, new Date(now - 3 * oneDay), new Date(now - 3 * oneDay));
fs.utimesSync(keepB, new Date(now - 2 * oneDay), new Date(now - 2 * oneDay));
fs.utimesSync(dropByCount, new Date(now - oneDay), new Date(now - oneDay));
cleanupOldLogs(logFilePath, 7);
cleanupOverflowLogs(logFilePath, 2);
assert.equal(fs.existsSync(oldFile), false);
assert.equal(fs.existsSync(keepA), false);
assert.equal(fs.existsSync(keepB), true);
assert.equal(fs.existsSync(dropByCount), true);
fs.rmSync(tmpDir, { recursive: true, force: true });
});