@watchtowerobservability/node 0.2.0 → 0.4.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 (94) 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/iast.d.ts +27 -0
  5. package/dist/cjs/iast.js +138 -0
  6. package/dist/cjs/index.d.ts +3 -0
  7. package/dist/cjs/index.js +14 -1
  8. package/dist/cjs/integrations/amqp.d.ts +13 -0
  9. package/dist/cjs/integrations/amqp.js +96 -0
  10. package/dist/cjs/integrations/bullmq.d.ts +19 -0
  11. package/dist/cjs/integrations/bullmq.js +83 -0
  12. package/dist/cjs/integrations/cache.d.ts +13 -0
  13. package/dist/cjs/integrations/cache.js +51 -0
  14. package/dist/cjs/integrations/db.d.ts +62 -0
  15. package/dist/cjs/integrations/db.js +288 -0
  16. package/dist/cjs/integrations/duckdb.d.ts +22 -0
  17. package/dist/cjs/integrations/duckdb.js +66 -0
  18. package/dist/cjs/integrations/graphile-worker.d.ts +22 -0
  19. package/dist/cjs/integrations/graphile-worker.js +50 -0
  20. package/dist/cjs/integrations/level.d.ts +18 -0
  21. package/dist/cjs/integrations/level.js +71 -0
  22. package/dist/cjs/integrations/lmdb.d.ts +26 -0
  23. package/dist/cjs/integrations/lmdb.js +78 -0
  24. package/dist/cjs/integrations/messaging.d.ts +17 -0
  25. package/dist/cjs/integrations/messaging.js +73 -0
  26. package/dist/cjs/integrations/mqtt.d.ts +7 -0
  27. package/dist/cjs/integrations/mqtt.js +41 -0
  28. package/dist/cjs/integrations/nats.d.ts +17 -0
  29. package/dist/cjs/integrations/nats.js +73 -0
  30. package/dist/cjs/integrations/orm.d.ts +47 -0
  31. package/dist/cjs/integrations/orm.js +93 -0
  32. package/dist/cjs/integrations/pg-boss.d.ts +8 -0
  33. package/dist/cjs/integrations/pg-boss.js +63 -0
  34. package/dist/cjs/integrations/pools.d.ts +41 -0
  35. package/dist/cjs/integrations/pools.js +94 -0
  36. package/dist/cjs/integrations/postgres.d.ts +12 -0
  37. package/dist/cjs/integrations/postgres.js +92 -0
  38. package/dist/cjs/integrations/sqlite.d.ts +36 -0
  39. package/dist/cjs/integrations/sqlite.js +125 -0
  40. package/dist/cjs/pprof.d.ts +6 -0
  41. package/dist/cjs/pprof.js +88 -0
  42. package/dist/cjs/profiling.d.ts +4 -1
  43. package/dist/cjs/profiling.js +18 -8
  44. package/dist/cjs/sql.d.ts +19 -0
  45. package/dist/cjs/sql.js +332 -0
  46. package/dist/cjs/test-plan.d.ts +10 -1
  47. package/dist/cjs/test-plan.js +91 -1
  48. package/dist/esm/appsec.d.ts +95 -0
  49. package/dist/esm/appsec.js +193 -0
  50. package/dist/esm/iast.d.ts +27 -0
  51. package/dist/esm/iast.js +130 -0
  52. package/dist/esm/index.d.ts +3 -0
  53. package/dist/esm/index.js +14 -1
  54. package/dist/esm/integrations/amqp.d.ts +13 -0
  55. package/dist/esm/integrations/amqp.js +92 -0
  56. package/dist/esm/integrations/bullmq.d.ts +19 -0
  57. package/dist/esm/integrations/bullmq.js +79 -0
  58. package/dist/esm/integrations/cache.d.ts +13 -0
  59. package/dist/esm/integrations/cache.js +46 -0
  60. package/dist/esm/integrations/db.d.ts +62 -0
  61. package/dist/esm/integrations/db.js +239 -0
  62. package/dist/esm/integrations/duckdb.d.ts +22 -0
  63. package/dist/esm/integrations/duckdb.js +29 -0
  64. package/dist/esm/integrations/graphile-worker.d.ts +22 -0
  65. package/dist/esm/integrations/graphile-worker.js +46 -0
  66. package/dist/esm/integrations/level.d.ts +18 -0
  67. package/dist/esm/integrations/level.js +35 -0
  68. package/dist/esm/integrations/lmdb.d.ts +26 -0
  69. package/dist/esm/integrations/lmdb.js +42 -0
  70. package/dist/esm/integrations/messaging.d.ts +17 -0
  71. package/dist/esm/integrations/messaging.js +64 -0
  72. package/dist/esm/integrations/mqtt.d.ts +7 -0
  73. package/dist/esm/integrations/mqtt.js +38 -0
  74. package/dist/esm/integrations/nats.d.ts +17 -0
  75. package/dist/esm/integrations/nats.js +70 -0
  76. package/dist/esm/integrations/orm.d.ts +47 -0
  77. package/dist/esm/integrations/orm.js +86 -0
  78. package/dist/esm/integrations/pg-boss.d.ts +8 -0
  79. package/dist/esm/integrations/pg-boss.js +60 -0
  80. package/dist/esm/integrations/pools.d.ts +41 -0
  81. package/dist/esm/integrations/pools.js +89 -0
  82. package/dist/esm/integrations/postgres.d.ts +12 -0
  83. package/dist/esm/integrations/postgres.js +55 -0
  84. package/dist/esm/integrations/sqlite.d.ts +36 -0
  85. package/dist/esm/integrations/sqlite.js +84 -0
  86. package/dist/esm/pprof.d.ts +6 -0
  87. package/dist/esm/pprof.js +85 -0
  88. package/dist/esm/profiling.d.ts +4 -1
  89. package/dist/esm/profiling.js +17 -8
  90. package/dist/esm/sql.d.ts +19 -0
  91. package/dist/esm/sql.js +324 -0
  92. package/dist/esm/test-plan.d.ts +10 -1
  93. package/dist/esm/test-plan.js +89 -1
  94. package/package.json +171 -1
@@ -0,0 +1,92 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.querySummary = void 0;
37
+ exports.instrumentPostgres = instrumentPostgres;
38
+ const dc = __importStar(require("node:diagnostics_channel"));
39
+ const api_1 = require("@opentelemetry/api");
40
+ const sql_js_1 = require("../sql.js");
41
+ Object.defineProperty(exports, "querySummary", { enumerable: true, get: function () { return sql_js_1.querySummary; } });
42
+ const db_js_1 = require("./db.js");
43
+ /**
44
+ * postgres.js has no OTel instrumentation; since 4.0 it publishes each query on the
45
+ * `postgres:query` diagnostics tracing channel. This turns those into CLIENT spans (child of the
46
+ * active span) with `db.system=postgresql`, the operation, a summary and `db.query.hash` (no
47
+ * statement text or parameters unless `opts.queryText`, then literal-free) and the SQLSTATE on
48
+ * failure. Works for ESM and CJS; returns an unsubscribe function (a no-op when this Node has no
49
+ * tracing channels).
50
+ */
51
+ function instrumentPostgres(opts) {
52
+ if (typeof dc.tracingChannel !== 'function')
53
+ return () => { };
54
+ const ch = dc.tracingChannel('postgres:query');
55
+ const spans = new WeakMap();
56
+ const handlers = {
57
+ start(c) {
58
+ spans.set(c, (0, db_js_1.startDbSpan)({
59
+ system: 'postgresql', sql: c.query, namespace: c.database, attributes: { 'db.system': 'postgresql' },
60
+ }, opts, api_1.context.active()));
61
+ },
62
+ end() { },
63
+ asyncStart() { },
64
+ error(c) {
65
+ const span = spans.get(c);
66
+ if (!span)
67
+ return;
68
+ const code = c.error?.code;
69
+ if (typeof code === 'string') {
70
+ span.setAttribute('db.response.status_code', code);
71
+ span.setAttribute('error.type', code);
72
+ }
73
+ // the message can quote values (e.g. a duplicate key), so it is not recorded
74
+ span.setStatus({ code: api_1.SpanStatusCode.ERROR, message: typeof code === 'string' ? `SQLSTATE ${code}` : undefined });
75
+ },
76
+ asyncEnd(c) {
77
+ const span = spans.get(c);
78
+ if (!span)
79
+ return;
80
+ spans.delete(c);
81
+ if (c.host)
82
+ span.setAttribute('server.address', c.host);
83
+ if (c.port)
84
+ span.setAttribute('server.port', c.port);
85
+ if (typeof c.count === 'number')
86
+ span.setAttribute('db.response.returned_rows', c.count);
87
+ span.end();
88
+ },
89
+ };
90
+ ch.subscribe(handlers);
91
+ return () => ch.unsubscribe(handlers);
92
+ }
@@ -0,0 +1,36 @@
1
+ import type { DbOptions } from './db.js';
2
+ type Proto = Record<string, any>;
3
+ interface Ctor {
4
+ prototype: Proto;
5
+ }
6
+ /** `import * as sqlite from 'node:sqlite'; instrumentNodeSqlite(sqlite)`: spans for `DatabaseSync.exec`
7
+ * and prepared `run`/`get`/`all`. shortcut: `iterate()` is not traced (the span would end before the rows). */
8
+ export declare function instrumentNodeSqlite(sqlite: {
9
+ DatabaseSync: Ctor;
10
+ }, opts?: DbOptions): void;
11
+ /** `import Database from 'better-sqlite3'; instrumentBetterSqlite3(Database)`: spans for `exec` and
12
+ * prepared `run`/`get`/`all`, `db.namespace` = the database file name. */
13
+ export declare function instrumentBetterSqlite3(Database: Ctor, opts?: DbOptions): void;
14
+ /**
15
+ * Callback drivers (`sqlite3`, classic `duckdb`): trace `run`/`get`/`all`/`exec`/`each` on each
16
+ * prototype; the span ends in the callback, which runs in the caller's context.
17
+ * shortcut: a call without a callback ends its span when the call returns (no duration).
18
+ */
19
+ export declare function patchCallbackDb(protos: Proto[], system: string, opts: DbOptions | undefined): void;
20
+ /** `import sqlite3 from 'sqlite3'; instrumentSqlite3(sqlite3)`: spans for `Database` run/get/all/exec/each.
21
+ * shortcut: `Statement` objects from `db.prepare` are not traced. */
22
+ export declare function instrumentSqlite3(sqlite3: {
23
+ Database: Ctor;
24
+ }, opts?: DbOptions): void;
25
+ interface LibsqlClient {
26
+ execute(...a: any[]): Promise<unknown>;
27
+ batch?(...a: any[]): Promise<unknown>;
28
+ executeMultiple?(sql: string): Promise<unknown>;
29
+ transaction?(...a: any[]): Promise<any>;
30
+ }
31
+ /** `instrumentLibsql(createClient({ url }))`: spans for `execute`, `batch`, `executeMultiple` and
32
+ * the same calls on transactions it opens. `db.system.name=sqlite`. Returns the client. */
33
+ export declare function instrumentLibsql<C extends LibsqlClient>(client: C, opts?: DbOptions): C;
34
+ /** `wt.embedded.file.size` and `wt.embedded.wal.size` (the `-wal` file) of a SQLite database file. Returns `stop()`. */
35
+ export declare function observeSqliteFile(path: string): () => void;
36
+ export {};
@@ -0,0 +1,125 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.instrumentNodeSqlite = instrumentNodeSqlite;
37
+ exports.instrumentBetterSqlite3 = instrumentBetterSqlite3;
38
+ exports.patchCallbackDb = patchCallbackDb;
39
+ exports.instrumentSqlite3 = instrumentSqlite3;
40
+ exports.instrumentLibsql = instrumentLibsql;
41
+ exports.observeSqliteFile = observeSqliteFile;
42
+ const nodePath = __importStar(require("node:path"));
43
+ const db_js_1 = require("./db.js");
44
+ const STATEMENT = ['run', 'get', 'all'];
45
+ /** Trace `exec(sql)` and the run/get/all of statements `prepare` returns (their prototype is patched once). */
46
+ function patchSync(db, system, opts, sqlOf, nsOf) {
47
+ (0, db_js_1.patch)(db, 'exec', (orig) => function (sql, ...a) {
48
+ return (0, db_js_1.traced)({ system, sql, namespace: nsOf({ database: this }) }, opts, () => orig.call(this, sql, ...a));
49
+ });
50
+ (0, db_js_1.patch)(db, 'prepare', (orig) => function (...a) {
51
+ const stmt = orig.apply(this, a);
52
+ const proto = stmt && Object.getPrototypeOf(stmt);
53
+ if (proto) {
54
+ for (const m of STATEMENT) {
55
+ (0, db_js_1.patch)(proto, m, (o) => function (...args) {
56
+ return (0, db_js_1.traced)({ system, sql: sqlOf(this), namespace: nsOf(this) }, opts, () => o.apply(this, args));
57
+ });
58
+ }
59
+ }
60
+ return stmt;
61
+ });
62
+ }
63
+ const base = (p) => (typeof p === 'string' && p && p !== ':memory:' ? nodePath.basename(p) : undefined);
64
+ /** `import * as sqlite from 'node:sqlite'; instrumentNodeSqlite(sqlite)`: spans for `DatabaseSync.exec`
65
+ * and prepared `run`/`get`/`all`. shortcut: `iterate()` is not traced (the span would end before the rows). */
66
+ function instrumentNodeSqlite(sqlite, opts) {
67
+ patchSync(sqlite.DatabaseSync.prototype, 'sqlite', opts, (s) => s.sourceSQL, () => undefined);
68
+ }
69
+ /** `import Database from 'better-sqlite3'; instrumentBetterSqlite3(Database)`: spans for `exec` and
70
+ * prepared `run`/`get`/`all`, `db.namespace` = the database file name. */
71
+ function instrumentBetterSqlite3(Database, opts) {
72
+ patchSync(Database.prototype, 'sqlite', opts, (s) => s.source, (s) => base(s.database?.name));
73
+ }
74
+ /**
75
+ * Callback drivers (`sqlite3`, classic `duckdb`): trace `run`/`get`/`all`/`exec`/`each` on each
76
+ * prototype; the span ends in the callback, which runs in the caller's context.
77
+ * shortcut: a call without a callback ends its span when the call returns (no duration).
78
+ */
79
+ function patchCallbackDb(protos, system, opts) {
80
+ for (const proto of protos) {
81
+ for (const m of ['run', 'get', 'all', 'exec', 'each']) {
82
+ (0, db_js_1.patch)(proto, m, (orig) => function (...a) {
83
+ const op = { system, sql: typeof a[0] === 'string' ? a[0] : undefined, namespace: base(this.filename) };
84
+ const call = (args) => orig.apply(this, args);
85
+ // each(sql, ..., row, complete): only the completion callback ends the span
86
+ const cb = m !== 'each' || typeof a[a.length - 2] === 'function';
87
+ return cb ? (0, db_js_1.tracedCb)(op, opts, a, call) : (0, db_js_1.traced)(op, opts, () => call(a));
88
+ });
89
+ }
90
+ }
91
+ }
92
+ /** `import sqlite3 from 'sqlite3'; instrumentSqlite3(sqlite3)`: spans for `Database` run/get/all/exec/each.
93
+ * shortcut: `Statement` objects from `db.prepare` are not traced. */
94
+ function instrumentSqlite3(sqlite3, opts) {
95
+ patchCallbackDb([sqlite3.Database.prototype], 'sqlite', opts);
96
+ }
97
+ /** `instrumentLibsql(createClient({ url }))`: spans for `execute`, `batch`, `executeMultiple` and
98
+ * the same calls on transactions it opens. `db.system.name=sqlite`. Returns the client. */
99
+ function instrumentLibsql(client, opts) {
100
+ const sqlOf = (s) => (typeof s === 'string' ? s : s?.sql);
101
+ const wrap = (c) => {
102
+ (0, db_js_1.patch)(c, 'execute', (orig) => function (stmt, ...a) {
103
+ return (0, db_js_1.traced)({ system: 'sqlite', sql: sqlOf(stmt) }, opts, () => orig.call(this, stmt, ...a));
104
+ });
105
+ (0, db_js_1.patch)(c, 'executeMultiple', (orig) => function (sql) {
106
+ return (0, db_js_1.traced)({ system: 'sqlite', sql }, opts, () => orig.call(this, sql));
107
+ });
108
+ (0, db_js_1.patch)(c, 'batch', (orig) => function (stmts, ...a) {
109
+ return (0, db_js_1.traced)({ system: 'sqlite', operation: 'BATCH',
110
+ attributes: { 'db.operation.batch.size': stmts?.length ?? 0 } }, opts, () => orig.call(this, stmts, ...a));
111
+ });
112
+ };
113
+ wrap(client);
114
+ (0, db_js_1.patch)(client, 'transaction', (orig) => async function (...a) {
115
+ const tx = await orig.apply(this, a);
116
+ if (tx)
117
+ wrap(tx);
118
+ return tx;
119
+ });
120
+ return client;
121
+ }
122
+ /** `wt.embedded.file.size` and `wt.embedded.wal.size` (the `-wal` file) of a SQLite database file. Returns `stop()`. */
123
+ function observeSqliteFile(path) {
124
+ return (0, db_js_1.observeEmbedded)('sqlite', nodePath.basename(path), ['file.size', 'wal.size'], () => ({ 'file.size': (0, db_js_1.sizeOf)(path), 'wal.size': (0, db_js_1.sizeOf)(`${path}-wal`) }));
125
+ }
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Folded stacks (`[span_id=…,trace_id=…\t]a;b;c` root first, as `foldProfile` makes them) to a
3
+ * gzipped pprof with sample types samples/count and cpu/nanoseconds. The `k=v` prefix becomes
4
+ * sample labels. Frame names keep their `(file:line)` suffix; one function per distinct frame.
5
+ */
6
+ export declare function foldedToPprof(counts: Map<string, number>, periodNs: number, startMs: number, durationMs: number): Uint8Array;
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.foldedToPprof = foldedToPprof;
4
+ // Minimal pprof (profile.proto) encoder, no dependency: folded stacks in, gzipped protobuf out.
5
+ const node_zlib_1 = require("node:zlib");
6
+ /** Protobuf writer for the handful of field shapes profile.proto needs. */
7
+ class Pb {
8
+ b = [];
9
+ varint(x) {
10
+ let n = BigInt(x);
11
+ while (n > 127n) {
12
+ this.b.push(Number(n & 127n) | 128);
13
+ n >>= 7n;
14
+ }
15
+ this.b.push(Number(n));
16
+ }
17
+ int(field, v) {
18
+ if (v) {
19
+ this.varint(field << 3);
20
+ this.varint(v);
21
+ }
22
+ return this;
23
+ }
24
+ bytes(field, data) {
25
+ this.varint((field << 3) | 2);
26
+ this.varint(data.length);
27
+ for (let i = 0; i < data.length; i++)
28
+ this.b.push(data[i]);
29
+ return this;
30
+ }
31
+ msg(field, m) {
32
+ return this.bytes(field, m.b);
33
+ }
34
+ packed(field, vs) {
35
+ const p = new Pb();
36
+ for (const v of vs)
37
+ p.varint(v);
38
+ return this.bytes(field, p.b);
39
+ }
40
+ }
41
+ /**
42
+ * Folded stacks (`[span_id=…,trace_id=…\t]a;b;c` root first, as `foldProfile` makes them) to a
43
+ * gzipped pprof with sample types samples/count and cpu/nanoseconds. The `k=v` prefix becomes
44
+ * sample labels. Frame names keep their `(file:line)` suffix; one function per distinct frame.
45
+ */
46
+ function foldedToPprof(counts, periodNs, startMs, durationMs) {
47
+ const strings = [''];
48
+ const index = new Map([['', 0]]);
49
+ const s = (x) => {
50
+ let i = index.get(x);
51
+ if (i === undefined) {
52
+ i = strings.length;
53
+ strings.push(x);
54
+ index.set(x, i);
55
+ }
56
+ return i;
57
+ };
58
+ const vt = (type, unit) => new Pb().int(1, s(type)).int(2, s(unit));
59
+ const frames = new Map();
60
+ const out = new Pb().msg(1, vt('samples', 'count')).msg(1, vt('cpu', 'nanoseconds'));
61
+ for (const [key, n] of counts) {
62
+ const tab = key.indexOf('\t');
63
+ const labels = tab >= 0 ? key.slice(0, tab) : '';
64
+ // pprof location ids are leaf first
65
+ const ids = key.slice(tab + 1).split(';').reverse().map((f) => {
66
+ let id = frames.get(f);
67
+ if (id === undefined)
68
+ frames.set(f, (id = frames.size + 1));
69
+ return id;
70
+ });
71
+ const sample = new Pb().packed(1, ids).packed(2, [n, n * periodNs]);
72
+ for (const kv of labels ? labels.split(',') : []) {
73
+ const eq = kv.indexOf('=');
74
+ if (eq > 0)
75
+ sample.msg(3, new Pb().int(1, s(kv.slice(0, eq))).int(2, s(kv.slice(eq + 1))));
76
+ }
77
+ out.msg(2, sample);
78
+ }
79
+ for (const [f, id] of frames) {
80
+ out.msg(4, new Pb().int(1, id).msg(4, new Pb().int(1, id))); // Location{id, line[{function_id}]}
81
+ out.msg(5, new Pb().int(1, id).int(2, s(f))); // Function{id, name}
82
+ }
83
+ out.int(9, BigInt(Math.round(startMs)) * 1000000n).int(10, BigInt(Math.round(durationMs)) * 1000000n);
84
+ out.msg(11, vt('cpu', 'nanoseconds')).int(12, periodNs);
85
+ for (const str of strings)
86
+ out.bytes(6, Buffer.from(str));
87
+ return new Uint8Array((0, node_zlib_1.gzipSync)(Uint8Array.from(out.b)));
88
+ }
@@ -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;