caelum0x ab9e54a189 feat(relay): per-key trace view and OD-RY2 trace retention (RY-RL3)
Implements OSS9. New relay-traces crate reads chat/json inferences by the
GIN-indexed tensorzero::api_key_public_id tag, joined with model_inferences
for model, provider, tokens, latency, cost and finish reason.

- GET /relay/traces: caller's own traces, newest first, keyset paging,
  window bounded (1-2160 h). Operator keys may use scope=all and key=.
- GET /relay/traces/{inference_id}: model calls plus stored bodies; a trace
  owned by another key returns 404, same as a missing one.
- relay-traces-retention: 14 d bodies, 90 d metadata (OD-RY2 defaults, env
  overridable), batched deletes against one cutoff, dry-run mode. Shipped in
  the relay image; systemd service/timer in crates/relay-traces/deploy (not
  installed).
- Relay UI: /observability/keys list (live refresh on the first page) and
  detail page; sidebar "Key Traces".
- No new tables or migrations.

Tests: 8 unit tests; live test on a scratch copy of the inference tables
proves key isolation, operator scope, paging and window-exact retention;
UI vitest 7/7, tsc and eslint clean; cargo check -p gateway clean.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 02:12:42 +03:00

76 lines
2.2 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { traceListQuery } from "~/types/traces";
import {
DEFAULT_WINDOW_HOURS,
filtersFromUrl,
formatLatency,
formatTraceCost,
hrefForFilters,
traceStatus,
} from "./format";
describe("trace view formatting", () => {
it("shows unknown prices honestly", () => {
expect(formatTraceCost(undefined)).toBe("unpriced");
expect(formatTraceCost(1250)).toBe("$0.00125");
});
it("formats latency", () => {
expect(formatLatency(undefined)).toBe("-");
expect(formatLatency(812)).toBe("812 ms");
expect(formatLatency(2500)).toBe("2.50 s");
});
it("derives a status", () => {
expect(traceStatus({ model_calls: 0 })).toBe("no model call stored");
expect(traceStatus({ model_calls: 2, finish_reason: "stop" })).toBe("stop");
expect(traceStatus({ model_calls: 1 })).toBe("unknown");
});
});
describe("trace filters", () => {
it("drops the key filter outside the operator scope", () => {
const filters = filtersFromUrl("http://ui/observability/keys?key=abc");
expect(filters.scope).toBe("own");
expect(filters.key).toBeUndefined();
});
it("keeps operator filters and rejects unknown windows", () => {
const filters = filtersFromUrl(
"http://ui/observability/keys?scope=all&key=abc&window_hours=999",
);
expect(filters).toMatchObject({
scope: "all",
key: "abc",
windowHours: DEFAULT_WINDOW_HOURS,
});
});
it("round-trips filters through a URL", () => {
const href = hrefForFilters("/observability/keys", {
scope: "all",
key: "k1",
windowHours: 24,
before: "1:abc",
functionName: "draft_email",
});
expect(filtersFromUrl(`http://ui${href}`)).toEqual({
scope: "all",
key: "k1",
windowHours: 24,
before: "1:abc",
functionName: "draft_email",
});
});
it("builds the gateway query", () => {
expect(traceListQuery()).toBe("");
expect(traceListQuery({ scope: "own", key: "ignored", limit: 50 })).toBe(
"?limit=50",
);
expect(
traceListQuery({ scope: "all", key: "k", windowHours: 24, before: "c" }),
).toBe("?scope=all&key=k&window_hours=24&before=c");
});
});