@watchtowerobservability/node 0.2.0 → 0.3.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.
Files changed (90) hide show
  1. package/README.md +186 -3
  2. package/dist/cjs/appsec.d.ts +95 -0
  3. package/dist/cjs/appsec.js +206 -0
  4. package/dist/cjs/index.d.ts +3 -0
  5. package/dist/cjs/index.js +14 -1
  6. package/dist/cjs/integrations/amqp.d.ts +13 -0
  7. package/dist/cjs/integrations/amqp.js +96 -0
  8. package/dist/cjs/integrations/bullmq.d.ts +19 -0
  9. package/dist/cjs/integrations/bullmq.js +83 -0
  10. package/dist/cjs/integrations/cache.d.ts +13 -0
  11. package/dist/cjs/integrations/cache.js +51 -0
  12. package/dist/cjs/integrations/db.d.ts +62 -0
  13. package/dist/cjs/integrations/db.js +288 -0
  14. package/dist/cjs/integrations/duckdb.d.ts +22 -0
  15. package/dist/cjs/integrations/duckdb.js +66 -0
  16. package/dist/cjs/integrations/graphile-worker.d.ts +22 -0
  17. package/dist/cjs/integrations/graphile-worker.js +50 -0
  18. package/dist/cjs/integrations/level.d.ts +18 -0
  19. package/dist/cjs/integrations/level.js +71 -0
  20. package/dist/cjs/integrations/lmdb.d.ts +26 -0
  21. package/dist/cjs/integrations/lmdb.js +78 -0
  22. package/dist/cjs/integrations/messaging.d.ts +17 -0
  23. package/dist/cjs/integrations/messaging.js +73 -0
  24. package/dist/cjs/integrations/mqtt.d.ts +7 -0
  25. package/dist/cjs/integrations/mqtt.js +41 -0
  26. package/dist/cjs/integrations/nats.d.ts +17 -0
  27. package/dist/cjs/integrations/nats.js +73 -0
  28. package/dist/cjs/integrations/orm.d.ts +47 -0
  29. package/dist/cjs/integrations/orm.js +93 -0
  30. package/dist/cjs/integrations/pg-boss.d.ts +8 -0
  31. package/dist/cjs/integrations/pg-boss.js +63 -0
  32. package/dist/cjs/integrations/pools.d.ts +41 -0
  33. package/dist/cjs/integrations/pools.js +94 -0
  34. package/dist/cjs/integrations/postgres.d.ts +12 -0
  35. package/dist/cjs/integrations/postgres.js +92 -0
  36. package/dist/cjs/integrations/sqlite.d.ts +36 -0
  37. package/dist/cjs/integrations/sqlite.js +125 -0
  38. package/dist/cjs/pprof.d.ts +6 -0
  39. package/dist/cjs/pprof.js +88 -0
  40. package/dist/cjs/profiling.d.ts +4 -1
  41. package/dist/cjs/profiling.js +18 -8
  42. package/dist/cjs/sql.d.ts +19 -0
  43. package/dist/cjs/sql.js +332 -0
  44. package/dist/cjs/test-plan.d.ts +10 -1
  45. package/dist/cjs/test-plan.js +91 -1
  46. package/dist/esm/appsec.d.ts +95 -0
  47. package/dist/esm/appsec.js +193 -0
  48. package/dist/esm/index.d.ts +3 -0
  49. package/dist/esm/index.js +14 -1
  50. package/dist/esm/integrations/amqp.d.ts +13 -0
  51. package/dist/esm/integrations/amqp.js +92 -0
  52. package/dist/esm/integrations/bullmq.d.ts +19 -0
  53. package/dist/esm/integrations/bullmq.js +79 -0
  54. package/dist/esm/integrations/cache.d.ts +13 -0
  55. package/dist/esm/integrations/cache.js +46 -0
  56. package/dist/esm/integrations/db.d.ts +62 -0
  57. package/dist/esm/integrations/db.js +239 -0
  58. package/dist/esm/integrations/duckdb.d.ts +22 -0
  59. package/dist/esm/integrations/duckdb.js +29 -0
  60. package/dist/esm/integrations/graphile-worker.d.ts +22 -0
  61. package/dist/esm/integrations/graphile-worker.js +46 -0
  62. package/dist/esm/integrations/level.d.ts +18 -0
  63. package/dist/esm/integrations/level.js +35 -0
  64. package/dist/esm/integrations/lmdb.d.ts +26 -0
  65. package/dist/esm/integrations/lmdb.js +42 -0
  66. package/dist/esm/integrations/messaging.d.ts +17 -0
  67. package/dist/esm/integrations/messaging.js +64 -0
  68. package/dist/esm/integrations/mqtt.d.ts +7 -0
  69. package/dist/esm/integrations/mqtt.js +38 -0
  70. package/dist/esm/integrations/nats.d.ts +17 -0
  71. package/dist/esm/integrations/nats.js +70 -0
  72. package/dist/esm/integrations/orm.d.ts +47 -0
  73. package/dist/esm/integrations/orm.js +86 -0
  74. package/dist/esm/integrations/pg-boss.d.ts +8 -0
  75. package/dist/esm/integrations/pg-boss.js +60 -0
  76. package/dist/esm/integrations/pools.d.ts +41 -0
  77. package/dist/esm/integrations/pools.js +89 -0
  78. package/dist/esm/integrations/postgres.d.ts +12 -0
  79. package/dist/esm/integrations/postgres.js +55 -0
  80. package/dist/esm/integrations/sqlite.d.ts +36 -0
  81. package/dist/esm/integrations/sqlite.js +84 -0
  82. package/dist/esm/pprof.d.ts +6 -0
  83. package/dist/esm/pprof.js +85 -0
  84. package/dist/esm/profiling.d.ts +4 -1
  85. package/dist/esm/profiling.js +17 -8
  86. package/dist/esm/sql.d.ts +19 -0
  87. package/dist/esm/sql.js +324 -0
  88. package/dist/esm/test-plan.d.ts +10 -1
  89. package/dist/esm/test-plan.js +89 -1
  90. package/package.json +161 -1
@@ -48,7 +48,7 @@ export interface ProfilerOptions {
48
48
  tags?: Record<string, string | undefined>;
49
49
  /** Sampling interval in microseconds (default 10 000 = 100 Hz). */
50
50
  intervalUs?: number;
51
- /** Upload period (default 15 000 ms). */
51
+ /** Upload period (default 60 000 ms). */
52
52
  periodMs?: number;
53
53
  /** Label `withSpan` code after `await` too (see [[enableAsyncSpanLabels]]). Default false. */
54
54
  asyncSpans?: boolean;
@@ -56,6 +56,9 @@ export interface ProfilerOptions {
56
56
  /** Root-first folded stacks with their sample counts; V8's synthetic `(root)` is left out. */
57
57
  export declare function foldProfile(p: CpuProfile, spans?: SpanInterval[]): Map<string, number>;
58
58
  export declare function foldedBody(counts: Map<string, number>): string;
59
+ /** `/v1/profiles?format=pprof&service=…&version=…` on the gateway. */
60
+ export declare function profilesUrl(endpoint: string, service: string, version?: string): string;
61
+ /** Pyroscope-compatible `/ingest` URL for folded uploads (kept for callers; the Profiler sends pprof). */
59
62
  export declare function ingestUrl(endpoint: string, service: string, tags: Record<string, string | undefined>, fromS: number, untilS: number, hz: number): string;
60
63
  export declare class Profiler {
61
64
  uploaded: number;
@@ -6,17 +6,19 @@ exports.withSpan = withSpan;
6
6
  exports.takeIntervals = takeIntervals;
7
7
  exports.foldProfile = foldProfile;
8
8
  exports.foldedBody = foldedBody;
9
+ exports.profilesUrl = profilesUrl;
9
10
  exports.ingestUrl = ingestUrl;
10
11
  exports.startProfiling = startProfiling;
11
12
  // Continuous CPU profiler on V8's own sampler (`node:inspector`, no native addon): every
12
- // `periodMs` the CPU profile is folded into stacks and posted to the gateway's
13
- // Pyroscope-compatible `/ingest` as `<service>.cpu` (format `folded`). Best effort: a failed
14
- // upload is counted and dropped. `withSpan` labels samples taken while its function runs, joined
13
+ // `periodMs` the CPU profile is folded into stacks, encoded as pprof and posted to the gateway's
14
+ // `/v1/profiles?format=pprof` with `service` and `version`. Best effort: a failed upload is
15
+ // counted and dropped. `withSpan` labels samples taken while its function runs, joined
15
16
  // on V8's sample timestamps, so a trace links to its exact profile.
16
17
  const node_async_hooks_1 = require("node:async_hooks");
17
18
  const node_inspector_1 = require("node:inspector");
18
19
  const node_v8_1 = require("node:v8");
19
20
  const api_1 = require("@opentelemetry/api");
21
+ const pprof_js_1 = require("./pprof.js");
20
22
  const intervals = [];
21
23
  const MAX_INTERVALS = 100_000;
22
24
  function nowUs() {
@@ -150,6 +152,14 @@ function foldedBody(counts) {
150
152
  out += `${k} ${n}\n`;
151
153
  return out;
152
154
  }
155
+ /** `/v1/profiles?format=pprof&service=…&version=…` on the gateway. */
156
+ function profilesUrl(endpoint, service, version) {
157
+ const q = new URLSearchParams({ format: 'pprof', service });
158
+ if (version)
159
+ q.set('version', version);
160
+ return `${endpoint.replace(/\/+$/, '')}/v1/profiles?${q}`;
161
+ }
162
+ /** Pyroscope-compatible `/ingest` URL for folded uploads (kept for callers; the Profiler sends pprof). */
153
163
  function ingestUrl(endpoint, service, tags, fromS, untilS, hz) {
154
164
  const labels = Object.entries(tags).filter(([, v]) => v).sort(([a], [b]) => a.localeCompare(b))
155
165
  .map(([k, v]) => `${k}=${v}`).join(',');
@@ -180,7 +190,7 @@ class Profiler {
180
190
  this.service = o.service ?? process.env.WT_SERVICE ?? 'app';
181
191
  this.tags = { env: process.env.WT_ENVIRONMENT, version: process.env.WT_RELEASE, ...o.tags };
182
192
  this.intervalUs = Math.min(Math.max(o.intervalUs ?? 10_000, 1_000), 1_000_000);
183
- this.periodMs = Math.max(o.periodMs ?? 15_000, 1_000);
193
+ this.periodMs = Math.max(o.periodMs ?? 60_000, 1_000);
184
194
  if (o.asyncSpans)
185
195
  enableAsyncSpanLabels();
186
196
  }
@@ -223,10 +233,10 @@ class Profiler {
223
233
  }
224
234
  async upload(counts, fromS, untilS) {
225
235
  try {
226
- const hz = Math.round(1_000_000 / this.intervalUs);
227
- const r = await fetch(ingestUrl(this.endpoint, this.service, this.tags, fromS, untilS, hz), {
228
- method: 'POST', body: foldedBody(counts),
229
- headers: { authorization: `Bearer ${this.key}`, 'content-type': 'text/plain' },
236
+ const body = (0, pprof_js_1.foldedToPprof)(counts, this.intervalUs * 1000, fromS * 1000, (untilS - fromS) * 1000);
237
+ const r = await fetch(profilesUrl(this.endpoint, this.service, this.tags.version), {
238
+ method: 'POST', body,
239
+ headers: { authorization: `Bearer ${this.key}`, 'content-type': 'application/octet-stream' },
230
240
  signal: AbortSignal.timeout(10_000),
231
241
  });
232
242
  if (r.ok)
@@ -0,0 +1,19 @@
1
+ /** Longest statement looked at, in UTF-8 bytes; the rest is dropped. */
2
+ export declare const MAX_STATEMENT_BYTES: number;
3
+ /**
4
+ * Literal-free statement text: strings, numbers and `$1` become `?`, comments go, whitespace
5
+ * collapses, `IN (?, ?)` becomes `IN (?+)`. `pg` keeps short `"identifiers"` and the `#` operator.
6
+ */
7
+ export declare function normalizeSql(stmt: string, pg?: boolean): string;
8
+ /** `[normalized text, decimal xxh3_64 of its UTF-8 bytes]`: the `db.query.hash` spans carry. */
9
+ export declare function normalizeAndHash(stmt: string, pg?: boolean): [string, string];
10
+ /** Systems whose dialect keeps `"identifier"` quotes and the `#` operator (as the worker decides). */
11
+ export declare const isPgDialect: (system: string | undefined) => system is "postgresql" | "cockroachdb" | "redshift";
12
+ /** `SELECT users`-style summary: the operation and first table, never values or the statement text. */
13
+ export declare function querySummary(text: string | null | undefined, fallback?: string): {
14
+ operation?: string;
15
+ target?: string;
16
+ summary: string;
17
+ };
18
+ /** XXH3_64bits(input), seed 0. */
19
+ export declare function xxh3(p: Uint8Array): bigint;
@@ -0,0 +1,332 @@
1
+ "use strict";
2
+ // Shared SQL normaliser and statement hash. A byte-for-byte port of the worker's
3
+ // services/worker/src/sqlnorm.rs (`normalize_with`, xxh3_64), so `db.query.hash` from the SDK equals
4
+ // the `wt.db.query_hash` the server stamps. Algorithm: docs/product/instrumentation/sql-normalization.md;
5
+ // vectors: testdata/sql_normalize_vectors.json. Change both sides together.
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.isPgDialect = exports.MAX_STATEMENT_BYTES = void 0;
8
+ exports.normalizeSql = normalizeSql;
9
+ exports.normalizeAndHash = normalizeAndHash;
10
+ exports.querySummary = querySummary;
11
+ exports.xxh3 = xxh3;
12
+ /** Longest statement looked at, in UTF-8 bytes; the rest is dropped. */
13
+ exports.MAX_STATEMENT_BYTES = 8 * 1024;
14
+ const MAX_DOLLAR_TAG = 32;
15
+ const ALNUM = /^[\p{Alphabetic}\p{N}]$/u; // Rust char::is_alphanumeric
16
+ const SPACE = /^\p{White_Space}$/u; // Rust char::is_whitespace
17
+ const isAlnum = (c) => ALNUM.test(c);
18
+ const isWord = (c) => c === '_' || c === '$' || ALNUM.test(c);
19
+ const isDigit = (c) => c !== undefined && c >= '0' && c <= '9';
20
+ /** Code points of the first 8 KiB of UTF-8, cut back to a whole character. */
21
+ function cap(s) {
22
+ const out = [];
23
+ let bytes = 0;
24
+ for (const c of s) {
25
+ const cp = c.codePointAt(0);
26
+ bytes += cp < 0x80 ? 1 : cp < 0x800 ? 2 : cp < 0x10000 ? 3 : 4;
27
+ if (bytes > exports.MAX_STATEMENT_BYTES)
28
+ break;
29
+ out.push(c);
30
+ }
31
+ return out;
32
+ }
33
+ const startsWith = (cs, at, tag) => tag.every((t, k) => cs[at + k] === t);
34
+ function dollarQuoted(cs, i) {
35
+ let tagLen = -1;
36
+ for (let k = 0; k < MAX_DOLLAR_TAG + 1 && i + 1 + k < cs.length; k++) {
37
+ if (cs[i + 1 + k] === '$') {
38
+ tagLen = k;
39
+ break;
40
+ }
41
+ }
42
+ if (tagLen < 0)
43
+ return undefined;
44
+ const tag = cs.slice(i, i + tagLen + 2);
45
+ if (!tag.slice(1, tagLen + 1).every((c) => isAlnum(c) || c === '_') || isDigit(tag[1]))
46
+ return undefined;
47
+ for (let j = i + tag.length; j < cs.length; j++)
48
+ if (startsWith(cs, j, tag))
49
+ return j + tag.length;
50
+ return cs.length;
51
+ }
52
+ function token(cs, i, pg, out) {
53
+ const c = cs[i];
54
+ if (c === "'") {
55
+ i++;
56
+ while (i < cs.length) {
57
+ if (cs[i] === "'") {
58
+ if (cs[i + 1] !== "'")
59
+ break;
60
+ i++;
61
+ }
62
+ i++;
63
+ }
64
+ out.push('?');
65
+ // a quote right after a backslash is ambiguous (MySQL escapes): mask to the end
66
+ return i < cs.length && cs[i - 1] === '\\' ? cs.length : i + 1;
67
+ }
68
+ if (c === '"' || c === '`') {
69
+ let close = cs.indexOf(c, i + 1);
70
+ if (close < 0)
71
+ close = cs.length;
72
+ const body = cs.slice(i + 1, close);
73
+ const ident = c === '"' && pg && close < cs.length && body.length >= 1 && body.length <= 64
74
+ && body.every((b) => isAlnum(b) || b === '_' || b === '$');
75
+ if (ident)
76
+ out.push(...cs.slice(i, close + 1));
77
+ else
78
+ out.push('?');
79
+ return close + 1;
80
+ }
81
+ if (c === '$') {
82
+ if (isDigit(cs[i + 1])) {
83
+ i++;
84
+ while (isDigit(cs[i]))
85
+ i++;
86
+ out.push('?');
87
+ return i;
88
+ }
89
+ const end = dollarQuoted(cs, i);
90
+ if (end !== undefined) {
91
+ out.push('?');
92
+ return end;
93
+ }
94
+ }
95
+ if (isDigit(c)) {
96
+ while (i < cs.length && (isWord(cs[i]) || cs[i] === '.'))
97
+ i++;
98
+ out.push('?');
99
+ }
100
+ else if (isWord(c)) {
101
+ while (i < cs.length && isWord(cs[i]))
102
+ out.push(cs[i++]);
103
+ }
104
+ else {
105
+ out.push(c);
106
+ i++;
107
+ }
108
+ return i;
109
+ }
110
+ const enc = new TextEncoder();
111
+ const dec = new TextDecoder();
112
+ const SP = 0x20;
113
+ /** After `in`: optional spaces, `(`, `?` items split by commas, `)`. Returns [open, end]. */
114
+ function inList(b, i) {
115
+ const skip = () => { while (b[i] === SP)
116
+ i++; };
117
+ skip();
118
+ const open = i;
119
+ if (b[i] !== 0x28)
120
+ return undefined;
121
+ i++;
122
+ for (;;) {
123
+ skip();
124
+ if (b[i] !== 0x3f)
125
+ return undefined;
126
+ i++;
127
+ skip();
128
+ if (b[i] === 0x2c)
129
+ i++;
130
+ else if (b[i] === 0x29)
131
+ return [open, i + 1];
132
+ else
133
+ return undefined;
134
+ }
135
+ }
136
+ /** `in (?, ?, ?)` -> `in (?+)`. On UTF-8 bytes like the Rust: the word check reads the previous byte as Latin-1. */
137
+ function collapseIn(s) {
138
+ const out = [];
139
+ let i = 0;
140
+ while (i < s.length) {
141
+ const atWord = i === 0 || !isWord(String.fromCharCode(s[i - 1]));
142
+ const span = atWord && i + 2 <= s.length && (s[i] | 0x20) === 0x69 && (s[i + 1] | 0x20) === 0x6e
143
+ ? inList(s, i + 2) : undefined;
144
+ if (span) {
145
+ for (let k = i; k < span[0]; k++)
146
+ out.push(s[k]);
147
+ out.push(0x28, 0x3f, 0x2b, 0x29);
148
+ i = span[1];
149
+ }
150
+ else {
151
+ out.push(s[i++]);
152
+ }
153
+ }
154
+ return Uint8Array.from(out);
155
+ }
156
+ function normalizeBytes(stmt, pg) {
157
+ const cs = cap(stmt);
158
+ const out = [];
159
+ let space = false;
160
+ let i = 0;
161
+ while (i < cs.length) {
162
+ const c = cs[i];
163
+ const next = cs[i + 1];
164
+ if (SPACE.test(c)) {
165
+ space = true;
166
+ i++;
167
+ }
168
+ else if (c === '/' && next === '*') {
169
+ let depth = 1;
170
+ i += 2;
171
+ while (i < cs.length && depth > 0) {
172
+ if (cs[i] === '/' && cs[i + 1] === '*') {
173
+ depth++;
174
+ i += 2;
175
+ }
176
+ else if (cs[i] === '*' && cs[i + 1] === '/') {
177
+ depth--;
178
+ i += 2;
179
+ }
180
+ else
181
+ i++;
182
+ }
183
+ space = true;
184
+ }
185
+ else if ((c === '-' && next === '-') || (c === '#' && !pg)) {
186
+ while (i < cs.length && cs[i] !== '\n')
187
+ i++;
188
+ space = true;
189
+ }
190
+ else {
191
+ if (space && out.length)
192
+ out.push(' ');
193
+ space = false;
194
+ i = token(cs, i, pg, out);
195
+ }
196
+ }
197
+ return collapseIn(enc.encode(out.join('')));
198
+ }
199
+ /**
200
+ * Literal-free statement text: strings, numbers and `$1` become `?`, comments go, whitespace
201
+ * collapses, `IN (?, ?)` becomes `IN (?+)`. `pg` keeps short `"identifiers"` and the `#` operator.
202
+ */
203
+ function normalizeSql(stmt, pg = false) {
204
+ return dec.decode(normalizeBytes(stmt, pg));
205
+ }
206
+ /** `[normalized text, decimal xxh3_64 of its UTF-8 bytes]`: the `db.query.hash` spans carry. */
207
+ function normalizeAndHash(stmt, pg = false) {
208
+ const b = normalizeBytes(stmt, pg);
209
+ return [dec.decode(b), xxh3(b).toString()];
210
+ }
211
+ /** Systems whose dialect keeps `"identifier"` quotes and the `#` operator (as the worker decides). */
212
+ const isPgDialect = (system) => system === 'postgresql' || system === 'cockroachdb' || system === 'redshift';
213
+ exports.isPgDialect = isPgDialect;
214
+ const OP = /^\s*(?:with\b[\s\S]*?\)\s*)?([a-z]+)/i;
215
+ const TARGET = /\b(?:from|into|update|join|table)\s+((?:"[^"]+"|[\w$]+)(?:\.(?:"[^"]+"|[\w$]+))?)/i;
216
+ /** `SELECT users`-style summary: the operation and first table, never values or the statement text. */
217
+ function querySummary(text, fallback = 'postgres') {
218
+ const operation = text?.match(OP)?.[1]?.toUpperCase();
219
+ const target = text?.match(TARGET)?.[1]?.replace(/"/g, '');
220
+ return { operation, target, summary: [operation ?? fallback, target].filter(Boolean).join(' ') };
221
+ }
222
+ // ---- XXH3-64, seed 0, default secret (port of the xxHash reference; BigInt is fine at <= 8 KiB) ----
223
+ const M64 = (1n << 64n) - 1n;
224
+ const P32_1 = 0x9e3779b1n, P32_2 = 0x85ebca77n, P32_3 = 0xc2b2ae3dn;
225
+ const P64_1 = 0x9e3779b185ebca87n, P64_2 = 0xc2b2ae3d27d4eb4fn, P64_3 = 0x165667b19e3779f9n;
226
+ const P64_4 = 0x85ebca77c2b2ae63n, P64_5 = 0x27d4eb2f165667c5n;
227
+ const SECRET = Uint8Array.from(Buffer.from('b8fe6c3923a44bbe7c01812cf721ad1cded46de9839097db7240a4a4b7b3671fcb79e64eccc0e578825ad07dccff7221'
228
+ + 'b8084674f743248ee03590e6813a264c3c2852bb91c300cb88d0658b1b532ea371644897a20df94e3819ef46a9deacd8'
229
+ + 'a8fa763fe39c343ff9dcbbc7c70b4f1d8a51e04bcdb45931c89f7ec9d9787364eac5ac8334d3ebc3c581a0fffa1363eb'
230
+ + '170ddd51b7f0da49d316552629d4689e2b16be587d47a1fc8ff8b8d17ad031ce45cb3a8f95160428afd7fbcabb4b407e', 'hex'));
231
+ const r64 = (b, o) => new DataView(b.buffer, b.byteOffset + o, 8).getBigUint64(0, true);
232
+ const r32 = (b, o) => BigInt(new DataView(b.buffer, b.byteOffset + o, 4).getUint32(0, true));
233
+ const mul = (a, b) => (a * b) & M64;
234
+ const add = (a, b) => (a + b) & M64;
235
+ const rotl = (x, r) => ((x << r) | (x >> (64n - r))) & M64;
236
+ const swap64 = (x) => {
237
+ let r = 0n;
238
+ for (let k = 0; k < 8; k++) {
239
+ r = (r << 8n) | (x & 0xffn);
240
+ x >>= 8n;
241
+ }
242
+ return r;
243
+ };
244
+ const fold = (a, b) => { const p = a * b; return (p ^ (p >> 64n)) & M64; };
245
+ const avalanche = (h) => { h ^= h >> 37n; h = mul(h, 0x165667919e3779f9n); return h ^ (h >> 32n); };
246
+ const avalanche64 = (h) => {
247
+ h ^= h >> 33n;
248
+ h = mul(h, P64_2);
249
+ h ^= h >> 29n;
250
+ h = mul(h, P64_3);
251
+ return h ^ (h >> 32n);
252
+ };
253
+ const mix16 = (p, o, s) => fold(r64(p, o) ^ r64(SECRET, s), r64(p, o + 8) ^ r64(SECRET, s + 8));
254
+ function accumulate512(acc, p, o, s) {
255
+ for (let i = 0; i < 8; i++) {
256
+ const v = r64(p, o + 8 * i);
257
+ const k = v ^ r64(SECRET, s + 8 * i);
258
+ acc[i ^ 1] = add(acc[i ^ 1], v);
259
+ acc[i] = add(acc[i], (k & 0xffffffffn) * (k >> 32n));
260
+ }
261
+ }
262
+ function long(p) {
263
+ const len = p.length;
264
+ const acc = [P32_3, P64_1, P64_2, P64_3, P64_4, P32_2, P64_5, P32_1];
265
+ const stripesPerBlock = (SECRET.length - 64) / 8;
266
+ const block = 64 * stripesPerBlock;
267
+ const blocks = Math.floor((len - 1) / block);
268
+ for (let n = 0; n < blocks; n++) {
269
+ for (let s = 0; s < stripesPerBlock; s++)
270
+ accumulate512(acc, p, n * block + s * 64, s * 8);
271
+ for (let i = 0; i < 8; i++) {
272
+ let a = acc[i];
273
+ a ^= a >> 47n;
274
+ a ^= r64(SECRET, SECRET.length - 64 + 8 * i);
275
+ acc[i] = mul(a, P32_1);
276
+ }
277
+ }
278
+ const stripes = Math.floor((len - 1 - block * blocks) / 64);
279
+ for (let s = 0; s < stripes; s++)
280
+ accumulate512(acc, p, blocks * block + s * 64, s * 8);
281
+ accumulate512(acc, p, len - 64, SECRET.length - 64 - 7);
282
+ let r = mul(BigInt(len), P64_1);
283
+ for (let i = 0; i < 4; i++)
284
+ r = add(r, fold(acc[2 * i] ^ r64(SECRET, 11 + 16 * i), acc[2 * i + 1] ^ r64(SECRET, 19 + 16 * i)));
285
+ return avalanche(r);
286
+ }
287
+ /** XXH3_64bits(input), seed 0. */
288
+ function xxh3(p) {
289
+ const len = p.length;
290
+ const L = BigInt(len);
291
+ if (len === 0)
292
+ return avalanche64(r64(SECRET, 56) ^ r64(SECRET, 64));
293
+ if (len <= 3) {
294
+ const combined = (BigInt(p[0]) << 16n) | (BigInt(p[len >> 1]) << 24n) | BigInt(p[len - 1]) | (L << 8n);
295
+ return avalanche64(combined ^ (r32(SECRET, 0) ^ r32(SECRET, 4)));
296
+ }
297
+ if (len <= 8) {
298
+ const keyed = (r32(p, len - 4) + (r32(p, 0) << 32n)) ^ (r64(SECRET, 8) ^ r64(SECRET, 16));
299
+ let h = keyed ^ rotl(keyed, 49n) ^ rotl(keyed, 24n);
300
+ h = mul(h, 0x9fb21c651e98df25n);
301
+ h ^= add(h >> 35n, L);
302
+ h = mul(h, 0x9fb21c651e98df25n);
303
+ return h ^ (h >> 28n);
304
+ }
305
+ if (len <= 16) {
306
+ const lo = r64(p, 0) ^ (r64(SECRET, 24) ^ r64(SECRET, 32));
307
+ const hi = r64(p, len - 8) ^ (r64(SECRET, 40) ^ r64(SECRET, 48));
308
+ return avalanche(add(add(add(L, swap64(lo)), hi), fold(lo, hi)));
309
+ }
310
+ if (len <= 128) {
311
+ let acc = mul(L, P64_1);
312
+ if (len > 32) {
313
+ if (len > 64) {
314
+ if (len > 96)
315
+ acc = add(add(acc, mix16(p, 48, 96)), mix16(p, len - 64, 112));
316
+ acc = add(add(acc, mix16(p, 32, 64)), mix16(p, len - 48, 80));
317
+ }
318
+ acc = add(add(acc, mix16(p, 16, 32)), mix16(p, len - 32, 48));
319
+ }
320
+ return avalanche(add(add(acc, mix16(p, 0, 0)), mix16(p, len - 16, 16)));
321
+ }
322
+ if (len <= 240) {
323
+ let acc = mul(L, P64_1);
324
+ for (let i = 0; i < 8; i++)
325
+ acc = add(acc, mix16(p, 16 * i, 16 * i));
326
+ acc = avalanche(acc);
327
+ for (let i = 8; i < Math.floor(len / 16); i++)
328
+ acc = add(acc, mix16(p, 16 * i, 16 * (i - 8) + 3));
329
+ return avalanche(add(acc, mix16(p, len - 16, 136 - 17)));
330
+ }
331
+ return long(p);
332
+ }
@@ -46,13 +46,15 @@ export interface PlanTask {
46
46
  }[];
47
47
  };
48
48
  }
49
- /** Vitest's `beforeAll`/`afterEach`, passed in so this package does not import Vitest. */
49
+ /** Vitest's `beforeAll`/`afterEach`/`afterAll`, passed in so this package does not import Vitest. */
50
50
  export interface VitestHooks {
51
51
  beforeAll(fn: (suite: PlanTask) => unknown): void;
52
52
  afterEach(fn: (ctx: {
53
53
  task: PlanTask;
54
54
  skip(note?: string): void;
55
55
  }) => unknown): void;
56
+ /** needed for WT_TIA_RECORD=1 */
57
+ afterAll?(fn: (suite: PlanTask) => unknown): void;
56
58
  }
57
59
  /** Applies the plan in a Vitest setup file. Decisions go to `task.meta.wt` for the reporter. */
58
60
  export declare function vitestSetup(hooks: VitestHooks): void;
@@ -90,4 +92,11 @@ export declare function jestEnvironment<B extends JestEnvBase>(Base: B): B;
90
92
  export declare function toJunit(suite: string, tests: TestRecord[]): string;
91
93
  /** Posts the results as JUnit when WT_CI_RUN_ID is set; never throws. */
92
94
  export declare function uploadResults(client: Client | null, suite: string, tests: TestRecord[]): Promise<boolean>;
95
+ /** Repository-relative files with an executed statement in an istanbul coverage object. */
96
+ export declare function coveredFiles(cov: unknown, root?: string): string[];
97
+ /**
98
+ * Uploads the coverage of one test file for its tests (`suite::rest` ids); never throws.
99
+ * Needs WT_API_URL, WT_PROJECT_ID and WT_API_KEY (the coverage map needs a project API key).
100
+ */
101
+ export declare function uploadCoverage(file: string | undefined, ids: string[], cov: unknown, fetchImpl?: typeof globalThis.fetch): Promise<boolean>;
93
102
  export {};
@@ -11,6 +11,8 @@ exports.vitestSetup = vitestSetup;
11
11
  exports.jestEnvironment = jestEnvironment;
12
12
  exports.toJunit = toJunit;
13
13
  exports.uploadResults = uploadResults;
14
+ exports.coveredFiles = coveredFiles;
15
+ exports.uploadCoverage = uploadCoverage;
14
16
  // Test optimization for Vitest and Jest, the same contract as the pytest plugin. Opt in with
15
17
  // WT_TEST_OPTIMIZATION=1 (plus WT_INGEST_KEY, WT_PROJECT_ID and WT_API_URL; WT_API_KEY, when set,
16
18
  // authenticates the project API calls instead of the ingest key):
@@ -23,13 +25,17 @@ exports.uploadResults = uploadResults;
23
25
  // - a test the project has never seen is retried up to `efd_retries` times (early flake detection).
24
26
  // The reporters upload the results as JUnit (`POST /v1/projects/{prj}/test-runs`) when
25
27
  // WT_CI_RUN_ID is set, which feeds flaky-test detection and auto-quarantine.
28
+ // With WT_TIA_RECORD=1 (and WT_API_KEY) each test file's istanbul coverage (Vitest
29
+ // `coverage.provider: 'istanbul'`, Jest `--coverage`) is uploaded as the per-test coverage map
30
+ // (`PUT /v1/projects/{prj}/tests/coverage-map`): every test of the file gets the files the file
31
+ // executed plus the test file itself, which Test Impact Analysis then uses to skip tests.
26
32
  //
27
33
  // Ids are `<suite>::<file relative to cwd> > <describe>... > <test>`, suite = WT_TEST_SUITE or
28
34
  // the framework name. Vitest:
29
35
  // globalSetup: ['./wt-global.mjs'] // export { default } from '@watchtowerobservability/node/test-plan'
30
36
  // setupFiles: ['./wt-setup.ts'] // import { beforeAll, afterEach } from 'vitest';
31
37
  // // import { vitestSetup } from '@watchtowerobservability/node/test-plan';
32
- // // vitestSetup({ beforeAll, afterEach });
38
+ // // vitestSetup({ beforeAll, afterEach, afterAll });
33
39
  // Jest: globalSetup as above, and testEnvironment: './wt-env.js' with
34
40
  // module.exports = require('@watchtowerobservability/node/test-plan').jestEnvironment(require('jest-environment-node').TestEnvironment);
35
41
  const node_child_process_1 = require("node:child_process");
@@ -134,8 +140,24 @@ function decider(plan) {
134
140
  return d;
135
141
  };
136
142
  }
143
+ /** Ids of the tests in `t` that ran (a skipped test executed nothing, so it gets no coverage). */
144
+ function ranIds(t, suite, file, names, out) {
145
+ for (const c of t.tasks || []) {
146
+ const ran = c.mode !== 'skip' && c.mode !== 'todo' && !!c.result?.state && c.result.state !== 'skip';
147
+ if (c.tasks)
148
+ ranIds(c, suite, file, [...names, c.name], out);
149
+ else if (ran)
150
+ out.push(testId(suite, file, [...names, c.name]));
151
+ }
152
+ return out;
153
+ }
137
154
  /** Applies the plan in a Vitest setup file. Decisions go to `task.meta.wt` for the reporter. */
138
155
  function vitestSetup(hooks) {
156
+ if (process.env.WT_TIA_RECORD === '1' && hooks.afterAll) {
157
+ const suite = (0, exports.suiteName)('vitest');
158
+ const g = globalThis;
159
+ hooks.afterAll((file) => uploadCoverage(file.filepath, ranIds(file, suite, file.filepath, [], []), g.__VITEST_COVERAGE__ || g.__coverage__));
160
+ }
139
161
  const plan = loadPlan();
140
162
  if (!plan)
141
163
  return;
@@ -179,6 +201,7 @@ function jestEnvironment(Base) {
179
201
  wtDecide = decider(loadPlan());
180
202
  wtSuite = (0, exports.suiteName)('jest');
181
203
  wtRetrySaved = [];
204
+ wtRan = [];
182
205
  // any[]: see JestEnvBase
183
206
  constructor(...args) {
184
207
  super(...args);
@@ -194,6 +217,12 @@ function jestEnvironment(Base) {
194
217
  if (super.handleTestEvent)
195
218
  await super.handleTestEvent(event, state);
196
219
  const t = event.test;
220
+ if (event.name === 'test_done' && t && process.env.WT_TIA_RECORD === '1')
221
+ this.wtRan.push(this.wtId(t));
222
+ if (event.name === 'run_finish' && process.env.WT_TIA_RECORD === '1') {
223
+ await uploadCoverage(this.wtFile, this.wtRan, this.global.__coverage__);
224
+ return;
225
+ }
197
226
  if (event.name === 'test_start' && t) {
198
227
  if (this.wtDecide(this.wtId(t)).skip)
199
228
  t.mode = 'skip';
@@ -260,3 +289,64 @@ async function uploadResults(client, suite, tests) {
260
289
  return false;
261
290
  }
262
291
  }
292
+ // ---------------------------------------------------------------- Test Impact Analysis recording
293
+ let rootCache;
294
+ /** The repository top level (git diff paths are relative to it), else the working directory. */
295
+ function repoRoot() {
296
+ if (rootCache === undefined) {
297
+ try {
298
+ rootCache = (0, node_child_process_1.execFileSync)('git', ['rev-parse', '--show-toplevel'], { encoding: 'utf8', timeout: 10_000 }).trim() || process.cwd();
299
+ }
300
+ catch (_e) {
301
+ rootCache = process.cwd();
302
+ }
303
+ }
304
+ return rootCache;
305
+ }
306
+ const repoRel = (f, root) => (0, node_path_1.relative)(root, f).split(node_path_1.sep).join('/');
307
+ /** Repository-relative files with an executed statement in an istanbul coverage object. */
308
+ function coveredFiles(cov, root = repoRoot()) {
309
+ const out = new Set();
310
+ if (!cov || typeof cov !== 'object')
311
+ return [];
312
+ for (const [key, fc] of Object.entries(cov)) {
313
+ if (!fc || !fc.s || !Object.values(fc.s).some((n) => n > 0))
314
+ continue;
315
+ const f = repoRel(fc.path || key, root);
316
+ if (f && !f.startsWith('..'))
317
+ out.add(f);
318
+ }
319
+ return [...out].sort();
320
+ }
321
+ /**
322
+ * Uploads the coverage of one test file for its tests (`suite::rest` ids); never throws.
323
+ * Needs WT_API_URL, WT_PROJECT_ID and WT_API_KEY (the coverage map needs a project API key).
324
+ */
325
+ async function uploadCoverage(file, ids, cov, fetchImpl = globalThis.fetch) {
326
+ const env = process.env;
327
+ const [api, project, key] = [env.WT_API_URL, env.WT_PROJECT_ID, env.WT_API_KEY];
328
+ if (!api || !project || !key || ids.length === 0)
329
+ return false;
330
+ const root = repoRoot();
331
+ const files = coveredFiles(cov, root);
332
+ if (file) {
333
+ const self = repoRel(file, root);
334
+ if (self && !self.startsWith('..') && !files.includes(self))
335
+ files.push(self);
336
+ }
337
+ const tests = [...new Set(ids)].slice(0, 20_000).map((id) => {
338
+ const i = id.indexOf('::');
339
+ return { suite: i < 0 ? '' : id.slice(0, i), test: i < 0 ? id : id.slice(i + 2), files: files.slice(0, 2000) };
340
+ });
341
+ try {
342
+ const res = await fetchImpl(`${api.replace(/\/+$/, '')}/v1/projects/${encodeURIComponent(project)}/tests/coverage-map`, {
343
+ method: 'PUT',
344
+ headers: { Authorization: `Bearer ${key}`, 'Content-Type': 'application/json' },
345
+ body: JSON.stringify({ sha: (env.WT_SHA || env.GITHUB_SHA || env.CI_COMMIT_SHA || '').slice(0, 64), tests }),
346
+ });
347
+ return res.ok;
348
+ }
349
+ catch (_e) {
350
+ return false;
351
+ }
352
+ }