Skip to content
92 linesCodeBlameRaw

Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.

Projects have Observability: their requests, logs, traces, metrics and errors, from apps on g1t.page with nothing to set up, and from software anywhere else over OpenTelemetry.1import assert from "node:assert/strict";
2import test from "node:test";
3
4import type { TelemetrySpan } from "@g1t/contracts";
5
6import { change, count, duration, errorIssue, filterOf, matchesLog, niceTicks, parseView, percent, viewHref } from "./observability.ts";
7import { spanTree } from "./observability-trace.ts";
8
9test("a view reads from the address and writes back to it", () => {
10 const view = parseView(new URLSearchParams("range=1h&env=production&q=timeout"));
11 assert.deepEqual(view, { range: "1h", environment: "production", service: null, deployment: null, q: "timeout" });
12 assert.equal(viewHref("/a/b/observability/logs", view), "/a/b/observability/logs?range=1h&env=production&q=timeout");
13 assert.equal(viewHref("/x", view, { env: null, q: null, range: "24h" }), "/x?range=24h");
14 // The default period is left out of the address.
15 assert.equal(viewHref("/x", parseView(new URLSearchParams("range=nonsense"))), "/x");
16 const filter = filterOf(view, 3_600_000);
17 assert.equal(filter.from_us, 0);
18 assert.equal(filter.to_us, 3_600_000_000);
19 assert.deepEqual(filter.environments, ["production"]);
20});
21
22test("numbers read as people read them", () => {
23 assert.equal(duration(0.42), "420 µs");
24 assert.equal(duration(4.25), "4.3 ms");
25 assert.equal(duration(87), "87 ms");
26 assert.equal(duration(1500), "1.5 s");
27 assert.equal(duration(125_000), "2m 5s");
28 assert.equal(duration(null), "—");
29 assert.equal(duration(0), "0");
30 assert.equal(count(12_400), "12.4K");
31 assert.equal(percent(0.0004), "0.04%");
32 assert.equal(percent(0.25), "25%");
33 assert.deepEqual(change(110, 100, true), { text: "+10%", good: false });
34 assert.deepEqual(change(90, 100, true), { text: "−10%", good: true });
35 assert.equal(change(5, 0, true), null);
36 assert.deepEqual(niceTicks(0.37), [0, 0.1, 0.2, 0.3, 0.4]);
37 assert.deepEqual(niceTicks(870), [0, 250, 500, 750, 1000]);
38});
39
40test("a live line is shown only when the page's search would have found it", () => {
41 const log = { severity: "error" as const, body: "Payment timeout for order 7", environment: "production", service: "api", deployment_id: null, attributes: { "user.id": "42" } };
42 const view = parseView(new URLSearchParams("env=production"));
43 assert.equal(matchesLog(log, view, []), true);
44 assert.equal(matchesLog(log, { ...view, q: "timeout" }, []), true);
45 assert.equal(matchesLog(log, { ...view, q: "refund" }, []), false);
46 assert.equal(matchesLog(log, { ...view, q: "user.id=42" }, []), true);
47 assert.equal(matchesLog(log, { ...view, q: "user.id=7" }, []), false);
48 assert.equal(matchesLog(log, { ...view, q: "level:warn" }, []), false);
49 assert.equal(matchesLog(log, view, ["error", "fatal"]), true);
50 assert.equal(matchesLog(log, { ...view, environment: "preview" }, []), false);
51});
52
53const span = (id: string, parent: string | null, start: number): TelemetrySpan => ({
54 trace_id: "t",
55 span_id: id,
56 parent_span_id: parent,
57 name: id,
58 kind: "internal",
59 start_us: start,
60 duration_us: 1,
61 status: "unset",
62 status_message: null,
63 attributes: {},
64 resource: {},
65 events: [],
66 links: [],
67 service: "s",
68 environment: null,
69 deployment_id: null,
70 commit_sha: null,
71 source: "otlp",
72});
73
74test("spans nest under their parents, and orphans are still shown", () => {
75 const rows = spanTree([span("c", "a", 3), span("a", null, 1), span("b", "a", 2), span("x", "missing", 5), span("loop1", "loop2", 9), span("loop2", "loop1", 9)]);
76 assert.deepEqual(
77 rows.map((r) => [r.span.span_id, r.depth]),
78 [["a", 0], ["b", 1], ["c", 1], ["x", 0], ["loop1", 0], ["loop2", 0]],
79 );
80});
81
82test("an error's issue says what, how often and where to look", () => {
83 const issue = errorIssue(
84 { name: "TypeError", message: "x is undefined", stack: "TypeError\n at f (a.ts:1)", count: 3, first_us: 0, last_us: 60_000_000, service: "web", environment: "production", first_deployment_id: "dep_1", last_request_id: "r1", last_trace_id: null },
85 "https://g1t.sh/acme/web/observability",
86 );
87 assert.equal(issue.title, "TypeError: x is undefined");
88 assert.match(issue.body, /3 times/);
89 assert.match(issue.body, /requests\/r1/);
90 assert.match(issue.body, /dep_1/);
91 assert.doesNotMatch(issue.body, /traces\//);
92});