Something went wrong. Try again.
Monorepo for Tangled tangled.org
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";import { points, total } from "#lib/server/metrics.testing.ts";import { recordUpstream, requestFinished, requestStarted, resourceAttributes, startLagSampler, startMetrics, upstreamHost} from "#lib/server/metrics.ts";
describe("requests", () => { it("counts a finished request by route class and status class, and times it", () => { const route = "/[handle]/[repo]/blob/[...path]"; const before = total("svfe.http.requests", { route_class: route, status_class: "5xx" }); requestStarted(); requestFinished(route, 502, performance.now() - 250); expect(total("svfe.http.requests", { route_class: route, status_class: "5xx" })).toBe( before + 1 ); const [duration] = points("svfe.http.request.duration", { route_class: route }); expect(duration.sum).toBeGreaterThanOrEqual(0.25); });
it("reports the requests in flight", () => { const read = () => Number(points("svfe.http.in_flight")[0].asInt); const base = read(); requestStarted(); requestStarted(); expect(read()).toBe(base + 2); requestFinished("/", 200, performance.now()); requestFinished("/", 200, performance.now()); expect(read()).toBe(base); });});
describe("upstreams", () => { const ours = new Set(["https://bobbin.test"]);
it("names our own hosts and lumps the rest together", () => { expect(upstreamHost(new URL("https://bobbin.test/xrpc/x"), ours)).toBe("bobbin.test"); expect(upstreamHost(new URL("https://knot.example/info/refs"), ours)).toBe("other"); expect(upstreamHost(new URL("https://bobbin.test:8443/x"), ours)).toBe("other"); });
it("counts outcomes apart, with the status class when there was an answer", () => { const host = "upstream-labels.test"; recordUpstream(host, "ok", 200, performance.now()); recordUpstream(host, "ok", 503, performance.now()); recordUpstream(host, "timeout", undefined, performance.now() - 15_000); expect(total("svfe.upstream.requests", { host, outcome: "ok", status_class: "2xx" })).toBe( 1 ); expect(total("svfe.upstream.requests", { host, outcome: "ok", status_class: "5xx" })).toBe( 1 ); expect( total("svfe.upstream.requests", { host, outcome: "timeout", status_class: "none" }) ).toBe(1); expect(points("svfe.upstream.duration", { host })[0].sum).toBeGreaterThanOrEqual(15); });});
describe("event loop lag", () => { let now = 0; let stop: () => void; const lagged = () => total("svfe.event_loop.lag");
beforeEach(() => { vi.useFakeTimers(); now = 0; vi.spyOn(performance, "now").mockImplementation(() => now); stop = startLagSampler(500); }); afterEach(() => { stop(); vi.useRealTimers(); vi.restoreAllMocks(); });
const tick = (at: number) => { now = at; vi.advanceTimersByTime(500); };
it("records how late a tick came while a request was in flight", () => { const before = lagged(); requestStarted(); tick(700); expect(lagged()).toBe(before + 1); expect(points("svfe.event_loop.lag")[0].sum).toBeCloseTo(0.2, 3); requestFinished("/", 200, 0); });
it("ignores a late tick when the server was idle", () => { const before = lagged(); tick(5_000); expect(lagged()).toBe(before); });
it("ignores the first tick after a request woke a throttled instance", () => { const before = lagged(); tick(5_000); now = 6_000; requestStarted(); tick(6_100); expect(lagged()).toBe(before); tick(6_600); expect(lagged()).toBe(before + 1); requestFinished("/", 200, 0); });});
describe("resourceAttributes", () => { it("reads cloud run's key=value list", () => { expect(resourceAttributes("cloud.region=europe-north1, env=a%20b,empty=")).toEqual({ "cloud.region": "europe-north1", env: "a b", empty: "" }); expect(resourceAttributes("")).toEqual({}); });});
describe("startMetrics", () => { const config = (otlpEndpoint: string) => ({ otlpEndpoint, otlpAuth: "", otlpServiceName: "svfe", otlpResourceAttributes: "" }) as Parameters<typeof startMetrics>[0];
beforeEach(() => vi.useFakeTimers()); afterEach(() => vi.useRealTimers());
it("starts nothing without an endpoint", () => { expect(startMetrics(config(""))).toBeUndefined(); expect(vi.getTimerCount()).toBe(0); });
it("runs the sampler and the push with one, and stops both", () => { const stop = startMetrics(config("https://telemetry.test")); expect(vi.getTimerCount()).toBe(2); stop?.(); expect(vi.getTimerCount()).toBe(0); });});