Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

observability.ts

203 lines8.0 KB

1import * as Axiom from "alchemy/Axiom";
2import * as Output from "alchemy/Output";
3import * as Effect from "effect/Effect";
4import { cacheName } from "./resources.ts";
5​
6// Native summaries may be observed by nested launchers and retried after a lost response.
7// Deduplicate before summing; task runtime is a compute proxy, not measured CPU or critical-path time.
8export const savingsQuery = (
9 dataset: string,
10 group?: "developer" | "host" | "ci",
11 lifetime = false,
12) =>
13 [
14 `['${dataset}']`,
15 `| where name == '${lifetime ? "TaskCache.lifetime" : "TaskCache.summary"}'`,
16 `| extend a = ['attributes.custom']`,
17 `| extend event_id = tostring(a['cache.event_id']), developer = tostring(a['developer.id']), host = tostring(['attributes.host.name']), ci = tobool(a['ci']), recorded_at = tolong(a['cache.recorded_at']), unspecified_ms = coalesce(tolong(a['cache.unspecified_saved_ms']), 0), unspecified_hits = coalesce(tolong(a['cache.unspecified_hits']), 0), local_ms = tolong(a['cache.local_saved_ms']), remote_ms = tolong(a['cache.remote_saved_ms']), local_hits = tolong(a['cache.local_hits']), remote_hits = tolong(a['cache.remote_hits']), executed = tolong(a['cache.executed']), restore_failures = tolong(a['cache.restore_failures'])`,
18 `| summarize arg_max(_time, unspecified_ms, unspecified_hits, local_ms, remote_ms, local_hits, remote_hits, executed, restore_failures${lifetime ? "" : ", developer, host, ci, recorded_at"}) by ${lifetime ? "developer, host, ci, platform = tostring(a['host.os']), arch = tostring(['attributes.host.arch'])" : "event_id"}`,
19 ...(!lifetime ? ["| extend _time = unixtime_milliseconds_todatetime(recorded_at)"] : []),
20 `| where local_hits + remote_hits + unspecified_hits + executed + restore_failures > 0`,
21 `| summarize saved_task_hours = sum(local_ms + remote_ms + unspecified_ms) / 3600000.0, remote_saved_hours = sum(remote_ms) / 3600000.0, local_saved_hours = sum(local_ms) / 3600000.0, unspecified_saved_hours = sum(unspecified_ms) / 3600000.0, unspecified_hits = sum(unspecified_hits), local_hits = sum(local_hits), remote_hits = sum(remote_hits), executed = sum(executed), restore_failures = sum(restore_failures)${group ? ` by ${group}` : ""}`,
22 ].join("\n");
23​
24const summaryRows = (dataset: string) => savingsQuery(dataset).split("\n").slice(0, -1).join("\n");
25​
26export const Observability = Effect.gen(function* () {
27 const name = yield* cacheName;
28 const traces = yield* Axiom.Dataset("CacheTraces", {
29 name: `${name}-traces`,
30 kind: "otel:traces:v1",
31 description: "Shared task cache: service traces and unsampled native task savings reports.",
32 retentionDays: 30,
33 useRetentionPeriod: true,
34 });
35 const token = yield* Axiom.ApiToken("CacheIngestToken", {
36 name: `${name}-otel-ingest`,
37 datasetCapabilities: Output.map(traces.name, (dataset) => ({
38 [dataset]: { ingest: ["create" as const] },
39 })),
40 });
41 const charts = [
42 {
43 id: "total",
44 name: "⏳ Task hours given back · lifetime",
45 type: "Statistic",
46 x: 0,
47 y: 0,
48 w: 3,
49 h: 3,
50 query: (dataset: string) =>
51 `${savingsQuery(dataset, undefined, true)}\n| project saved_task_hours`,
52 },
53 {
54 id: "remote",
55 name: "🌍 Remote hours shared · lifetime",
56 type: "Statistic",
57 x: 3,
58 y: 0,
59 w: 3,
60 h: 3,
61 query: (dataset: string) =>
62 `${savingsQuery(dataset, undefined, true)}\n| project remote_saved_hours`,
63 },
64 {
65 id: "hit-rate",
66 name: "🎯 Cache hit rate · lifetime (%)",
67 type: "Statistic",
68 x: 6,
69 y: 0,
70 w: 3,
71 h: 3,
72 query: (dataset: string) =>
73 `${savingsQuery(dataset, undefined, true)}\n| project hit_rate = round(100.0 * (local_hits + remote_hits + unspecified_hits) / (local_hits + remote_hits + unspecified_hits + executed), 1)`,
74 },
75 {
76 id: "skipped",
77 name: "⚡ Tasks we skipped · lifetime",
78 type: "Statistic",
79 x: 9,
80 y: 0,
81 w: 3,
82 h: 3,
83 query: (dataset: string) =>
84 `${savingsQuery(dataset, undefined, true)}\n| project cached_tasks = local_hits + remote_hits + unspecified_hits`,
85 },
86 {
87 id: "savings-trend",
88 name: "📈 Minutes given back over time",
89 type: "TimeSeries",
90 x: 0,
91 y: 3,
92 w: 8,
93 h: 5,
94 query: (dataset: string) =>
95 `${summaryRows(dataset)}\n| summarize local_minutes = sum(local_ms) / 60000.0, remote_minutes = sum(remote_ms) / 60000.0, unspecified_minutes = sum(unspecified_ms) / 60000.0 by bin_auto(_time)`,
96 },
97 {
98 id: "developer",
99 name: "🏆 Who is saving the most?",
100 type: "Pie",
101 x: 8,
102 y: 3,
103 w: 4,
104 h: 5,
105 query: (dataset: string) =>
106 `${savingsQuery(dataset, "developer", true)}\n| project developer, saved_task_hours\n| order by saved_task_hours desc`,
107 },
108 {
109 id: "tasks-trend",
110 name: "🔥 Cache hits vs actual execution",
111 type: "TimeSeries",
112 x: 0,
113 y: 8,
114 w: 8,
115 h: 5,
116 query: (dataset: string) =>
117 `${summaryRows(dataset)}\n| summarize local_hits = sum(local_hits), remote_hits = sum(remote_hits), unspecified_hits = sum(unspecified_hits), executed = sum(executed) by bin_auto(_time)`,
118 },
119 {
120 id: "host",
121 name: "🖥️ The machine leaderboard",
122 type: "Pie",
123 x: 8,
124 y: 8,
125 w: 4,
126 h: 5,
127 query: (dataset: string) =>
128 `${savingsQuery(dataset, "host", true)}\n| project host, saved_task_hours\n| order by saved_task_hours desc`,
129 },
130 {
131 id: "leaderboard",
132 name: "🏅 Developer scorecard · lifetime",
133 type: "Table",
134 x: 0,
135 y: 13,
136 w: 8,
137 h: 4,
138 query: (dataset: string) =>
139 `${savingsQuery(dataset, "developer", true)}\n| project developer, saved_task_hours, remote_saved_hours, cached_tasks = local_hits + remote_hits + unspecified_hits, executed\n| order by saved_task_hours desc`,
140 },
141 {
142 id: "ci",
143 name: "🤖 CI vs developer machines · hours saved",
144 type: "Pie",
145 x: 8,
146 y: 13,
147 w: 4,
148 h: 4,
149 query: (dataset: string) =>
150 `${savingsQuery(dataset, "ci", true)}\n| summarize saved_task_hours = sum(saved_task_hours) by environment = iff(ci, "CI", "Developer machines")`,
151 },
152 ] as const;
153 for (const chart of charts) {
154 yield* Axiom.View(`CacheSavings-${chart.id}`, {
155 name: `${name}-${chart.id}-savings`,
156 description: chart.name,
157 datasets: [traces.name],
158 aplQuery: Output.map(traces.name, chart.query),
159 });
160 }
161 const dashboard = yield* Axiom.Dashboard("CacheSavingsDashboard", {
162 dashboard: {
163 name: "⚡ Task Cache · Time Back",
164 owner: "",
165 schemaVersion: 2,
166 refreshTime: 60,
167 description:
168 "Avoided task execution time, not CPU seconds or elapsed developer time. Local and remote hits; deduplicated native Vite+ summaries. Lifetime counters are retained in D1 and refreshed hourly. Raw traces have 30-day retention.",
169 timeWindowStart: "qr-now-30d",
170 timeWindowEnd: "qr-now",
171 charts: charts.map((chart) => {
172 const common = {
173 id: chart.id,
174 name: chart.name,
175 query: { apl: Output.map(traces.name, chart.query) },
176 };
177 switch (chart.type) {
178 case "Statistic":
179 return { ...common, type: "Statistic" as const };
180 case "TimeSeries":
181 return {
182 ...common,
183 type: "TimeSeries" as const,
184 query: {
185 ...common.query,
186 queryOptions: {
187 timeSeriesVariant: chart.id === "savings-trend" ? "area" : "bars",
188 displayNull: "zero",
189 overlayCharts: "true",
190 },
191 },
192 };
193 case "Pie":
194 return { ...common, type: "Pie" as const };
195 case "Table":
196 return { ...common, type: "Table" as const };
197 }
198 }),
199 layout: charts.map(({ id, x, y, w, h }) => ({ i: id, x, y, w, h })),
200 },
201 });
202 return { traces, token, dashboard };
203});