@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,138 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.startIast = startIast;
4
+ exports.stopIast = stopIast;
5
+ exports.iastMiddleware = iastMiddleware;
6
+ exports.checkSink = checkSink;
7
+ exports.guardSink = guardSink;
8
+ exports.flushIast = flushIast;
9
+ // IAST (opt-in): notices at runtime when request-derived input reaches a sensitive sink and reports
10
+ // it to `POST /v1/projects/{prj}/code-security/runtime` (`{service, findings:[{vuln_type, trace_id,
11
+ // code_location, severity, title}]}`). Off until `startIast` is called; until then `iastMiddleware`
12
+ // is a pass-through and `guardSink` calls the original function untouched.
13
+ //
14
+ // How: `iastMiddleware()` stores the request's input strings (query, params, body, cookies, header
15
+ // values) in an AsyncLocalStorage for the life of the request. `guardSink(fn, vulnType)` wraps a
16
+ // sink (`pool.query`, `child_process.exec`, `fs.readFile`, ...); before the call it checks whether
17
+ // any input string appears inside the sink's first argument. Parameterised queries keep the input
18
+ // in the values array, not the text, so they never match. A hit is queued with the `file:line` of
19
+ // the caller (first stack frame outside node_modules and this file) and the active trace id, and
20
+ // is flushed in the background. Each (type, location) is reported once per process.
21
+ // shortcut: taint is substring matching of raw input values (>= 4 chars) in the sink text, not
22
+ // byte-level propagation; input that is transformed (encoded, split) before use is missed.
23
+ const node_async_hooks_1 = require("node:async_hooks");
24
+ const api_1 = require("@opentelemetry/api");
25
+ const MIN_LEN = 4;
26
+ const MAX_INPUTS = 256;
27
+ const store = new node_async_hooks_1.AsyncLocalStorage();
28
+ let opts = null;
29
+ let queue = [];
30
+ let seen = new Set();
31
+ let timer = null;
32
+ function startIast(o) {
33
+ opts = o;
34
+ }
35
+ function stopIast() {
36
+ opts = null;
37
+ queue = [];
38
+ seen = new Set();
39
+ if (timer)
40
+ clearTimeout(timer);
41
+ timer = null;
42
+ }
43
+ function collect(v, out, depth = 0) {
44
+ if (out.length >= MAX_INPUTS || depth > 4 || v == null)
45
+ return;
46
+ if (typeof v === 'string') {
47
+ if (v.length >= MIN_LEN)
48
+ out.push(v);
49
+ }
50
+ else if (Array.isArray(v)) {
51
+ for (const x of v)
52
+ collect(x, out, depth + 1);
53
+ }
54
+ else if (typeof v === 'object') {
55
+ for (const x of Object.values(v))
56
+ collect(x, out, depth + 1);
57
+ }
58
+ }
59
+ /** Express/Connect middleware: marks this request's inputs as tainted for downstream sinks. */
60
+ function iastMiddleware() {
61
+ return (req, _res, next) => {
62
+ if (!opts)
63
+ return next();
64
+ // Resolved at sink time: body parsers and routers run after this middleware.
65
+ const inputsFor = () => {
66
+ const inputs = [];
67
+ for (const k of ['query', 'params', 'body', 'cookies', 'headers'])
68
+ collect(req[k], inputs);
69
+ return inputs;
70
+ };
71
+ store.run(inputsFor, next);
72
+ };
73
+ }
74
+ function callerLocation() {
75
+ const frames = (new Error().stack || '').split('\n').slice(2);
76
+ for (const f of frames) {
77
+ if (f.includes('node_modules') || f.includes('node:internal') || /iast\.[jt]s/.test(f))
78
+ continue;
79
+ const m = /\(?([^()\s]+:\d+)(?::\d+)?\)?\s*$/.exec(f);
80
+ if (m)
81
+ return m[1].replace(/^file:\/\//, '');
82
+ }
83
+ return '';
84
+ }
85
+ /** Check a sink's text against the current request's inputs; queue a finding on a hit. */
86
+ function checkSink(vulnType, text, severity = 'high') {
87
+ const inputsFor = store.getStore();
88
+ if (!opts || !inputsFor || typeof text !== 'string')
89
+ return false;
90
+ if (!inputsFor().some((v) => text.includes(v)))
91
+ return false;
92
+ const loc = callerLocation();
93
+ const key = `${vulnType}|${loc}`;
94
+ if (seen.has(key))
95
+ return true;
96
+ seen.add(key);
97
+ queue.push({
98
+ vuln_type: vulnType,
99
+ trace_id: api_1.trace.getActiveSpan()?.spanContext().traceId || '',
100
+ code_location: loc,
101
+ severity,
102
+ title: `Request input reaches ${vulnType.replace(/_/g, ' ')} sink`,
103
+ });
104
+ if (!timer) {
105
+ timer = setTimeout(() => void flushIast(), opts.flushMs ?? 2000);
106
+ timer.unref?.();
107
+ }
108
+ return true;
109
+ }
110
+ /** Wrap a sink function whose first argument is the sensitive text (SQL, command, path, URL). */
111
+ function guardSink(fn, vulnType) {
112
+ return function (...a) {
113
+ const first = a[0];
114
+ checkSink(vulnType, typeof first === 'string' ? first : first?.text);
115
+ return fn.apply(this, a);
116
+ };
117
+ }
118
+ /** Send queued findings now. Never throws; a failed send drops the batch (IAST is best effort). */
119
+ async function flushIast() {
120
+ if (timer)
121
+ clearTimeout(timer);
122
+ timer = null;
123
+ const o = opts;
124
+ const findings = queue;
125
+ queue = [];
126
+ if (!o || findings.length === 0)
127
+ return;
128
+ try {
129
+ await (o.fetch || globalThis.fetch)(`${o.apiUrl.replace(/\/$/, '')}/v1/projects/${encodeURIComponent(o.project)}/code-security/runtime`, {
130
+ method: 'POST',
131
+ headers: { Authorization: `Bearer ${o.apiKey}`, 'Content-Type': 'application/json' },
132
+ body: JSON.stringify({ service: o.service, findings }),
133
+ });
134
+ }
135
+ catch (_e) {
136
+ /* best effort */
137
+ }
138
+ }
@@ -6,6 +6,7 @@ import { LoggerProvider } from '@opentelemetry/sdk-logs';
6
6
  import type { MeterProvider } from '@opentelemetry/sdk-metrics';
7
7
  import { captureException } from './errors.js';
8
8
  import type { CaptureContext } from './errors.js';
9
+ import type { Profiler } from './profiling.js';
9
10
  export { captureException, metrics, trace };
10
11
  export { setCaptureRules, capturePayload } from './payload.js';
11
12
  export type { CaptureRule } from './payload.js';
@@ -86,6 +87,8 @@ export declare class Client {
86
87
  readonly meterProvider?: MeterProvider;
87
88
  private readonly stopRuntime;
88
89
  uninstall?: () => void;
90
+ /** Set when WT_PROFILING=1 started the continuous profiler. */
91
+ profiler?: Promise<Profiler | undefined>;
89
92
  constructor(opts: InitOptions);
90
93
  /** Emit an OTLP log record; trace and span ids come from the active context. */
91
94
  log(level: LogLevel, body: string, attributes?: Record<string, unknown>): void;
package/dist/cjs/index.js CHANGED
@@ -54,6 +54,7 @@ const nodePath = __importStar(require("node:path"));
54
54
  const errors_js_1 = require("./errors.js");
55
55
  Object.defineProperty(exports, "captureException", { enumerable: true, get: function () { return errors_js_1.captureException; } });
56
56
  const payload_js_1 = require("./payload.js");
57
+ const profiling_js_1 = require("./profiling.js");
57
58
  var payload_js_2 = require("./payload.js");
58
59
  Object.defineProperty(exports, "setCaptureRules", { enumerable: true, get: function () { return payload_js_2.setCaptureRules; } });
59
60
  Object.defineProperty(exports, "capturePayload", { enumerable: true, get: function () { return payload_js_2.capturePayload; } });
@@ -79,6 +80,8 @@ class Client {
79
80
  meterProvider;
80
81
  stopRuntime = () => { };
81
82
  uninstall;
83
+ /** Set when WT_PROFILING=1 started the continuous profiler. */
84
+ profiler;
82
85
  constructor(opts) {
83
86
  const o = opts || {};
84
87
  const env = process.env;
@@ -248,7 +251,7 @@ class Client {
248
251
  }
249
252
  shutdown() {
250
253
  this.stopRuntime();
251
- return Promise.all([this.provider.shutdown(), this.loggerProvider.shutdown(), this.meterProvider?.shutdown()]).then(() => undefined);
254
+ return Promise.all([this.profiler?.then((p) => p?.stop()), this.provider.shutdown(), this.loggerProvider.shutdown(), this.meterProvider?.shutdown()]).then(() => undefined);
252
255
  }
253
256
  }
254
257
  exports.Client = Client;
@@ -267,6 +270,7 @@ let current;
267
270
  function init(opts) {
268
271
  if (current && current.uninstall)
269
272
  current.uninstall();
273
+ void current?.profiler?.then((p) => p?.stop());
270
274
  const c = new Client(opts);
271
275
  current = c;
272
276
  c.provider.register(); // global tracer + async context so trace.getActiveSpan() works
@@ -274,6 +278,15 @@ function init(opts) {
274
278
  api_1.metrics.disable(); // a re-init replaces the previous client's provider
275
279
  api_1.metrics.setGlobalMeterProvider(c.meterProvider);
276
280
  }
281
+ if (process.env.WT_PROFILING === '1') {
282
+ const attrs = c.provider.resource.attributes;
283
+ c.profiler = (0, profiling_js_1.startProfiling)({
284
+ apiKey: c.headers.Authorization.slice('Bearer '.length),
285
+ endpoint: c.endpoint,
286
+ service: String(attrs['service.name']),
287
+ tags: { version: attrs['service.version'] },
288
+ }).catch(() => undefined); // best effort: never break init
289
+ }
277
290
  if ((opts || {}).captureProcessErrors !== false) {
278
291
  c.uninstall = (0, errors_js_1.installProcessHandlers)(() => c.forceFlush());
279
292
  }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * `instrumentAmqplib(await conn.createChannel())`: patches that channel's class (every channel of
3
+ * it): PRODUCER spans on `publish`/`sendToQueue` with the trace context in `options.headers`, and a
4
+ * CONSUMER span (child of and linked to the producer) around each `consume` callback.
5
+ * shortcut: a ConfirmChannel is its own class; pass one of those too if you use both kinds.
6
+ */
7
+ export declare function instrumentAmqplib(channel: object): void;
8
+ /**
9
+ * `instrumentRabbitmqClient(new Connection(url))` for rabbitmq-client: publishers made by
10
+ * `createPublisher` get PRODUCER spans on `send` (context in the envelope headers); handlers given
11
+ * to `createConsumer` run in a CONSUMER span. Call it before creating publishers and consumers.
12
+ */
13
+ export declare function instrumentRabbitmqClient(connection: object): void;
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.instrumentAmqplib = instrumentAmqplib;
4
+ exports.instrumentRabbitmqClient = instrumentRabbitmqClient;
5
+ const db_js_1 = require("./db.js");
6
+ const messaging_js_1 = require("./messaging.js");
7
+ const sendAttrs = (dest, routingKey, op, messageId, size) => ({
8
+ 'messaging.system': 'rabbitmq',
9
+ 'messaging.destination.name': dest,
10
+ 'messaging.operation.type': 'send',
11
+ 'messaging.operation.name': op,
12
+ 'messaging.rabbitmq.destination.routing_key': routingKey,
13
+ ...(typeof messageId === 'string' ? { 'messaging.message.id': messageId } : {}),
14
+ ...(typeof size === 'number' ? { 'messaging.message.body.size': size } : {}),
15
+ });
16
+ function processAttrs(queue, m) {
17
+ const a = {
18
+ 'messaging.system': 'rabbitmq',
19
+ 'messaging.destination.name': queue,
20
+ 'messaging.operation.type': 'process',
21
+ 'messaging.operation.name': 'consume',
22
+ };
23
+ if (m.properties?.messageId)
24
+ a['messaging.message.id'] = m.properties.messageId;
25
+ if (typeof m.content?.length === 'number')
26
+ a['messaging.message.body.size'] = m.content.length;
27
+ if (m.fields?.deliveryTag !== undefined)
28
+ a['messaging.rabbitmq.message.delivery_tag'] = m.fields.deliveryTag;
29
+ if (m.fields?.redelivered !== undefined)
30
+ a['messaging.rabbitmq.message.redelivered'] = m.fields.redelivered;
31
+ if (m.fields?.consumerTag)
32
+ a['messaging.consumer.group.name'] = m.fields.consumerTag;
33
+ return a;
34
+ }
35
+ const withHeaders = (o, h) => ({ ...o, headers: { ...o?.headers, ...h } });
36
+ /**
37
+ * `instrumentAmqplib(await conn.createChannel())`: patches that channel's class (every channel of
38
+ * it): PRODUCER spans on `publish`/`sendToQueue` with the trace context in `options.headers`, and a
39
+ * CONSUMER span (child of and linked to the producer) around each `consume` callback.
40
+ * shortcut: a ConfirmChannel is its own class; pass one of those too if you use both kinds.
41
+ */
42
+ function instrumentAmqplib(channel) {
43
+ const proto = Object.getPrototypeOf(channel);
44
+ (0, db_js_1.patch)(proto, 'publish', (orig) => function (exchange, routingKey, content, options, ...rest) {
45
+ const dest = exchange || routingKey;
46
+ return (0, messaging_js_1.produce)(`publish ${dest}`, sendAttrs(dest, routingKey, 'publish', options?.messageId, content?.length), (h) => orig.call(this, exchange, routingKey, content, withHeaders(options, h), ...rest));
47
+ });
48
+ // amqplib's sendToQueue calls publish('', queue): that inner call adds no second span
49
+ (0, db_js_1.patch)(proto, 'sendToQueue', (orig) => function (queue, content, options, ...rest) {
50
+ return (0, messaging_js_1.produce)(`publish ${queue}`, sendAttrs(queue, queue, 'sendToQueue', options?.messageId, content?.length), (h) => orig.call(this, queue, content, withHeaders(options, h), ...rest));
51
+ });
52
+ (0, db_js_1.patch)(proto, 'consume', (orig) => function (queue, onMessage, ...rest) {
53
+ if (typeof onMessage !== 'function')
54
+ return orig.call(this, queue, onMessage, ...rest);
55
+ const wrapped = (m) => (m
56
+ ? (0, messaging_js_1.consume)(`process ${queue}`, processAttrs(queue, m), [m.properties?.headers], () => onMessage(m))
57
+ : onMessage(m)); // null: the server cancelled the consumer
58
+ return orig.call(this, queue, wrapped, ...rest);
59
+ });
60
+ }
61
+ /**
62
+ * `instrumentRabbitmqClient(new Connection(url))` for rabbitmq-client: publishers made by
63
+ * `createPublisher` get PRODUCER spans on `send` (context in the envelope headers); handlers given
64
+ * to `createConsumer` run in a CONSUMER span. Call it before creating publishers and consumers.
65
+ */
66
+ function instrumentRabbitmqClient(connection) {
67
+ (0, db_js_1.patch)(connection, 'createPublisher', (orig) => function (...a) {
68
+ const pub = orig.apply(this, a);
69
+ (0, db_js_1.patch)(pub, 'send', (send) => function (envelope, body, ...rest) {
70
+ const env = typeof envelope === 'string' ? { routingKey: envelope } : { ...envelope };
71
+ const rk = env.routingKey ?? '';
72
+ const dest = env.exchange || rk;
73
+ const size = typeof body === 'string' || body instanceof Uint8Array ? body.length : undefined;
74
+ return (0, messaging_js_1.produce)(`publish ${dest}`, sendAttrs(dest, rk, 'send', env.messageId, size), (h) => send.call(this, { ...env, headers: { ...env.headers, ...h } }, body, ...rest));
75
+ });
76
+ return pub;
77
+ });
78
+ (0, db_js_1.patch)(connection, 'createConsumer', (orig) => function (props, handler, ...rest) {
79
+ if (typeof handler !== 'function')
80
+ return orig.call(this, props, handler, ...rest);
81
+ const queue = (typeof props === 'string' ? props : props?.queue) ?? '';
82
+ const wrapped = function (m, ...more) {
83
+ const a = {
84
+ 'messaging.system': 'rabbitmq',
85
+ 'messaging.destination.name': queue,
86
+ 'messaging.operation.type': 'process',
87
+ 'messaging.operation.name': 'consume',
88
+ ...(m?.messageId ? { 'messaging.message.id': m.messageId } : {}),
89
+ ...(m?.deliveryTag !== undefined ? { 'messaging.rabbitmq.message.delivery_tag': m.deliveryTag } : {}),
90
+ ...(m?.redelivered !== undefined ? { 'messaging.rabbitmq.message.redelivered': m.redelivered } : {}),
91
+ };
92
+ return (0, messaging_js_1.consume)(`process ${queue}`, a, [m?.headers], () => handler.call(this, m, ...more));
93
+ };
94
+ return orig.call(this, props, wrapped, ...rest);
95
+ });
96
+ }
@@ -0,0 +1,19 @@
1
+ import { CONTEXT_KEY } from './messaging.js';
2
+ export { CONTEXT_KEY };
3
+ interface BullMQLike {
4
+ Queue: {
5
+ prototype: Record<string | symbol, unknown>;
6
+ };
7
+ Worker: {
8
+ prototype: Record<string | symbol, unknown>;
9
+ };
10
+ }
11
+ /**
12
+ * `import * as bullmq from 'bullmq'; instrumentBullMQ(bullmq)` (ESM or CJS): PRODUCER spans on
13
+ * `Queue.add`/`addBulk`, CONSUMER spans around each job a `Worker` processes, parented by the
14
+ * producer's context carried in job data under `_wt` (stripped before your processor runs; object
15
+ * data only). Call it once, before creating queues and workers. Returns false when this bullmq
16
+ * has no `Worker.callProcessJob` (consumer spans off; producer spans still on).
17
+ * shortcut: sandboxed (file path) processors run in a child process and get no consumer span.
18
+ */
19
+ export declare function instrumentBullMQ(bullmq: BullMQLike): boolean;
@@ -0,0 +1,83 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CONTEXT_KEY = void 0;
4
+ exports.instrumentBullMQ = instrumentBullMQ;
5
+ const api_1 = require("@opentelemetry/api");
6
+ const messaging_js_1 = require("./messaging.js");
7
+ Object.defineProperty(exports, "CONTEXT_KEY", { enumerable: true, get: function () { return messaging_js_1.CONTEXT_KEY; } });
8
+ const PATCHED = Symbol.for('watchtower.bullmq');
9
+ const tracer = () => api_1.trace.getTracer('watchtower');
10
+ const base = (queue, op, name) => ({
11
+ 'messaging.system': 'bullmq',
12
+ 'messaging.destination.name': queue,
13
+ 'messaging.operation.type': op,
14
+ 'messaging.operation.name': name,
15
+ });
16
+ async function run(span, fn) {
17
+ try {
18
+ return await fn();
19
+ }
20
+ catch (e) {
21
+ span.recordException(e);
22
+ span.setStatus({ code: api_1.SpanStatusCode.ERROR, message: e?.message });
23
+ throw e;
24
+ }
25
+ finally {
26
+ span.end();
27
+ }
28
+ }
29
+ /**
30
+ * `import * as bullmq from 'bullmq'; instrumentBullMQ(bullmq)` (ESM or CJS): PRODUCER spans on
31
+ * `Queue.add`/`addBulk`, CONSUMER spans around each job a `Worker` processes, parented by the
32
+ * producer's context carried in job data under `_wt` (stripped before your processor runs; object
33
+ * data only). Call it once, before creating queues and workers. Returns false when this bullmq
34
+ * has no `Worker.callProcessJob` (consumer spans off; producer spans still on).
35
+ * shortcut: sandboxed (file path) processors run in a child process and get no consumer span.
36
+ */
37
+ function instrumentBullMQ(bullmq) {
38
+ const q = bullmq.Queue.prototype;
39
+ const w = bullmq.Worker.prototype;
40
+ if (q[PATCHED])
41
+ return typeof w.callProcessJob === 'function';
42
+ q[PATCHED] = true;
43
+ const add = q.add;
44
+ q.add = function (name, data, opts) {
45
+ const attrs = { ...base(this.name, 'send', 'add'), 'messaging.bullmq.job.name': name };
46
+ if (opts?.delay)
47
+ attrs['messaging.bullmq.job.delay_ms'] = opts.delay;
48
+ return tracer().startActiveSpan(`send ${this.name}`, { kind: api_1.SpanKind.PRODUCER, attributes: attrs }, (span) => run(span, async () => {
49
+ const job = (await add.call(this, name, (0, messaging_js_1.injectData)(data), opts));
50
+ if (job?.id)
51
+ span.setAttribute('messaging.message.id', job.id);
52
+ return job;
53
+ }));
54
+ };
55
+ const addBulk = q.addBulk;
56
+ if (addBulk) {
57
+ q.addBulk = function (jobs) {
58
+ const attrs = { ...base(this.name, 'send', 'addBulk'), 'messaging.batch.message_count': jobs.length };
59
+ return tracer().startActiveSpan(`send ${this.name}`, { kind: api_1.SpanKind.PRODUCER, attributes: attrs }, (span) => run(span, () => addBulk.call(this, jobs.map((j) => ({ ...j, data: (0, messaging_js_1.injectData)(j.data) })))));
60
+ };
61
+ }
62
+ const call = w.callProcessJob;
63
+ if (typeof call !== 'function')
64
+ return false;
65
+ w.callProcessJob = function (job, ...rest) {
66
+ let parent = api_1.context.active();
67
+ if ((0, messaging_js_1.isPlainObject)(job.data) && (0, messaging_js_1.isPlainObject)(job.data[messaging_js_1.CONTEXT_KEY])) {
68
+ parent = api_1.propagation.extract(parent, job.data[messaging_js_1.CONTEXT_KEY]);
69
+ delete job.data[messaging_js_1.CONTEXT_KEY];
70
+ }
71
+ const attrs = {
72
+ ...base(this.name, 'process', 'process'),
73
+ 'messaging.bullmq.job.name': job.name,
74
+ 'messaging.bullmq.job.attempt': (job.attemptsMade ?? 0) + 1,
75
+ };
76
+ if (job.id)
77
+ attrs['messaging.message.id'] = job.id;
78
+ if (job.opts?.delay)
79
+ attrs['messaging.bullmq.job.delay_ms'] = job.opts.delay;
80
+ return tracer().startActiveSpan(`process ${this.name}`, { kind: api_1.SpanKind.CONSUMER, attributes: attrs }, parent, (span) => run(span, () => call.call(this, job, ...rest)));
81
+ };
82
+ return true;
83
+ }
@@ -0,0 +1,13 @@
1
+ /** Hit/miss counters and entry count for a node-cache instance (`get`, `mget`). Returns `stop()`. */
2
+ export declare function instrumentNodeCache(cache: {
3
+ getStats?(): {
4
+ keys?: number;
5
+ };
6
+ }, name?: string): () => void;
7
+ /** Hit/miss counters and entry count for an lru-cache instance (`get`). Returns `stop()`.
8
+ * shortcut: `fetch()` (the async loader) is not counted. */
9
+ export declare function instrumentLruCache(cache: {
10
+ size?: number;
11
+ }, name?: string): () => void;
12
+ /** Hit/miss counters for a Keyv instance (`get` with one key or an array; also `getMany`). */
13
+ export declare function instrumentKeyv(keyv: object, name?: string): void;
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.instrumentNodeCache = instrumentNodeCache;
4
+ exports.instrumentLruCache = instrumentLruCache;
5
+ exports.instrumentKeyv = instrumentKeyv;
6
+ const db_js_1 = require("./db.js");
7
+ /** Count hits and misses of `get`-like methods: `undefined` is a miss; arrays and objects count per key. */
8
+ function count(c, keys, v) {
9
+ if (Array.isArray(keys)) {
10
+ const found = Array.isArray(v) ? v.filter((x) => x !== undefined).length
11
+ : v && typeof v === 'object' ? Object.keys(v).length : 0;
12
+ c.hit(found);
13
+ c.miss(keys.length - found);
14
+ }
15
+ else if (v === undefined) {
16
+ c.miss();
17
+ }
18
+ else {
19
+ c.hit();
20
+ }
21
+ }
22
+ function wrapGets(cache, system, name, methods) {
23
+ const c = (0, db_js_1.cacheCounters)(system, name);
24
+ for (const m of methods) {
25
+ (0, db_js_1.patch)(cache, m, (orig) => function (key, ...a) {
26
+ const v = orig.call(this, key, ...a);
27
+ if (v && typeof v.then === 'function') {
28
+ v.then((x) => count(c, key, x), () => { });
29
+ }
30
+ else {
31
+ count(c, key, v);
32
+ }
33
+ return v;
34
+ });
35
+ }
36
+ }
37
+ /** Hit/miss counters and entry count for a node-cache instance (`get`, `mget`). Returns `stop()`. */
38
+ function instrumentNodeCache(cache, name = 'default') {
39
+ wrapGets(cache, 'node-cache', name, ['get', 'mget']);
40
+ return (0, db_js_1.observeEmbedded)('node-cache', name, ['cache.entries'], () => ({ 'cache.entries': cache.getStats?.().keys }));
41
+ }
42
+ /** Hit/miss counters and entry count for an lru-cache instance (`get`). Returns `stop()`.
43
+ * shortcut: `fetch()` (the async loader) is not counted. */
44
+ function instrumentLruCache(cache, name = 'default') {
45
+ wrapGets(cache, 'lru-cache', name, ['get']);
46
+ return (0, db_js_1.observeEmbedded)('lru-cache', name, ['cache.entries'], () => ({ 'cache.entries': cache.size }));
47
+ }
48
+ /** Hit/miss counters for a Keyv instance (`get` with one key or an array; also `getMany`). */
49
+ function instrumentKeyv(keyv, name = 'default') {
50
+ wrapGets(keyv, 'keyv', name, ['get', 'getMany']);
51
+ }
@@ -0,0 +1,62 @@
1
+ import type { Attributes, Context, Span, TimeInput } from '@opentelemetry/api';
2
+ export interface DbOptions {
3
+ /** Add the literal-free statement as `db.query.text`. Default: `WATCHTOWER_DB_QUERY_TEXT=1`. */
4
+ queryText?: boolean;
5
+ /** Flag a statement run more than this many times under one parent span. Default:
6
+ * `WATCHTOWER_DB_N_PLUS_ONE` or 5; 0 turns it off. */
7
+ nPlusOne?: number;
8
+ }
9
+ export interface DbOp {
10
+ /** `db.system.name`, e.g. `sqlite`. */
11
+ system: string;
12
+ operation?: string;
13
+ collection?: string;
14
+ namespace?: string;
15
+ /** Statement text: summarised and hashed, never recorded raw. */
16
+ sql?: string | null;
17
+ attributes?: Attributes;
18
+ }
19
+ export declare const inside: (ctx?: Context) => boolean;
20
+ /** `parent` with `span` active; `nested` marks it as inside an instrumented call (no inner spans). */
21
+ export declare const enter: (parent: Context, span: Span, nested?: boolean) => Context;
22
+ /** A CLIENT span for `op` (not made active): summary, hash, N+1 hint. Prefer `traced`. */
23
+ export declare function startDbSpan(op: DbOp, opts: DbOptions | undefined, parent: Context, startTime?: TimeInput): Span;
24
+ /** A finished CLIENT span for a query a library reports after the fact (ORM loggers): it ends now. */
25
+ export declare function recordDbSpan(op: DbOp, opts: DbOptions | undefined, durationMs: number, error?: unknown): void;
26
+ /** Error status with the driver's code; the message is not recorded (it can quote values). */
27
+ export declare function fail(span: Span, e: unknown): void;
28
+ /** Run `fn` and end `span` when it returns, throws or its promise settles. The result is returned as is. */
29
+ export declare function settle<T>(span: Span, fn: () => T): T;
30
+ /** A CLIENT span around `fn` (sync or promise), child of the active span. `nested: false` lets
31
+ * calls made inside `fn` (a transaction callback) get their own spans. */
32
+ export declare function traced<T>(op: DbOp, opts: DbOptions | undefined, fn: () => T, nested?: boolean): T;
33
+ /** `traced` for node-style callback APIs: the last function argument ends the span and runs in the caller's context. */
34
+ export declare function tracedCb<T>(op: DbOp, opts: DbOptions | undefined, args: unknown[], call: (args: unknown[]) => T): T;
35
+ /** Wrap `obj[name]` once; `wrap` gets the original. Missing methods are skipped. */
36
+ export declare function patch<T extends object>(obj: T, name: string, wrap: (orig: (...a: any[]) => any) => (...a: any[]) => any): void;
37
+ export interface PoolState {
38
+ used?: number;
39
+ idle?: number;
40
+ pending?: number;
41
+ max?: number;
42
+ }
43
+ /**
44
+ * Pool gauges per OTel db semconv, read at each export: `db.client.connection.count{state}`,
45
+ * `.max`, `.pending_requests`, with `db.client.connection.pool.name` and `db.system.name`.
46
+ * Returns the recorders for wait/use time and timeouts, and `stop()`.
47
+ */
48
+ export declare function observePool(system: string, pool: string, read: () => PoolState): {
49
+ wait: (seconds: number) => void;
50
+ use: (seconds: number) => void;
51
+ timeout: () => void;
52
+ stop: () => void;
53
+ };
54
+ /** `wt.embedded.<key>` gauges for an embedded store, read at each export. Returns `stop()`. */
55
+ export declare function observeEmbedded(system: string, namespace: string | undefined, keys: string[], read: () => Partial<Record<string, number>>): () => void;
56
+ /** Hit/miss counters (`wt.embedded.cache.hits` / `.misses`) for an in-memory cache. */
57
+ export declare function cacheCounters(system: string, name: string): {
58
+ hit: (n?: number) => void;
59
+ miss: (n?: number) => void;
60
+ };
61
+ /** Size in bytes of a file, or of the files directly in a directory; 0 when missing. */
62
+ export declare function sizeOf(path: string): number;