@obzerver/server 0.14.0 → 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/alerts.js +9 -3
- package/dist/alerts.js.map +1 -1
- package/dist/checkpoint.d.ts +18 -0
- package/dist/checkpoint.js +115 -0
- package/dist/checkpoint.js.map +1 -0
- package/dist/cli.js +5 -1
- package/dist/cli.js.map +1 -1
- package/dist/db.d.ts +34 -2
- package/dist/db.js +196 -13
- package/dist/db.js.map +1 -1
- package/dist/history.js +3 -2
- package/dist/history.js.map +1 -1
- package/dist/ingest.d.ts +1 -1
- package/dist/ingest.js +93 -3
- package/dist/ingest.js.map +1 -1
- package/dist/ingestqueue.d.ts +37 -0
- package/dist/ingestqueue.js +128 -0
- package/dist/ingestqueue.js.map +1 -0
- package/dist/mathutil.d.ts +3 -0
- package/dist/mathutil.js +4 -0
- package/dist/mathutil.js.map +1 -0
- package/dist/mcp.js +4 -4
- package/dist/mcp.js.map +1 -1
- package/dist/queries.d.ts +89 -0
- package/dist/queries.js +424 -78
- package/dist/queries.js.map +1 -1
- package/dist/reader.d.ts +34 -0
- package/dist/reader.js +104 -0
- package/dist/reader.js.map +1 -0
- package/dist/readworker.d.ts +32 -0
- package/dist/readworker.js +35 -0
- package/dist/readworker.js.map +1 -0
- package/dist/seed.js +147 -1
- package/dist/seed.js.map +1 -1
- package/dist/server.d.ts +12 -0
- package/dist/server.js +64 -35
- package/dist/server.js.map +1 -1
- package/dist/usage.js +20 -6
- package/dist/usage.js.map +1 -1
- package/package.json +3 -3
- package/public/assets/index-CW1YpnVk.js +118 -0
- package/public/assets/index-CqY78TFX.css +2 -0
- package/public/index.html +2 -2
- package/public/assets/index-BzoQW7Fz.css +0 -2
- package/public/assets/index-XA6fr8QX.js +0 -114
package/dist/queries.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { VERSION } from './version.js';
|
|
2
|
+
import { maxOf, minOf } from './mathutil.js';
|
|
3
|
+
import { SLOT_MS } from './db.js';
|
|
2
4
|
import { openSecretCount, secretGroups } from './secrets.js';
|
|
3
5
|
import { band, baseline, typical, historySplit, hybridOperations, hybridSeries, hybridTotals, historyErrorCount, usualWindow, rawOldest } from './history.js';
|
|
4
6
|
// ------------------------------------------------------------------ helpers
|
|
@@ -18,6 +20,8 @@ const round0 = (v, d = 2) => round(v, d) ?? 0;
|
|
|
18
20
|
const avg = (arr) => (arr.length ? arr.reduce((a, b) => a + b, 0) / arr.length : null);
|
|
19
21
|
const sum = (arr) => arr.reduce((a, b) => a + b, 0);
|
|
20
22
|
const asc = (a, b) => a - b;
|
|
23
|
+
/** Code-point order, as SQLite sorts text. */
|
|
24
|
+
const cmp = (a, b) => (a < b ? -1 : a > b ? 1 : 0);
|
|
21
25
|
function parse(s, fb) {
|
|
22
26
|
try {
|
|
23
27
|
return s ? JSON.parse(s) : fb;
|
|
@@ -73,6 +77,250 @@ function stats(rows) {
|
|
|
73
77
|
totalTime: round0(sum(d), 0),
|
|
74
78
|
};
|
|
75
79
|
}
|
|
80
|
+
export function readColumns(db, where, args, { ops = false, queueWait = false, index = true } = {}) {
|
|
81
|
+
const r = db.get(`SELECT COUNT(*) n, json_group_array(ts) ts, json_group_array(duration) d, group_concat(failed, '') f, json_group_array(status) st
|
|
82
|
+
${queueWait ? ', json_group_array(queue_wait) qw' : ''}${ops ? ', json_group_array(service) sv, json_group_array(name) nm' : ''}
|
|
83
|
+
FROM traces${index ? ' INDEXED BY traces_window' : ''} WHERE ${where}`, ...args);
|
|
84
|
+
const n = r.n;
|
|
85
|
+
const f = r.f || '';
|
|
86
|
+
const failed = new Uint8Array(n);
|
|
87
|
+
for (let i = 0; i < n; i++)
|
|
88
|
+
failed[i] = f.charCodeAt(i) === 49 ? 1 : 0;
|
|
89
|
+
const st = JSON.parse(r.st);
|
|
90
|
+
const status = new Int32Array(n);
|
|
91
|
+
for (let i = 0; i < n; i++)
|
|
92
|
+
status[i] = st[i] ?? -1;
|
|
93
|
+
let qw = null;
|
|
94
|
+
if (queueWait) {
|
|
95
|
+
const q = JSON.parse(r.qw);
|
|
96
|
+
qw = new Float64Array(n);
|
|
97
|
+
for (let i = 0; i < n; i++)
|
|
98
|
+
qw[i] = q[i] ?? NaN;
|
|
99
|
+
}
|
|
100
|
+
let op = null;
|
|
101
|
+
const list = [];
|
|
102
|
+
if (ops) {
|
|
103
|
+
const sv = JSON.parse(r.sv);
|
|
104
|
+
const nm = JSON.parse(r.nm);
|
|
105
|
+
const ids = new Map();
|
|
106
|
+
op = new Int32Array(n);
|
|
107
|
+
for (let i = 0; i < n; i++) {
|
|
108
|
+
let byName = ids.get(sv[i]);
|
|
109
|
+
if (!byName)
|
|
110
|
+
ids.set(sv[i], (byName = new Map()));
|
|
111
|
+
let id = byName.get(nm[i]);
|
|
112
|
+
if (id === undefined) {
|
|
113
|
+
id = list.length;
|
|
114
|
+
byName.set(nm[i], id);
|
|
115
|
+
list.push({ service: sv[i], name: nm[i] });
|
|
116
|
+
}
|
|
117
|
+
op[i] = id;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return { n, ts: Float64Array.from(JSON.parse(r.ts)), d: Float64Array.from(JSON.parse(r.d)), failed, status, qw, op, ops: list };
|
|
121
|
+
}
|
|
122
|
+
// ------------------------------------------------------------------ the window cache
|
|
123
|
+
/**
|
|
124
|
+
* A window is read as 5-minute slots. A slot that lies wholly inside the window is kept in memory after it is
|
|
125
|
+
* read, together with the slot's write mark (`slot_marks`, bumped by ingest and retention whenever a row in
|
|
126
|
+
* that slot is written or deleted); the next read reuses it while the mark is unchanged. Only the window's
|
|
127
|
+
* partial first slot, its open last slot and slots that changed are read from SQLite again. The numbers are
|
|
128
|
+
* computed from the same rows as before, so they stay exact; the cache only saves reading them.
|
|
129
|
+
*/
|
|
130
|
+
export const SLOT = SLOT_MS;
|
|
131
|
+
const cacheLimit = (() => { const mb = Number(process.env.OBZERVER_READ_CACHE_MB ?? 256); return Number.isFinite(mb) && mb >= 0 ? mb * 1024 * 1024 : 256 * 1024 * 1024; })();
|
|
132
|
+
const slotCaches = new WeakMap();
|
|
133
|
+
let cachedBytes = 0;
|
|
134
|
+
export const cacheStats = { hits: 0, misses: 0, evicted: 0, get bytes() { return cachedBytes; } };
|
|
135
|
+
const colBytes = (c) => c.n * (8 + 8 + 1 + 4 + (c.qw ? 8 : 0) + (c.op ? 4 : 0)) + c.ops.length * 64;
|
|
136
|
+
/**
|
|
137
|
+
* The window's rows as columns: `base` filters (no time condition) and the time range is [from, to], both ends
|
|
138
|
+
* included. Same rows, same numbers as `readColumns` over the whole window.
|
|
139
|
+
*/
|
|
140
|
+
export function readWindow(db, base, args, from, to, opts = {}) {
|
|
141
|
+
const range = (lo, hi, inclusive) => readColumns(db, `${base} AND ts >= ? AND ts ${inclusive ? '<=' : '<'} ?`, [...args, lo, hi], opts);
|
|
142
|
+
const first = Math.ceil(from / SLOT) * SLOT;
|
|
143
|
+
const last = Math.floor((to + 1) / SLOT) * SLOT; // whole slots [first, last) lie inside [from, to]
|
|
144
|
+
if (!cacheLimit || last - first < SLOT)
|
|
145
|
+
return range(from, to, true);
|
|
146
|
+
let cache = slotCaches.get(db.raw);
|
|
147
|
+
if (!cache)
|
|
148
|
+
slotCaches.set(db.raw, (cache = new Map()));
|
|
149
|
+
// Marks first, rows second: a write in between leaves a newer mark than the cached rows' and is read again.
|
|
150
|
+
const marks = new Map(db.all('SELECT slot, v FROM slot_marks WHERE slot >= ? AND slot < ?', first, last).map((r) => [r.slot, r.v]));
|
|
151
|
+
const key = `${base}\u0001${JSON.stringify(args)}\u0001${opts.ops ? 1 : 0}${opts.queueWait ? 1 : 0}\u0001`;
|
|
152
|
+
const parts = [];
|
|
153
|
+
if (from < first)
|
|
154
|
+
parts.push(range(from, first, false));
|
|
155
|
+
for (let t = first; t < last; t += SLOT) {
|
|
156
|
+
const mark = marks.get(t) ?? 0;
|
|
157
|
+
const k = key + t;
|
|
158
|
+
const hit = cache.get(k);
|
|
159
|
+
if (hit && hit.mark === mark) {
|
|
160
|
+
cacheStats.hits++;
|
|
161
|
+
cache.delete(k);
|
|
162
|
+
cache.set(k, hit); // most recently used last
|
|
163
|
+
parts.push(hit.cols);
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
cacheStats.misses++;
|
|
167
|
+
const cols = range(t, t + SLOT, false);
|
|
168
|
+
if (hit) {
|
|
169
|
+
cachedBytes -= hit.bytes;
|
|
170
|
+
cache.delete(k);
|
|
171
|
+
}
|
|
172
|
+
const bytes = colBytes(cols);
|
|
173
|
+
cache.set(k, { mark, cols, bytes });
|
|
174
|
+
cachedBytes += bytes;
|
|
175
|
+
parts.push(cols);
|
|
176
|
+
}
|
|
177
|
+
if (last <= to)
|
|
178
|
+
parts.push(range(last, to, true));
|
|
179
|
+
// Least recently used first out.
|
|
180
|
+
for (const [k, v] of cache) {
|
|
181
|
+
if (cachedBytes <= cacheLimit)
|
|
182
|
+
break;
|
|
183
|
+
cache.delete(k);
|
|
184
|
+
cachedBytes -= v.bytes;
|
|
185
|
+
cacheStats.evicted++;
|
|
186
|
+
}
|
|
187
|
+
return concatColumns(parts, opts);
|
|
188
|
+
}
|
|
189
|
+
/** Columns end to end, with each part's operations mapped onto one list. */
|
|
190
|
+
function concatColumns(parts, { ops = false, queueWait = false }) {
|
|
191
|
+
const n = parts.reduce((a, c) => a + c.n, 0);
|
|
192
|
+
const out = {
|
|
193
|
+
n, ts: new Float64Array(n), d: new Float64Array(n), failed: new Uint8Array(n), status: new Int32Array(n),
|
|
194
|
+
qw: queueWait ? new Float64Array(n) : null, op: ops ? new Int32Array(n) : null, ops: [],
|
|
195
|
+
};
|
|
196
|
+
const ids = new Map();
|
|
197
|
+
let at = 0;
|
|
198
|
+
for (const c of parts) {
|
|
199
|
+
out.ts.set(c.ts, at);
|
|
200
|
+
out.d.set(c.d, at);
|
|
201
|
+
out.failed.set(c.failed, at);
|
|
202
|
+
out.status.set(c.status, at);
|
|
203
|
+
if (out.qw && c.qw)
|
|
204
|
+
out.qw.set(c.qw, at);
|
|
205
|
+
if (out.op && c.op) {
|
|
206
|
+
const map = c.ops.map(({ service, name }) => {
|
|
207
|
+
const k = `${service}\u0000${name}`;
|
|
208
|
+
let id = ids.get(k);
|
|
209
|
+
if (id === undefined) {
|
|
210
|
+
id = out.ops.length;
|
|
211
|
+
ids.set(k, id);
|
|
212
|
+
out.ops.push({ service, name });
|
|
213
|
+
}
|
|
214
|
+
return id;
|
|
215
|
+
});
|
|
216
|
+
for (let i = 0; i < c.n; i++)
|
|
217
|
+
out.op[at + i] = map[c.op[i]];
|
|
218
|
+
}
|
|
219
|
+
at += c.n;
|
|
220
|
+
}
|
|
221
|
+
return out;
|
|
222
|
+
}
|
|
223
|
+
/** The scope's conditions without the time range (readWindow adds it). */
|
|
224
|
+
function scopeBase(w) {
|
|
225
|
+
const cond = [];
|
|
226
|
+
const args = [];
|
|
227
|
+
if (w.service) {
|
|
228
|
+
cond.push('service = ?');
|
|
229
|
+
args.push(w.service);
|
|
230
|
+
}
|
|
231
|
+
if (w.env) {
|
|
232
|
+
cond.push('env = ?');
|
|
233
|
+
args.push(w.env);
|
|
234
|
+
}
|
|
235
|
+
return { sql: cond.length ? ` AND ${cond.join(' AND ')}` : '', args };
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Exact statistics per group: each group's values are laid out next to each other (a counting sort, O(n))
|
|
239
|
+
* and each group's run is sorted on its own, natively, as a Float64Array. Percentiles are nearest-rank, the
|
|
240
|
+
* definition `percentile` uses: the ceil(p·n/100)-th smallest value.
|
|
241
|
+
* `groupOf(i)` is the group of row i, or -1 to leave the row out. Rows without a value (NaN) are left out.
|
|
242
|
+
*/
|
|
243
|
+
export function groupAgg(c, groups, groupOf, value = c.d) {
|
|
244
|
+
const count = new Int32Array(groups);
|
|
245
|
+
const g = new Int32Array(c.n);
|
|
246
|
+
for (let i = 0; i < c.n; i++) {
|
|
247
|
+
const k = Number.isNaN(value[i]) ? -1 : groupOf(i);
|
|
248
|
+
g[i] = k;
|
|
249
|
+
if (k >= 0)
|
|
250
|
+
count[k]++;
|
|
251
|
+
}
|
|
252
|
+
const start = new Int32Array(groups + 1);
|
|
253
|
+
for (let k = 0; k < groups; k++)
|
|
254
|
+
start[k + 1] = start[k] + count[k];
|
|
255
|
+
const pos = start.slice(0, groups);
|
|
256
|
+
const vals = new Float64Array(start[groups]);
|
|
257
|
+
const out = Array.from({ length: groups }, () => ({ calls: 0, failed: 0, s4xx: 0, s5xx: 0, total: 0, max: null, last: null, p50: null, p95: null, p99: null }));
|
|
258
|
+
for (let i = 0; i < c.n; i++) {
|
|
259
|
+
const k = g[i];
|
|
260
|
+
if (k < 0)
|
|
261
|
+
continue;
|
|
262
|
+
const v = value[i];
|
|
263
|
+
vals[pos[k]++] = v;
|
|
264
|
+
const a = out[k];
|
|
265
|
+
a.calls++;
|
|
266
|
+
a.failed += c.failed[i];
|
|
267
|
+
const s = c.status[i];
|
|
268
|
+
if (s >= 500)
|
|
269
|
+
a.s5xx++;
|
|
270
|
+
else if (s >= 400)
|
|
271
|
+
a.s4xx++;
|
|
272
|
+
a.total += Math.round(v * 1000);
|
|
273
|
+
if (a.last == null || c.ts[i] > a.last)
|
|
274
|
+
a.last = c.ts[i];
|
|
275
|
+
}
|
|
276
|
+
for (let k = 0; k < groups; k++) {
|
|
277
|
+
const n = count[k];
|
|
278
|
+
if (!n)
|
|
279
|
+
continue;
|
|
280
|
+
const run = vals.subarray(start[k], start[k + 1]).sort();
|
|
281
|
+
const a = out[k];
|
|
282
|
+
const at = (p) => run[Math.ceil((p * n) / 100) - 1];
|
|
283
|
+
a.max = run[n - 1];
|
|
284
|
+
a.p50 = at(50);
|
|
285
|
+
a.p95 = at(95);
|
|
286
|
+
a.p99 = at(99);
|
|
287
|
+
}
|
|
288
|
+
return out;
|
|
289
|
+
}
|
|
290
|
+
/** The mean in ms to two decimals, rounded half up from the exact microsecond total. */
|
|
291
|
+
const meanOf = (a) => (a && a.calls ? Math.round(a.total / (a.calls * 10)) / 100 : null);
|
|
292
|
+
/** `Stats` from an aggregate (an empty window when there is none). */
|
|
293
|
+
function statsOf(a) {
|
|
294
|
+
if (!a || !a.calls)
|
|
295
|
+
return { calls: 0, failed: 0, failureRate: 0, avg: null, p50: null, p95: null, p99: null, max: null, totalTime: 0 };
|
|
296
|
+
return {
|
|
297
|
+
calls: a.calls, failed: a.failed, failureRate: round0((a.failed / a.calls) * 100), avg: meanOf(a),
|
|
298
|
+
p50: round(a.p50), p95: round(a.p95), p99: round(a.p99), max: round(a.max), totalTime: Math.round(a.total / 1000),
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
const allRows = () => 0;
|
|
302
|
+
/** Time series per bucket: the same points 0.15 built from row objects. */
|
|
303
|
+
function seriesOf(c, w, extra) {
|
|
304
|
+
const b = buckets(w);
|
|
305
|
+
const at = (i) => b.index(c.ts[i]);
|
|
306
|
+
const by = groupAgg(c, b.n, at);
|
|
307
|
+
const qw = extra === 'job' && c.qw ? groupAgg(c, b.n, at, c.qw) : null;
|
|
308
|
+
return {
|
|
309
|
+
bucketMs: b.size,
|
|
310
|
+
points: by.map((a, i) => {
|
|
311
|
+
const n = a.calls;
|
|
312
|
+
const o = {
|
|
313
|
+
t: b.start + i * b.size, count: n, rpm: round(n / (b.size / 60000)), failed: a.failed, s2xx: n - a.s4xx - a.s5xx, s4xx: a.s4xx, s5xx: a.s5xx,
|
|
314
|
+
avg: meanOf(a), p95: round(a.p95), p50: round(a.p50), failureRate: n ? round0((a.failed / n) * 100) : 0,
|
|
315
|
+
};
|
|
316
|
+
if (qw) {
|
|
317
|
+
o.queueWaitAvg = meanOf(qw[i]);
|
|
318
|
+
o.queueWaitP95 = round(qw[i].p95);
|
|
319
|
+
}
|
|
320
|
+
return o;
|
|
321
|
+
}),
|
|
322
|
+
};
|
|
323
|
+
}
|
|
76
324
|
function buckets(w, target) {
|
|
77
325
|
const size = bucketFor(w.from, w.to, target);
|
|
78
326
|
const start = Math.floor(w.from / size) * size;
|
|
@@ -124,14 +372,14 @@ export function meta(db) {
|
|
|
124
372
|
id: s.id, firstSeen: s.first_seen, lastSeen: s.last_seen, environment: s.environment, sdk: s.sdk,
|
|
125
373
|
node: s.node, frameworks: parse(s.frameworks, []), instances: Object.keys(parse(s.instances, {})),
|
|
126
374
|
}));
|
|
127
|
-
const versions = db.all('SELECT service, version,
|
|
375
|
+
const versions = db.all('SELECT service, version, first, last FROM trace_versions ORDER BY first, service, version');
|
|
128
376
|
const counts = db.get('SELECT (SELECT COUNT(*) FROM traces) traces, (SELECT COUNT(*) FROM errors) errors, (SELECT COUNT(*) FROM logs) logs');
|
|
129
377
|
const unreadAlerts = db.get('SELECT COUNT(*) n FROM alert_events WHERE read = 0 AND state = ?', 'firing').n;
|
|
130
378
|
const openIssues = db.get("SELECT COUNT(*) n FROM issues WHERE status NOT IN ('closed','resolved')").n;
|
|
131
379
|
// Environments seen, most recent first, with the apps that report from each.
|
|
132
380
|
const envRows = db.all('SELECT service, env, last_seen FROM service_envs ORDER BY last_seen DESC');
|
|
133
381
|
const environments = [...new Set(envRows.map((r) => r.env))].map((env) => ({
|
|
134
|
-
id: env, services: envRows.filter((r) => r.env === env).map((r) => r.service), lastSeen:
|
|
382
|
+
id: env, services: envRows.filter((r) => r.env === env).map((r) => r.service), lastSeen: maxOf(envRows.filter((r) => r.env === env).map((r) => r.last_seen)),
|
|
135
383
|
}));
|
|
136
384
|
return { version: VERSION, services, environments, versions, counts, unreadAlerts, openIssues, openSecrets: openSecretCount(db), now: Date.now() };
|
|
137
385
|
}
|
|
@@ -145,23 +393,26 @@ export function overview(db, w) {
|
|
|
145
393
|
if (split != null)
|
|
146
394
|
return overviewFromHistory(db, w, split);
|
|
147
395
|
const s = scope(w);
|
|
148
|
-
const http = db.all(`SELECT ts, duration, failed, status FROM traces WHERE kind = 'http' AND ${s.sql}`, ...s.args);
|
|
149
396
|
const ps = scope({ ...w, from: w.from - (w.to - w.from), to: w.from });
|
|
150
|
-
const prev = db.all(`SELECT ts, duration, failed, status FROM traces WHERE kind = 'http' AND ${ps.sql}`, ...ps.args);
|
|
151
|
-
const jobs = db.all(`SELECT ts, duration, failed, queue_wait, status FROM traces WHERE kind = 'job' AND ${s.sql}`, ...s.args);
|
|
152
397
|
const minutes = (w.to - w.from) / 60000;
|
|
153
|
-
|
|
154
|
-
const
|
|
155
|
-
const
|
|
398
|
+
// One read of the window's requests answers the totals, the chart and the route tables.
|
|
399
|
+
const b = scopeBase(w);
|
|
400
|
+
const http = readWindow(db, `kind = 'http'${b.sql}`, b.args, w.from, w.to, { ops: true });
|
|
401
|
+
const curAgg = groupAgg(http, 1, allRows)[0];
|
|
402
|
+
const cur = statsOf(curAgg);
|
|
403
|
+
const pre = statsOf(groupAgg(readWindow(db, `kind = 'http'${b.sql}`, b.args, w.from - (w.to - w.from), w.from), 1, allRows)[0]);
|
|
404
|
+
const jobCols = readWindow(db, `kind = 'job'${b.sql}`, b.args, w.from, w.to, { queueWait: true });
|
|
405
|
+
const jobs = statsOf(groupAgg(jobCols, 1, allRows)[0]);
|
|
406
|
+
const ops = operationsOf(db, http, w, 'http');
|
|
156
407
|
const errorCount = db.get(`SELECT COUNT(*) n FROM errors WHERE handled = 0 AND ${s.sql}`, ...s.args).n;
|
|
157
|
-
const users = db.get(`SELECT COUNT(DISTINCT user_id) n FROM traces WHERE user_id IS NOT NULL AND ${s.sql}`, ...s.args).n;
|
|
408
|
+
const users = db.get(`SELECT COUNT(DISTINCT user_id) n FROM traces INDEXED BY traces_window WHERE kind IN ('http', 'job') AND user_id IS NOT NULL AND ${s.sql}`, ...s.args).n;
|
|
158
409
|
const logCounts = db.all(`SELECT level, COUNT(*) n FROM logs WHERE ${s.sql} GROUP BY level`, ...s.args);
|
|
159
410
|
const runtimeRows = db.all(`SELECT r.* FROM runtime r JOIN (SELECT service, instance, MAX(ts) mts FROM runtime WHERE ${s.sql} GROUP BY service, instance) m
|
|
160
411
|
ON r.service = m.service AND r.instance = m.instance AND r.ts = m.mts`, ...s.args);
|
|
161
412
|
const firing = db.all("SELECT id, name, type, last_value, last_change FROM alert_rules WHERE state = 'firing' AND enabled = 1");
|
|
162
|
-
const s4 =
|
|
163
|
-
const httpSeries =
|
|
164
|
-
const jobSeries =
|
|
413
|
+
const s4 = curAgg.s4xx;
|
|
414
|
+
const httpSeries = seriesOf(http, w);
|
|
415
|
+
const jobSeries = seriesOf(jobCols, w, 'job');
|
|
165
416
|
// The previous window only counts when raw data really covers it; otherwise there is nothing to compare.
|
|
166
417
|
const oldest = rawOldest(db);
|
|
167
418
|
const prevCovered = oldest != null && oldest <= w.from - (w.to - w.from) + 60000;
|
|
@@ -172,7 +423,7 @@ export function overview(db, w) {
|
|
|
172
423
|
prev: prevCovered ? { requests: pre.calls, failureRate: pre.failureRate, avg: pre.avg, p95: pre.p95 } : null,
|
|
173
424
|
/** The same window on earlier weeks (or days): what is usual, from long-term history. */
|
|
174
425
|
usual: w.env ? null : usualWindow(db, { series: 'op', kind: 'http', service: w.service }, w.from, w.to),
|
|
175
|
-
jobs: jobs.
|
|
426
|
+
jobs: jobs.calls, jobsFailed: jobs.failed,
|
|
176
427
|
},
|
|
177
428
|
series: httpSeries,
|
|
178
429
|
/** Normal range per bucket (same hour on earlier weeks/days), or null without enough history. */
|
|
@@ -230,54 +481,129 @@ function secretSummary(db, w) {
|
|
|
230
481
|
return { open: open.length, inWindow: groups.reduce((a, g) => a + g.inWindow, 0), top: open.slice(0, 3) };
|
|
231
482
|
}
|
|
232
483
|
// ------------------------------------------------------------------ operations
|
|
484
|
+
/** GraphQL requests are stored as requests whose method is the operation type. */
|
|
485
|
+
export const GRAPHQL_METHODS = ['QUERY', 'MUTATION', 'SUBSCRIPTION'];
|
|
486
|
+
export const isGraphqlMethod = (m) => !!m && GRAPHQL_METHODS.includes(m);
|
|
487
|
+
/** Requests over one protocol: HTTP routes, or GraphQL operations. */
|
|
488
|
+
export function operationsBy(db, w, kind, protocol) {
|
|
489
|
+
const rows = operations(db, w, kind);
|
|
490
|
+
if (kind !== 'http' || (protocol !== 'graphql' && protocol !== 'http'))
|
|
491
|
+
return rows;
|
|
492
|
+
const method = (o) => o.method || o.name.split(' ')[0];
|
|
493
|
+
return rows.filter((o) => isGraphqlMethod(method(o)) === (protocol === 'graphql'));
|
|
494
|
+
}
|
|
495
|
+
/** Throughput and latency for one protocol's requests (the GraphQL tab's charts). */
|
|
496
|
+
export function protocolSeries(db, w, protocol) {
|
|
497
|
+
const s = scope(w);
|
|
498
|
+
const gql = protocol === 'graphql';
|
|
499
|
+
const b = scopeBase(w);
|
|
500
|
+
return seriesOf(readWindow(db, `kind = 'http'${b.sql} AND ${gql ? '' : 'NOT '}COALESCE(method, '') IN (${GRAPHQL_METHODS.map(() => '?').join(', ')})`, [...b.args, ...GRAPHQL_METHODS], w.from, w.to), w);
|
|
501
|
+
}
|
|
233
502
|
export function operations(db, w, kind = 'http') {
|
|
234
503
|
// Older than raw retention: the long-term roll-ups answer (latency within ~5%), raw data for the rest.
|
|
235
504
|
const split = histSplit(db, w);
|
|
236
505
|
if (split != null)
|
|
237
506
|
return hybridOperations(db, w, kind, split);
|
|
238
507
|
const s = scope(w);
|
|
239
|
-
const
|
|
508
|
+
const b = scopeBase(w);
|
|
509
|
+
return operationsOf(db, readWindow(db, `kind = ?${b.sql}`, [kind, ...b.args], w.from, w.to, { ops: true, queueWait: kind === 'job' }), w, kind);
|
|
510
|
+
}
|
|
511
|
+
/**
|
|
512
|
+
* Own and total time per span name for one route: whole minutes from the per-minute totals kept at ingest,
|
|
513
|
+
* the window's partial first and last minute from the spans themselves. Exact, and the same as summing spans.
|
|
514
|
+
*/
|
|
515
|
+
function spanBreakdown(db, w, name, service) {
|
|
516
|
+
const M = 60000;
|
|
517
|
+
const lo = Math.ceil(w.from / M) * M;
|
|
518
|
+
const hi = Math.floor((w.to + 1) / M) * M; // minutes [lo, hi) lie wholly inside [from, to]
|
|
519
|
+
const filt = (alias, env) => `${w.service ? ` AND ${alias}service = ?` : ''}${service ? ` AND ${alias}service = ?` : ''}${w.env ? ` AND ${env} = ?` : ''}`;
|
|
520
|
+
const fargs = [...(w.service ? [w.service] : []), ...(service ? [service] : []), ...(w.env ? [w.env] : [])];
|
|
521
|
+
const acc = new Map();
|
|
522
|
+
const add = (r) => {
|
|
523
|
+
const k = `${r.name}\u0000${r.kind}`;
|
|
524
|
+
const x = acc.get(k);
|
|
525
|
+
if (x) {
|
|
526
|
+
x.calls += r.calls;
|
|
527
|
+
x.execs += r.execs;
|
|
528
|
+
x.self += r.self;
|
|
529
|
+
x.total += r.total;
|
|
530
|
+
x.traces += r.traces;
|
|
531
|
+
}
|
|
532
|
+
else
|
|
533
|
+
acc.set(k, { ...r });
|
|
534
|
+
};
|
|
535
|
+
const raw = (from, to, upTo) => {
|
|
536
|
+
for (const r of db.all(`SELECT name, kind, COUNT(*) calls, SUM(count) execs, SUM(CAST(ROUND(self * 1000) AS INTEGER)) self, SUM(CAST(ROUND(duration * 1000) AS INTEGER)) total,
|
|
537
|
+
COUNT(DISTINCT trace_row) traces FROM spans WHERE operation = ? AND ts >= ? AND ts ${upTo} ?${filt('', 'env')} GROUP BY name, kind`, name, from, to, ...fargs))
|
|
538
|
+
add(r);
|
|
539
|
+
};
|
|
540
|
+
if (lo < hi) {
|
|
541
|
+
for (const r of db.all(`SELECT name, kind, SUM(calls) calls, SUM(execs) execs, SUM(self_us) self, SUM(total_us) total, SUM(traces) traces
|
|
542
|
+
FROM span_minutes WHERE operation = ? AND t >= ? AND t < ?${filt('', 'env')} GROUP BY name, kind`, name, lo, hi, ...fargs))
|
|
543
|
+
add(r);
|
|
544
|
+
if (w.from < lo)
|
|
545
|
+
raw(w.from, lo, '<');
|
|
546
|
+
if (hi <= w.to)
|
|
547
|
+
raw(hi, w.to, '<=');
|
|
548
|
+
}
|
|
549
|
+
else
|
|
550
|
+
raw(w.from, w.to, '<=');
|
|
551
|
+
return [...acc.values()].map((b) => ({ ...b, self: b.self / 1000, total: b.total / 1000 }))
|
|
552
|
+
.sort((a, b) => b.self - a.self || cmp(a.name, b.name) || cmp(a.kind, b.kind)).slice(0, 15);
|
|
553
|
+
}
|
|
554
|
+
/** Per-route numbers from a window's columns. */
|
|
555
|
+
function operationsOf(db, c, w, kind) {
|
|
240
556
|
const minutes = (w.to - w.from) / 60000;
|
|
557
|
+
const op = c.op;
|
|
558
|
+
const lat = groupAgg(c, c.ops.length, (i) => op[i]);
|
|
559
|
+
const qw = kind === 'job' && c.qw ? groupAgg(c, c.ops.length, (i) => op[i], c.qw) : null;
|
|
241
560
|
const b = buckets(w, 24);
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
const
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
561
|
+
const spark = c.ops.map(() => new Array(b.n).fill(0));
|
|
562
|
+
for (let i = 0; i < c.n; i++) {
|
|
563
|
+
const k = b.index(c.ts[i]);
|
|
564
|
+
if (k >= 0)
|
|
565
|
+
spark[op[i]][k]++;
|
|
566
|
+
}
|
|
567
|
+
// Method, route and queue: the route's first row in the window, as before.
|
|
568
|
+
const env = w.env ? ' AND env = ?' : '';
|
|
569
|
+
return c.ops.map(({ service, name }, k) => {
|
|
570
|
+
const meta = db.get(`SELECT method, route, queue FROM traces WHERE name = ? AND kind = ? AND service = ? AND ts >= ? AND ts <= ?${env} ORDER BY ts LIMIT 1`, name, kind, service, w.from, w.to, ...(w.env ? [w.env] : []));
|
|
571
|
+
const a = lat[k];
|
|
572
|
+
const st = statsOf(a);
|
|
251
573
|
const out = {
|
|
252
|
-
service
|
|
253
|
-
rpm: round(st.calls / minutes), lastSeen:
|
|
254
|
-
s4xx: g.filter((r) => (r.status ?? 0) >= 400 && (r.status ?? 0) < 500).length,
|
|
574
|
+
service, name, method: meta.method, route: meta.route, ...st,
|
|
575
|
+
rpm: round(st.calls / minutes), lastSeen: a.last, spark: spark[k], s4xx: a.s4xx,
|
|
255
576
|
};
|
|
256
|
-
if (
|
|
257
|
-
|
|
258
|
-
out.
|
|
259
|
-
out.
|
|
260
|
-
out.queueWaitP95 = round(percentile(qw, 95));
|
|
577
|
+
if (qw) {
|
|
578
|
+
out.queue = meta.queue;
|
|
579
|
+
out.queueWaitAvg = meanOf(qw[k]);
|
|
580
|
+
out.queueWaitP95 = round(qw[k].p95);
|
|
261
581
|
}
|
|
262
582
|
return out;
|
|
263
|
-
}).sort((
|
|
583
|
+
}).sort((x, y) => y.calls - x.calls || cmp(x.service, y.service) || cmp(x.name, y.name));
|
|
264
584
|
}
|
|
265
585
|
export function operation(db, w, { kind = 'http', name, service, sort = 'recent', filter = 'all', limit = 50, offset = 0 }) {
|
|
266
586
|
const s = scope(w);
|
|
267
587
|
const svcCond = service ? ' AND service = ?' : '';
|
|
268
588
|
const svcArgs = service ? [service] : [];
|
|
269
|
-
const
|
|
270
|
-
const
|
|
589
|
+
const ob = scopeBase(w);
|
|
590
|
+
const cols = readWindow(db, `kind = ? AND name = ?${ob.sql}${svcCond}`, [kind, name, ...ob.args, ...svcArgs], w.from, w.to, { queueWait: kind === 'job' });
|
|
591
|
+
const st = statsOf(groupAgg(cols, 1, allRows)[0]);
|
|
271
592
|
const statuses = {};
|
|
272
|
-
for (
|
|
273
|
-
const k =
|
|
593
|
+
for (let i = 0; i < cols.n; i++) {
|
|
594
|
+
const k = cols.status[i] < 0 ? 'none' : String(cols.status[i]);
|
|
274
595
|
statuses[k] = (statuses[k] || 0) + 1;
|
|
275
596
|
}
|
|
276
597
|
// latency histogram (log buckets)
|
|
277
598
|
const edges = [1, 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000, Infinity];
|
|
278
599
|
const hist = edges.map((e, i) => ({ le: e, label: i === edges.length - 1 ? `>${edges[i - 1]}ms` : `≤${e}ms`, count: 0 }));
|
|
279
|
-
for (
|
|
280
|
-
|
|
600
|
+
for (let i = 0; i < cols.n; i++) {
|
|
601
|
+
const d = cols.d[i];
|
|
602
|
+
let k = 0;
|
|
603
|
+
while (d > edges[k])
|
|
604
|
+
k++;
|
|
605
|
+
hist[k].count++;
|
|
606
|
+
}
|
|
281
607
|
while (hist.length > 1 && hist[hist.length - 1].count === 0)
|
|
282
608
|
hist.pop();
|
|
283
609
|
let where = `kind = ? AND name = ? AND ${s.sql}${svcCond}`;
|
|
@@ -291,18 +617,16 @@ export function operation(db, w, { kind = 'http', name, service, sort = 'recent'
|
|
|
291
617
|
const order = sort === 'slowest' ? 'duration DESC' : sort === 'fastest' ? 'duration ASC' : 'ts DESC';
|
|
292
618
|
const executions = db.all(`SELECT id, ts, duration, failed, status, user_id, trace_id, version, instance, error_class, error_message, queue_wait, attempt, sampled
|
|
293
619
|
FROM traces WHERE ${where} ORDER BY ${order} LIMIT ? OFFSET ?`, ...args, Number(limit), Number(offset));
|
|
294
|
-
const total = db.get(`SELECT COUNT(*) n FROM traces WHERE ${where}`, ...args).n;
|
|
620
|
+
const total = db.get(`SELECT COUNT(*) n FROM traces INDEXED BY traces_window WHERE ${where}`, ...args).n;
|
|
295
621
|
// Where the time goes for this operation (own time per span name)
|
|
296
|
-
const
|
|
297
|
-
const
|
|
298
|
-
FROM spans s WHERE s.operation = ? AND ${ss.sql}${service ? ' AND s.service = ?' : ''} GROUP BY s.name, s.kind ORDER BY self DESC LIMIT 15`, name, ...ss.args, ...svcArgs);
|
|
299
|
-
const sampled = db.get(`SELECT COUNT(*) n FROM traces WHERE kind = ? AND name = ? AND sampled = 1 AND ${s.sql}${svcCond}`, kind, name, ...s.args, ...svcArgs).n || 1;
|
|
622
|
+
const breakdown = spanBreakdown(db, w, name, service);
|
|
623
|
+
const sampled = db.get(`SELECT COUNT(*) n FROM traces INDEXED BY traces_window WHERE kind = ? AND name = ? AND sampled = 1 AND ${s.sql}${svcCond}`, kind, name, ...s.args, ...svcArgs).n || 1;
|
|
300
624
|
const errors = db.all(`SELECT fingerprint, class, MAX(message) message, COUNT(*) n, MIN(handled) handled FROM errors WHERE operation = ? AND ${s.sql}${svcCond} GROUP BY fingerprint ORDER BY n DESC LIMIT 10`, name, ...s.args, ...svcArgs);
|
|
301
625
|
const hf = { series: 'op', kind, service, name };
|
|
302
626
|
// Past raw retention, the numbers and charts come from roll-ups; executions exist only for the raw period.
|
|
303
627
|
const split = histSplit(db, w);
|
|
304
628
|
const hw = { from: w.from, to: w.to, service: service || null, name };
|
|
305
|
-
const opSeries = split != null ? hybridSeries(db, hw, kind, split) :
|
|
629
|
+
const opSeries = split != null ? hybridSeries(db, hw, kind, split) : seriesOf(cols, w, kind);
|
|
306
630
|
const fullStats = split != null ? hybridTotals(db, hw, kind, split) : st;
|
|
307
631
|
return {
|
|
308
632
|
name, kind, service, stats: { ...fullStats, rpm: round(fullStats.calls / ((w.to - w.from) / 60000)) }, statuses, histogram: hist,
|
|
@@ -323,7 +647,7 @@ function rowToExecution(r) {
|
|
|
323
647
|
instance: r.instance, kind: r.kind, name: r.name, method: r.method, route: r.route, path: r.path, status: r.status,
|
|
324
648
|
duration: r.duration, ts: r.ts, userId: r.user_id, failed: !!r.failed, errorClass: r.error_class, fingerprint: r.fingerprint,
|
|
325
649
|
errorMessage: r.error_message, queue: r.queue, queueWait: r.queue_wait, attempt: r.attempt, sampled: !!r.sampled,
|
|
326
|
-
tags: data.tags || null, capture: data.capture || null, spans: data.spans || [],
|
|
650
|
+
tags: data.tags || null, capture: data.capture || null, spans: data.spans || [], graphql: data.graphql || null,
|
|
327
651
|
};
|
|
328
652
|
}
|
|
329
653
|
/** Merge every service's part of a distributed trace into one waterfall relative to `base`. */
|
|
@@ -357,7 +681,7 @@ function mergeTrace(db, base) {
|
|
|
357
681
|
if (p && p.service !== s.service)
|
|
358
682
|
p.self = Math.max(0, p.self - Math.min(s.duration, p.duration));
|
|
359
683
|
}
|
|
360
|
-
const t0 =
|
|
684
|
+
const t0 = minOf(spans.map((s) => s.start));
|
|
361
685
|
for (const s of spans)
|
|
362
686
|
s.start -= t0;
|
|
363
687
|
return {
|
|
@@ -374,9 +698,15 @@ export function execution(db, id) {
|
|
|
374
698
|
const waterfall = mergeTrace(db, ex);
|
|
375
699
|
const errors = db.all('SELECT * FROM errors WHERE trace_id = ? ORDER BY ts LIMIT 20', ex.traceId).map(errorRow);
|
|
376
700
|
const logs = db.all('SELECT * FROM logs WHERE trace_id = ? ORDER BY ts, id LIMIT 500', ex.traceId).map(logRow);
|
|
377
|
-
const
|
|
378
|
-
|
|
379
|
-
const
|
|
701
|
+
const baseWhere = 'kind = ? AND name = ? AND service = ? AND ts BETWEEN ? AND ?';
|
|
702
|
+
const baseArgs = [ex.kind, ex.name, ex.service, ex.ts - 86400e3, ex.ts + 3600e3];
|
|
703
|
+
const baseCols = readColumns(db, baseWhere, baseArgs);
|
|
704
|
+
const base = baseCols.n ? groupAgg(baseCols, 1, allRows)[0] : undefined;
|
|
705
|
+
let faster = 0;
|
|
706
|
+
for (let i = 0; i < baseCols.n; i++)
|
|
707
|
+
if (baseCols.d[i] < ex.duration)
|
|
708
|
+
faster++;
|
|
709
|
+
const rank = base ? round((faster / base.calls) * 100, 0) : null;
|
|
380
710
|
const rt = ex.instance ? db.get('SELECT * FROM runtime WHERE instance = ? AND service = ? ORDER BY ABS(ts - ?) LIMIT 1', ex.instance, ex.service, ex.ts) : undefined;
|
|
381
711
|
const rtBase = ex.instance ? db.get('SELECT AVG(cpu) cpu, AVG(el_p99) el, AVG(rss) rss FROM runtime WHERE instance = ? AND ts BETWEEN ? AND ?', ex.instance, ex.ts - 3600e3, ex.ts) : undefined;
|
|
382
712
|
const usual = baseline(db, { series: 'op', kind: ex.kind, service: ex.service, name: ex.name }, ex.ts);
|
|
@@ -386,7 +716,7 @@ export function execution(db, id) {
|
|
|
386
716
|
waterfall,
|
|
387
717
|
errors,
|
|
388
718
|
logs,
|
|
389
|
-
baseline: { samples: base
|
|
719
|
+
baseline: { samples: base?.calls ?? 0, avg: meanOf(base), p50: round(base?.p50), p95: round(base?.p95), slowerThanPct: rank },
|
|
390
720
|
/** Normal for this route at this hour, learned from earlier weeks; `vsUsualP95` is duration ÷ usual p95. */
|
|
391
721
|
usual: usual.basis ? { ...usual, vsUsualP95: usual.p95?.expected ? round(ex.duration / usual.p95.expected) : null } : null,
|
|
392
722
|
host: rt ? {
|
|
@@ -394,7 +724,7 @@ export function execution(db, id) {
|
|
|
394
724
|
baseline: rtBase ? { cpu: round(rtBase.cpu), elP99: round(rtBase.el), rss: rtBase.rss } : null,
|
|
395
725
|
} : null,
|
|
396
726
|
issue: issue || null,
|
|
397
|
-
worthInvestigating: ex.failed || (rank != null && rank >= 95 && base
|
|
727
|
+
worthInvestigating: ex.failed || (rank != null && rank >= 95 && (base?.calls ?? 0) >= 20) || errors.length > 0,
|
|
398
728
|
};
|
|
399
729
|
}
|
|
400
730
|
export function traceById(db, traceId) {
|
|
@@ -581,8 +911,8 @@ export function queries(db, w) {
|
|
|
581
911
|
const first = g[0];
|
|
582
912
|
return {
|
|
583
913
|
service: first.service, system: first.system, name: first.name, statement: first.statement || first.name, calls, avg: round(avg(d)), p95: round(percentile(d, 95)),
|
|
584
|
-
total: round0(sum(d) * (calls / d.length), 0), traces: perList.length, perRequest: round(avg(perList), 1), maxPerRequest:
|
|
585
|
-
nPlusOne:
|
|
914
|
+
total: round0(sum(d) * (calls / d.length), 0), traces: perList.length, perRequest: round(avg(perList), 1), maxPerRequest: maxOf(perList),
|
|
915
|
+
nPlusOne: maxOf(perList) >= 10,
|
|
586
916
|
};
|
|
587
917
|
}).sort((a, b) => b.total - a.total).slice(0, 200);
|
|
588
918
|
}
|
|
@@ -605,7 +935,7 @@ export function users(db, w, { groupBy } = {}) {
|
|
|
605
935
|
return [...group(list, (u) => (u.userId.includes('@') ? u.userId.split('@')[1] : '(no domain)')).entries()].map(([customer, us]) => {
|
|
606
936
|
const calls = sum(us.map((u) => u.calls));
|
|
607
937
|
const failed = sum(us.map((u) => u.failed));
|
|
608
|
-
return { customer, users: us.length, calls, failed, lastSeen:
|
|
938
|
+
return { customer, users: us.length, calls, failed, lastSeen: maxOf(us.map((u) => u.lastSeen)), failureRate: round((failed / calls) * 100) };
|
|
609
939
|
}).sort((a, b) => b.calls - a.calls);
|
|
610
940
|
}
|
|
611
941
|
export function user(db, w, id) {
|
|
@@ -701,18 +1031,42 @@ function twoPropZ(f1, n1, f2, n2) {
|
|
|
701
1031
|
const se = Math.sqrt(p * (1 - p) * (1 / n1 + 1 / n2));
|
|
702
1032
|
return se ? (f2 / n2 - f1 / n1) / se : 0;
|
|
703
1033
|
}
|
|
1034
|
+
/**
|
|
1035
|
+
* A version's numbers only change when executions arrive for it or retention takes some away, and both move its
|
|
1036
|
+
* row in trace_versions: kept until they do. (The exact p95 needs a pass over the version's requests.)
|
|
1037
|
+
*/
|
|
1038
|
+
const versionCaches = new WeakMap();
|
|
704
1039
|
function versionStats(db, service, version) {
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
const
|
|
709
|
-
const
|
|
1040
|
+
let cache = versionCaches.get(db.raw);
|
|
1041
|
+
if (!cache)
|
|
1042
|
+
versionCaches.set(db.raw, (cache = new Map()));
|
|
1043
|
+
const g = db.get('SELECT first, last, http_n, http_first, http_last FROM trace_versions WHERE service = ? AND version IS ?', service, version);
|
|
1044
|
+
const gen = g ? `${g.first}|${g.last}|${g.http_n}|${g.http_first}|${g.http_last}` : '';
|
|
1045
|
+
const key = `${service}\u0000${version}`;
|
|
1046
|
+
const hit = cache.get(key);
|
|
1047
|
+
if (hit && gen && hit.gen === gen)
|
|
1048
|
+
return hit.stats;
|
|
1049
|
+
const stats = computeVersionStats(db, service, version);
|
|
1050
|
+
if (cache.size > 500)
|
|
1051
|
+
cache.clear();
|
|
1052
|
+
if (gen)
|
|
1053
|
+
cache.set(key, { gen, stats });
|
|
1054
|
+
return stats;
|
|
1055
|
+
}
|
|
1056
|
+
function computeVersionStats(db, service, version) {
|
|
1057
|
+
const where = "service = ? AND version IS ? AND kind = 'http'";
|
|
1058
|
+
const cols = readColumns(db, where, [service, version], { index: false });
|
|
1059
|
+
const a = cols.n ? groupAgg(cols, 1, allRows)[0] : undefined;
|
|
1060
|
+
const span = db.get(`SELECT MIN(ts) first, MAX(ts) last FROM traces WHERE ${where}`, service, version);
|
|
710
1061
|
const routes = new Map();
|
|
711
|
-
for (const r of
|
|
712
|
-
routes.set(r.name,
|
|
1062
|
+
for (const r of db.all(`SELECT name, COUNT(*) n FROM traces WHERE ${where} GROUP BY name`, service, version))
|
|
1063
|
+
routes.set(r.name, r.n);
|
|
1064
|
+
const n = a?.calls ?? 0;
|
|
1065
|
+
const s5 = a?.s5xx ?? 0;
|
|
1066
|
+
const { first, last } = span;
|
|
713
1067
|
return {
|
|
714
|
-
requests:
|
|
715
|
-
rpm: first && last && last > first ? round(
|
|
1068
|
+
requests: n, s5xx: s5, errorRate: n ? round((s5 / n) * 100) : 0, p95: round(a?.p95), avg: meanOf(a), first, last,
|
|
1069
|
+
rpm: first && last && last > first ? round(n / ((last - first) / 60000)) : null, routes,
|
|
716
1070
|
};
|
|
717
1071
|
}
|
|
718
1072
|
/** Runtime samples of a service while a version was serving (its first to last request). */
|
|
@@ -748,19 +1102,11 @@ function runtimeStats(db, service, from, to) {
|
|
|
748
1102
|
};
|
|
749
1103
|
}
|
|
750
1104
|
function spanProfile(db, service, version) {
|
|
751
|
-
|
|
752
|
-
const
|
|
1105
|
+
// Kept per version at ingest: calls and own time (in exact microseconds) per query, outbound host and method.
|
|
1106
|
+
const traceCount = db.get('SELECT sampled_n n FROM trace_versions WHERE service = ? AND version = ?', service, version)?.n || 1;
|
|
753
1107
|
const m = new Map();
|
|
754
|
-
for (const r of
|
|
755
|
-
|
|
756
|
-
const x = m.get(key) || { calls: 0, self: 0, perReq: 0, selfPerCall: 0 };
|
|
757
|
-
x.calls += r.calls;
|
|
758
|
-
x.self += r.self;
|
|
759
|
-
m.set(key, x);
|
|
760
|
-
}
|
|
761
|
-
for (const x of m.values()) {
|
|
762
|
-
x.perReq = x.calls / traceCount;
|
|
763
|
-
x.selfPerCall = x.self / Math.max(1, x.calls);
|
|
1108
|
+
for (const r of db.all('SELECT key, calls, self_us us FROM version_profile WHERE service = ? AND version = ? ORDER BY key', service, version)) {
|
|
1109
|
+
m.set(r.key, { calls: r.calls, self: r.us / 1000, perReq: r.calls / traceCount, selfPerCall: r.us / 1000 / Math.max(1, r.calls) });
|
|
764
1110
|
}
|
|
765
1111
|
return m;
|
|
766
1112
|
}
|
|
@@ -784,7 +1130,7 @@ export function releaseVerdict(db, service, version) {
|
|
|
784
1130
|
return out;
|
|
785
1131
|
}
|
|
786
1132
|
function computeVerdict(db, service, version) {
|
|
787
|
-
const versions = db.all(
|
|
1133
|
+
const versions = db.all('SELECT version, http_first first FROM trace_versions WHERE service = ? AND http_n > 0 ORDER BY http_first, version', service);
|
|
788
1134
|
const idx = versions.findIndex((v) => v.version === version);
|
|
789
1135
|
if (idx < 0)
|
|
790
1136
|
return null;
|
|
@@ -873,10 +1219,10 @@ function computeVerdict(db, service, version) {
|
|
|
873
1219
|
return { ...base, previousStats: stripRoutes(prev), runtime: rtB, previousRuntime: rtA, verdict, summary, evidence, changes: changes.slice(0, 40), z: round(z) };
|
|
874
1220
|
}
|
|
875
1221
|
export function releases(db, w) {
|
|
876
|
-
const services = db.all(`SELECT DISTINCT service FROM traces WHERE version IS NOT NULL${w.service ? ' AND service = ?' : ''}`, ...(w.service ? [w.service] : []));
|
|
877
1222
|
const out = [];
|
|
1223
|
+
const services = db.all(`SELECT DISTINCT service FROM trace_versions${w.service ? ' WHERE service = ?' : ''} ORDER BY service`, ...(w.service ? [w.service] : []));
|
|
878
1224
|
for (const { service } of services) {
|
|
879
|
-
const versions = db.all(
|
|
1225
|
+
const versions = db.all('SELECT version, http_first first, http_last last, http_n n FROM trace_versions WHERE service = ? AND http_n > 0 ORDER BY http_first DESC LIMIT 15', service);
|
|
880
1226
|
for (const v of versions) {
|
|
881
1227
|
const verdict = releaseVerdict(db, service, v.version);
|
|
882
1228
|
if (!verdict)
|