@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.
- package/README.md +186 -3
- package/dist/cjs/appsec.d.ts +95 -0
- package/dist/cjs/appsec.js +206 -0
- package/dist/cjs/index.d.ts +3 -0
- package/dist/cjs/index.js +14 -1
- package/dist/cjs/integrations/amqp.d.ts +13 -0
- package/dist/cjs/integrations/amqp.js +96 -0
- package/dist/cjs/integrations/bullmq.d.ts +19 -0
- package/dist/cjs/integrations/bullmq.js +83 -0
- package/dist/cjs/integrations/cache.d.ts +13 -0
- package/dist/cjs/integrations/cache.js +51 -0
- package/dist/cjs/integrations/db.d.ts +62 -0
- package/dist/cjs/integrations/db.js +288 -0
- package/dist/cjs/integrations/duckdb.d.ts +22 -0
- package/dist/cjs/integrations/duckdb.js +66 -0
- package/dist/cjs/integrations/graphile-worker.d.ts +22 -0
- package/dist/cjs/integrations/graphile-worker.js +50 -0
- package/dist/cjs/integrations/level.d.ts +18 -0
- package/dist/cjs/integrations/level.js +71 -0
- package/dist/cjs/integrations/lmdb.d.ts +26 -0
- package/dist/cjs/integrations/lmdb.js +78 -0
- package/dist/cjs/integrations/messaging.d.ts +17 -0
- package/dist/cjs/integrations/messaging.js +73 -0
- package/dist/cjs/integrations/mqtt.d.ts +7 -0
- package/dist/cjs/integrations/mqtt.js +41 -0
- package/dist/cjs/integrations/nats.d.ts +17 -0
- package/dist/cjs/integrations/nats.js +73 -0
- package/dist/cjs/integrations/orm.d.ts +47 -0
- package/dist/cjs/integrations/orm.js +93 -0
- package/dist/cjs/integrations/pg-boss.d.ts +8 -0
- package/dist/cjs/integrations/pg-boss.js +63 -0
- package/dist/cjs/integrations/pools.d.ts +41 -0
- package/dist/cjs/integrations/pools.js +94 -0
- package/dist/cjs/integrations/postgres.d.ts +12 -0
- package/dist/cjs/integrations/postgres.js +92 -0
- package/dist/cjs/integrations/sqlite.d.ts +36 -0
- package/dist/cjs/integrations/sqlite.js +125 -0
- package/dist/cjs/pprof.d.ts +6 -0
- package/dist/cjs/pprof.js +88 -0
- package/dist/cjs/profiling.d.ts +4 -1
- package/dist/cjs/profiling.js +18 -8
- package/dist/cjs/sql.d.ts +19 -0
- package/dist/cjs/sql.js +332 -0
- package/dist/cjs/test-plan.d.ts +10 -1
- package/dist/cjs/test-plan.js +91 -1
- package/dist/esm/appsec.d.ts +95 -0
- package/dist/esm/appsec.js +193 -0
- package/dist/esm/index.d.ts +3 -0
- package/dist/esm/index.js +14 -1
- package/dist/esm/integrations/amqp.d.ts +13 -0
- package/dist/esm/integrations/amqp.js +92 -0
- package/dist/esm/integrations/bullmq.d.ts +19 -0
- package/dist/esm/integrations/bullmq.js +79 -0
- package/dist/esm/integrations/cache.d.ts +13 -0
- package/dist/esm/integrations/cache.js +46 -0
- package/dist/esm/integrations/db.d.ts +62 -0
- package/dist/esm/integrations/db.js +239 -0
- package/dist/esm/integrations/duckdb.d.ts +22 -0
- package/dist/esm/integrations/duckdb.js +29 -0
- package/dist/esm/integrations/graphile-worker.d.ts +22 -0
- package/dist/esm/integrations/graphile-worker.js +46 -0
- package/dist/esm/integrations/level.d.ts +18 -0
- package/dist/esm/integrations/level.js +35 -0
- package/dist/esm/integrations/lmdb.d.ts +26 -0
- package/dist/esm/integrations/lmdb.js +42 -0
- package/dist/esm/integrations/messaging.d.ts +17 -0
- package/dist/esm/integrations/messaging.js +64 -0
- package/dist/esm/integrations/mqtt.d.ts +7 -0
- package/dist/esm/integrations/mqtt.js +38 -0
- package/dist/esm/integrations/nats.d.ts +17 -0
- package/dist/esm/integrations/nats.js +70 -0
- package/dist/esm/integrations/orm.d.ts +47 -0
- package/dist/esm/integrations/orm.js +86 -0
- package/dist/esm/integrations/pg-boss.d.ts +8 -0
- package/dist/esm/integrations/pg-boss.js +60 -0
- package/dist/esm/integrations/pools.d.ts +41 -0
- package/dist/esm/integrations/pools.js +89 -0
- package/dist/esm/integrations/postgres.d.ts +12 -0
- package/dist/esm/integrations/postgres.js +55 -0
- package/dist/esm/integrations/sqlite.d.ts +36 -0
- package/dist/esm/integrations/sqlite.js +84 -0
- package/dist/esm/pprof.d.ts +6 -0
- package/dist/esm/pprof.js +85 -0
- package/dist/esm/profiling.d.ts +4 -1
- package/dist/esm/profiling.js +17 -8
- package/dist/esm/sql.d.ts +19 -0
- package/dist/esm/sql.js +324 -0
- package/dist/esm/test-plan.d.ts +10 -1
- package/dist/esm/test-plan.js +89 -1
- package/package.json +161 -1
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { patch } from './db.js';
|
|
2
|
+
import { consume, produce } from './messaging.js';
|
|
3
|
+
const attrs = (topic, type, op, payload, id) => ({
|
|
4
|
+
'messaging.system': 'mqtt',
|
|
5
|
+
'messaging.destination.name': topic,
|
|
6
|
+
'messaging.operation.type': type,
|
|
7
|
+
'messaging.operation.name': op,
|
|
8
|
+
...(typeof payload === 'string' || payload instanceof Uint8Array ? { 'messaging.message.body.size': payload.length } : {}),
|
|
9
|
+
...(id !== undefined ? { 'messaging.message.id': String(id) } : {}),
|
|
10
|
+
});
|
|
11
|
+
/**
|
|
12
|
+
* `instrumentMqtt(mqtt.connect(url))` for mqtt.js: PRODUCER spans on `publish`/`publishAsync`, and a
|
|
13
|
+
* CONSUMER span around the client's `message` listeners. The trace context travels in MQTT 5 user
|
|
14
|
+
* properties, so only `protocolVersion: 5` clients propagate (older clients get unlinked spans).
|
|
15
|
+
* shortcut: `publish` spans end when the call returns, not at the QoS 1/2 ack; use `publishAsync` for ack time.
|
|
16
|
+
*/
|
|
17
|
+
export function instrumentMqtt(client) {
|
|
18
|
+
const v5 = () => client.options?.protocolVersion === 5;
|
|
19
|
+
patch(client, 'publish', (orig) => function (topic, message, opts, cb) {
|
|
20
|
+
if (typeof opts === 'function')
|
|
21
|
+
[cb, opts] = [opts, undefined];
|
|
22
|
+
return produce(`publish ${topic}`, attrs(topic, 'send', 'publish', message), (h) => {
|
|
23
|
+
const o = opts;
|
|
24
|
+
const out = v5() ? { ...o, properties: { ...o?.properties, userProperties: { ...o?.properties?.userProperties, ...h } } } : o;
|
|
25
|
+
return cb === undefined ? orig.call(this, topic, message, out) : orig.call(this, topic, message, out ?? {}, cb);
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
// publishAsync calls publish, which then injects the context of this span
|
|
29
|
+
patch(client, 'publishAsync', (orig) => function (topic, message, ...rest) {
|
|
30
|
+
return produce(`publish ${topic}`, attrs(topic, 'send', 'publishAsync', message), () => orig.call(this, topic, message, ...rest));
|
|
31
|
+
});
|
|
32
|
+
patch(client, 'emit', (orig) => function (event, ...args) {
|
|
33
|
+
if (event !== 'message')
|
|
34
|
+
return orig.call(this, event, ...args);
|
|
35
|
+
const [topic, payload, packet] = args;
|
|
36
|
+
return consume(`process ${topic}`, attrs(topic, 'process', 'message', payload, packet?.messageId), [packet?.properties?.userProperties], () => orig.call(this, event, ...args));
|
|
37
|
+
});
|
|
38
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
interface MsgHdrs {
|
|
2
|
+
get(k: string): string;
|
|
3
|
+
set(k: string, v: string): void;
|
|
4
|
+
}
|
|
5
|
+
interface NatsModule {
|
|
6
|
+
headers(): MsgHdrs;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* `instrumentNats(nc, nats)` (`import * as nats from 'nats'`; `nc = await nats.connect()`): patches the
|
|
10
|
+
* connection class. `publish`/`request` get PRODUCER spans with the context in NATS headers;
|
|
11
|
+
* `subscribe` messages get CONSUMER spans (child of and linked to the producer). With a `callback`
|
|
12
|
+
* subscription the span is active in the callback. With `for await (const m of sub)` each span runs
|
|
13
|
+
* from receipt to the next iteration and is not active in the loop body (child spans there are not
|
|
14
|
+
* parented to it; the producer link still gives lag).
|
|
15
|
+
*/
|
|
16
|
+
export declare function instrumentNats(nc: object, nats: NatsModule): void;
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { patch } from './db.js';
|
|
2
|
+
import { consume, produce } from './messaging.js';
|
|
3
|
+
const attrs = (subject, type, op, data, queue) => ({
|
|
4
|
+
'messaging.system': 'nats',
|
|
5
|
+
'messaging.destination.name': subject,
|
|
6
|
+
'messaging.operation.type': type,
|
|
7
|
+
'messaging.operation.name': op,
|
|
8
|
+
...(typeof data?.length === 'number' ? { 'messaging.message.body.size': data.length } : {}),
|
|
9
|
+
...(queue ? { 'messaging.consumer.group.name': queue } : {}),
|
|
10
|
+
});
|
|
11
|
+
const carrier = (m) => ({ traceparent: m.headers?.get('traceparent'), tracestate: m.headers?.get('tracestate') });
|
|
12
|
+
/**
|
|
13
|
+
* `instrumentNats(nc, nats)` (`import * as nats from 'nats'`; `nc = await nats.connect()`): patches the
|
|
14
|
+
* connection class. `publish`/`request` get PRODUCER spans with the context in NATS headers;
|
|
15
|
+
* `subscribe` messages get CONSUMER spans (child of and linked to the producer). With a `callback`
|
|
16
|
+
* subscription the span is active in the callback. With `for await (const m of sub)` each span runs
|
|
17
|
+
* from receipt to the next iteration and is not active in the loop body (child spans there are not
|
|
18
|
+
* parented to it; the producer link still gives lag).
|
|
19
|
+
*/
|
|
20
|
+
export function instrumentNats(nc, nats) {
|
|
21
|
+
const proto = Object.getPrototypeOf(nc);
|
|
22
|
+
const withHeaders = (o, h) => {
|
|
23
|
+
const hdrs = o?.headers ?? nats.headers();
|
|
24
|
+
for (const [k, v] of Object.entries(h))
|
|
25
|
+
hdrs.set(k, v);
|
|
26
|
+
return { ...o, headers: hdrs };
|
|
27
|
+
};
|
|
28
|
+
patch(proto, 'publish', (orig) => function (subject, data, o) {
|
|
29
|
+
return produce(`send ${subject}`, attrs(subject, 'send', 'publish', data), (h) => orig.call(this, subject, data, withHeaders(o, h)));
|
|
30
|
+
});
|
|
31
|
+
patch(proto, 'request', (orig) => function (subject, data, o) {
|
|
32
|
+
return produce(`send ${subject}`, attrs(subject, 'send', 'request', data), (h) => orig.call(this, subject, data, withHeaders(o, h)));
|
|
33
|
+
});
|
|
34
|
+
patch(proto, 'subscribe', (orig) => function (subject, o) {
|
|
35
|
+
const cb = o?.callback;
|
|
36
|
+
if (typeof cb === 'function') {
|
|
37
|
+
const callback = (err, m) => (err || !m
|
|
38
|
+
? cb(err, m)
|
|
39
|
+
: consume(`process ${m.subject}`, attrs(m.subject, 'process', 'subscribe', m.data, o?.queue), [carrier(m)], () => cb(err, m)));
|
|
40
|
+
return orig.call(this, subject, { ...o, callback });
|
|
41
|
+
}
|
|
42
|
+
const sub = orig.call(this, subject, o);
|
|
43
|
+
const iter = sub[Symbol.asyncIterator]?.bind(sub);
|
|
44
|
+
if (!iter)
|
|
45
|
+
return sub;
|
|
46
|
+
sub[Symbol.asyncIterator] = () => {
|
|
47
|
+
const inner = iter();
|
|
48
|
+
let end;
|
|
49
|
+
const close = () => { end?.(); end = undefined; };
|
|
50
|
+
return {
|
|
51
|
+
async next() {
|
|
52
|
+
close();
|
|
53
|
+
const r = await inner.next();
|
|
54
|
+
if (!r.done) {
|
|
55
|
+
const m = r.value;
|
|
56
|
+
// the span ends when this promise resolves, i.e. on the next iteration or return
|
|
57
|
+
void consume(`process ${m.subject}`, attrs(m.subject, 'process', 'subscribe', m.data, o?.queue), [carrier(m)], () => new Promise((res) => { end = res; }));
|
|
58
|
+
}
|
|
59
|
+
return r;
|
|
60
|
+
},
|
|
61
|
+
async return(v) {
|
|
62
|
+
close();
|
|
63
|
+
return inner.return ? inner.return(v) : { done: true, value: v };
|
|
64
|
+
},
|
|
65
|
+
[Symbol.asyncIterator]() { return this; },
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
return sub;
|
|
69
|
+
});
|
|
70
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { DbOptions } from './db.js';
|
|
2
|
+
/**
|
|
3
|
+
* `instrumentDrizzle(db, 'postgresql')`: a CLIENT span per query a Drizzle database runs (`system`
|
|
4
|
+
* is the `db.system.name`: postgresql, mysql, sqlite, ...). Call once per `drizzle(...)` instance.
|
|
5
|
+
* shortcut: `db.transaction(tx => ...)` runs on its own session and gets no spans; instrument the
|
|
6
|
+
* driver as well if transactions matter.
|
|
7
|
+
*/
|
|
8
|
+
export declare function instrumentDrizzle(db: object, system: string, opts?: DbOptions): void;
|
|
9
|
+
interface KyselyLogEvent {
|
|
10
|
+
level: 'query' | 'error';
|
|
11
|
+
query: {
|
|
12
|
+
sql: string;
|
|
13
|
+
};
|
|
14
|
+
queryDurationMillis: number;
|
|
15
|
+
error?: unknown;
|
|
16
|
+
}
|
|
17
|
+
/** `new Kysely({ dialect, log: kyselyLog('postgresql') })`; `next` still gets every event. */
|
|
18
|
+
export declare function kyselyLog(system: string, next?: (e: KyselyLogEvent) => void, opts?: DbOptions): (e: KyselyLogEvent) => void;
|
|
19
|
+
interface DataSourceLike {
|
|
20
|
+
options: {
|
|
21
|
+
type: string;
|
|
22
|
+
database?: unknown;
|
|
23
|
+
};
|
|
24
|
+
driver: object;
|
|
25
|
+
}
|
|
26
|
+
/** `instrumentTypeORM(dataSource)` (before or after `initialize`): a CLIENT span per `QueryRunner.query`. */
|
|
27
|
+
export declare function instrumentTypeORM(dataSource: DataSourceLike, opts?: DbOptions): void;
|
|
28
|
+
/**
|
|
29
|
+
* `new Sequelize(url, { benchmark: true, logging: sequelizeLogging('postgresql') })`: a span per
|
|
30
|
+
* executed query (only lines with a timing, so `benchmark: true` is required). `next` gets every call.
|
|
31
|
+
* shortcut: failed queries are not logged by Sequelize with a timing, so they get no span.
|
|
32
|
+
*/
|
|
33
|
+
export declare function sequelizeLogging(system: string, next?: (sql: string, ...rest: unknown[]) => void, opts?: DbOptions): (sql: string, ...rest: unknown[]) => void;
|
|
34
|
+
interface MikroLogContext {
|
|
35
|
+
query?: string;
|
|
36
|
+
took?: number;
|
|
37
|
+
level?: string;
|
|
38
|
+
}
|
|
39
|
+
interface MikroLogger {
|
|
40
|
+
logQuery(context: MikroLogContext): void;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* `MikroORM.init({ loggerFactory: mikroOrmLoggerFactory(DefaultLogger, 'postgresql') })`: a span per
|
|
44
|
+
* query MikroORM reports (with its `took`), then the `Base` logger logs as usual.
|
|
45
|
+
*/
|
|
46
|
+
export declare function mikroOrmLoggerFactory<O, L extends MikroLogger>(Base: new (options: O) => L, system: string, opts?: DbOptions): (options: O) => L;
|
|
47
|
+
export {};
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// ORM query spans: summary (`SELECT users`), `db.query.hash` and the N+1 hint from the shared core.
|
|
2
|
+
// The statement text is never recorded raw (with `queryText`, literal-free only).
|
|
3
|
+
import { patch, recordDbSpan, traced } from './db.js';
|
|
4
|
+
const PREPARED = ['execute', 'all', 'get', 'run', 'values'];
|
|
5
|
+
/**
|
|
6
|
+
* `instrumentDrizzle(db, 'postgresql')`: a CLIENT span per query a Drizzle database runs (`system`
|
|
7
|
+
* is the `db.system.name`: postgresql, mysql, sqlite, ...). Call once per `drizzle(...)` instance.
|
|
8
|
+
* shortcut: `db.transaction(tx => ...)` runs on its own session and gets no spans; instrument the
|
|
9
|
+
* driver as well if transactions matter.
|
|
10
|
+
*/
|
|
11
|
+
export function instrumentDrizzle(db, system, opts) {
|
|
12
|
+
const d = db;
|
|
13
|
+
const session = d.session ?? d._?.session;
|
|
14
|
+
if (!session)
|
|
15
|
+
return;
|
|
16
|
+
patch(session, 'prepareQuery', (orig) => function (query, ...rest) {
|
|
17
|
+
const pq = orig.call(this, query, ...rest);
|
|
18
|
+
if (pq && typeof pq === 'object') {
|
|
19
|
+
for (const m of PREPARED) {
|
|
20
|
+
patch(pq, m, (o) => function (...a) {
|
|
21
|
+
return traced({ system, sql: query?.sql }, opts, () => o.apply(this, a));
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return pq;
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
/** `new Kysely({ dialect, log: kyselyLog('postgresql') })`; `next` still gets every event. */
|
|
29
|
+
export function kyselyLog(system, next, opts) {
|
|
30
|
+
return (e) => {
|
|
31
|
+
recordDbSpan({ system, sql: e.query?.sql }, opts, e.queryDurationMillis, e.level === 'error' ? e.error ?? new Error() : undefined);
|
|
32
|
+
next?.(e);
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
const TYPEORM = {
|
|
36
|
+
postgres: 'postgresql', 'aurora-postgres': 'postgresql', cockroachdb: 'cockroachdb',
|
|
37
|
+
mysql: 'mysql', 'aurora-mysql': 'mysql', mariadb: 'mariadb', mssql: 'microsoft.sql_server',
|
|
38
|
+
oracle: 'oracle.db', sap: 'sap.hana', spanner: 'gcp.spanner', 'better-sqlite3': 'sqlite', sqlite: 'sqlite',
|
|
39
|
+
sqljs: 'sqlite', capacitor: 'sqlite', cordova: 'sqlite', expo: 'sqlite', nativescript: 'sqlite', 'react-native': 'sqlite',
|
|
40
|
+
};
|
|
41
|
+
/** `instrumentTypeORM(dataSource)` (before or after `initialize`): a CLIENT span per `QueryRunner.query`. */
|
|
42
|
+
export function instrumentTypeORM(dataSource, opts) {
|
|
43
|
+
const { type, database } = dataSource.options;
|
|
44
|
+
const system = TYPEORM[type] ?? type;
|
|
45
|
+
const namespace = typeof database === 'string' ? database : undefined;
|
|
46
|
+
patch(dataSource.driver, 'createQueryRunner', (orig) => function (...args) {
|
|
47
|
+
const runner = orig.apply(this, args);
|
|
48
|
+
if (runner && typeof runner === 'object') {
|
|
49
|
+
patch(runner, 'query', (q) => function (sql, ...rest) {
|
|
50
|
+
return traced({ system, sql, namespace }, opts, () => q.call(this, sql, ...rest));
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
return runner;
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
const EXECUTED = /^Execut(?:ed|ing) \([^)]*\):\s*/;
|
|
57
|
+
/**
|
|
58
|
+
* `new Sequelize(url, { benchmark: true, logging: sequelizeLogging('postgresql') })`: a span per
|
|
59
|
+
* executed query (only lines with a timing, so `benchmark: true` is required). `next` gets every call.
|
|
60
|
+
* shortcut: failed queries are not logged by Sequelize with a timing, so they get no span.
|
|
61
|
+
*/
|
|
62
|
+
export function sequelizeLogging(system, next, opts) {
|
|
63
|
+
return (sql, ...rest) => {
|
|
64
|
+
if (typeof rest[0] === 'number' && typeof sql === 'string') {
|
|
65
|
+
recordDbSpan({ system, sql: sql.replace(EXECUTED, '') }, opts, rest[0]);
|
|
66
|
+
}
|
|
67
|
+
next?.(sql, ...rest);
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* `MikroORM.init({ loggerFactory: mikroOrmLoggerFactory(DefaultLogger, 'postgresql') })`: a span per
|
|
72
|
+
* query MikroORM reports (with its `took`), then the `Base` logger logs as usual.
|
|
73
|
+
*/
|
|
74
|
+
export function mikroOrmLoggerFactory(Base, system, opts) {
|
|
75
|
+
return (options) => {
|
|
76
|
+
const logger = new Base(options);
|
|
77
|
+
const logQuery = logger.logQuery.bind(logger);
|
|
78
|
+
logger.logQuery = (c) => {
|
|
79
|
+
if (c && typeof c.query === 'string' && typeof c.took === 'number') {
|
|
80
|
+
recordDbSpan({ system, sql: c.query }, opts, c.took, c.level === 'error' ? new Error() : undefined);
|
|
81
|
+
}
|
|
82
|
+
logQuery(c);
|
|
83
|
+
};
|
|
84
|
+
return logger;
|
|
85
|
+
};
|
|
86
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `instrumentPgBoss(boss)`: PRODUCER spans on `send` and `insert` (context in job data under `_wt`,
|
|
3
|
+
* object data only), and a CONSUMER span around each `work` handler call, child of the producer for
|
|
4
|
+
* a single job and linked to every job's producer for a batch. `_wt` is removed before the handler
|
|
5
|
+
* sees `job.data`. `messaging.system` is `pg-boss` (OTel has no value for it).
|
|
6
|
+
* shortcut: sendAfter/sendThrottled/sendDebounced/sendSingleton bypass `send`; their jobs carry no context.
|
|
7
|
+
*/
|
|
8
|
+
export declare function instrumentPgBoss(boss: object): void;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { trace } from '@opentelemetry/api';
|
|
2
|
+
import { patch } from './db.js';
|
|
3
|
+
import { consume, injectData, produce, takeContext } from './messaging.js';
|
|
4
|
+
const attrs = (queue, type, op) => ({
|
|
5
|
+
'messaging.system': 'pg-boss',
|
|
6
|
+
'messaging.destination.name': queue,
|
|
7
|
+
'messaging.operation.type': type,
|
|
8
|
+
'messaging.operation.name': op,
|
|
9
|
+
});
|
|
10
|
+
/** Run `call` and set the returned job id on the active (producer) span. */
|
|
11
|
+
function withId(call) {
|
|
12
|
+
const span = trace.getActiveSpan();
|
|
13
|
+
return call().then((id) => {
|
|
14
|
+
if (typeof id === 'string')
|
|
15
|
+
span?.setAttribute('messaging.message.id', id);
|
|
16
|
+
return id;
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* `instrumentPgBoss(boss)`: PRODUCER spans on `send` and `insert` (context in job data under `_wt`,
|
|
21
|
+
* object data only), and a CONSUMER span around each `work` handler call, child of the producer for
|
|
22
|
+
* a single job and linked to every job's producer for a batch. `_wt` is removed before the handler
|
|
23
|
+
* sees `job.data`. `messaging.system` is `pg-boss` (OTel has no value for it).
|
|
24
|
+
* shortcut: sendAfter/sendThrottled/sendDebounced/sendSingleton bypass `send`; their jobs carry no context.
|
|
25
|
+
*/
|
|
26
|
+
export function instrumentPgBoss(boss) {
|
|
27
|
+
patch(boss, 'send', (orig) => function (nameOrReq, data, ...rest) {
|
|
28
|
+
if (typeof nameOrReq === 'object' && nameOrReq) {
|
|
29
|
+
const req = nameOrReq;
|
|
30
|
+
return produce(`send ${req.name}`, attrs(req.name, 'send', 'send'), () => withId(() => orig.call(this, { ...req, data: injectData(req.data) }, data, ...rest)));
|
|
31
|
+
}
|
|
32
|
+
return produce(`send ${nameOrReq}`, attrs(nameOrReq, 'send', 'send'), () => withId(() => orig.call(this, nameOrReq, injectData(data), ...rest)));
|
|
33
|
+
});
|
|
34
|
+
// v9: insert(jobs[]); v10: insert(name, jobs[])
|
|
35
|
+
patch(boss, 'insert', (orig) => function (a, b, ...rest) {
|
|
36
|
+
const jobs = (Array.isArray(a) ? a : b);
|
|
37
|
+
const queue = typeof a === 'string' ? a : jobs?.[0]?.name ?? '';
|
|
38
|
+
const at = { ...attrs(queue, 'send', 'insert'), 'messaging.batch.message_count': jobs?.length ?? 0 };
|
|
39
|
+
return produce(`send ${queue}`, at, () => {
|
|
40
|
+
const out = jobs?.map((j) => ({ ...j, data: injectData(j.data) }));
|
|
41
|
+
return Array.isArray(a) ? orig.call(this, out, b, ...rest) : orig.call(this, a, out, ...rest);
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
patch(boss, 'work', (orig) => function (name, ...rest) {
|
|
45
|
+
const i = rest.length - 1;
|
|
46
|
+
const handler = rest[i];
|
|
47
|
+
if (typeof handler !== 'function')
|
|
48
|
+
return orig.call(this, name, ...rest);
|
|
49
|
+
rest[i] = function (arg) {
|
|
50
|
+
const jobs = Array.isArray(arg) ? arg : [arg];
|
|
51
|
+
const a = attrs(name, 'process', 'work');
|
|
52
|
+
if (jobs.length === 1 && jobs[0]?.id)
|
|
53
|
+
a['messaging.message.id'] = jobs[0].id;
|
|
54
|
+
if (Array.isArray(arg))
|
|
55
|
+
a['messaging.batch.message_count'] = arg.length;
|
|
56
|
+
return consume(`process ${name}`, a, jobs.map((j) => takeContext(j?.data)), () => handler.call(this, arg));
|
|
57
|
+
};
|
|
58
|
+
return orig.call(this, name, ...rest);
|
|
59
|
+
});
|
|
60
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { observePool } from './db.js';
|
|
2
|
+
import type { PoolState } from './db.js';
|
|
3
|
+
export { observePool };
|
|
4
|
+
export type { PoolState };
|
|
5
|
+
interface Emitter {
|
|
6
|
+
on(event: string, fn: (e: any) => void): unknown;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* `instrumentMongoPool(client)` on a `MongoClient` (before or after `connect`): pool gauges per
|
|
10
|
+
* server address from the driver's CMAP events, wait time (driver >= 6.9 reports `durationMS`),
|
|
11
|
+
* use time (check-out to check-in) and check-out timeouts. Call after `init`.
|
|
12
|
+
*/
|
|
13
|
+
export declare function instrumentMongoPool(client: Emitter): void;
|
|
14
|
+
interface Mysql2CorePool extends Emitter {
|
|
15
|
+
_allConnections?: {
|
|
16
|
+
length: number;
|
|
17
|
+
};
|
|
18
|
+
_freeConnections?: {
|
|
19
|
+
length: number;
|
|
20
|
+
};
|
|
21
|
+
_connectionQueue?: {
|
|
22
|
+
length: number;
|
|
23
|
+
};
|
|
24
|
+
config?: {
|
|
25
|
+
connectionLimit?: number;
|
|
26
|
+
connectionConfig?: {
|
|
27
|
+
host?: string;
|
|
28
|
+
port?: number;
|
|
29
|
+
database?: string;
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* `instrumentMysql2Pool(pool)` on a mysql2 pool (callback or `.promise()` pool): used/idle/pending
|
|
35
|
+
* connections and the limit, plus use time (acquire to release). Name defaults to `host:port/db`.
|
|
36
|
+
* shortcut: no wait time or timeouts (mysql2 has no acquire timeout and does not report queue wait).
|
|
37
|
+
* Returns `stop()`.
|
|
38
|
+
*/
|
|
39
|
+
export declare function instrumentMysql2Pool(pool: Mysql2CorePool | {
|
|
40
|
+
pool: Mysql2CorePool;
|
|
41
|
+
}, name?: string): () => void;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// Connection-pool metrics (OTel db semconv) for drivers whose pools upstream does not cover.
|
|
2
|
+
import { observePool } from './db.js';
|
|
3
|
+
export { observePool };
|
|
4
|
+
/**
|
|
5
|
+
* `instrumentMongoPool(client)` on a `MongoClient` (before or after `connect`): pool gauges per
|
|
6
|
+
* server address from the driver's CMAP events, wait time (driver >= 6.9 reports `durationMS`),
|
|
7
|
+
* use time (check-out to check-in) and check-out timeouts. Call after `init`.
|
|
8
|
+
*/
|
|
9
|
+
export function instrumentMongoPool(client) {
|
|
10
|
+
const pools = new Map();
|
|
11
|
+
const out = new Map(); // `${address}#${connectionId}` -> checked out at (ms)
|
|
12
|
+
const get = (address) => {
|
|
13
|
+
let p = pools.get(address);
|
|
14
|
+
if (!p) {
|
|
15
|
+
const s = { used: 0, pending: 0, total: 0 };
|
|
16
|
+
s.rec = observePool('mongodb', address, () => ({
|
|
17
|
+
used: s.used, idle: Math.max(0, s.total - s.used), pending: s.pending, max: s.max,
|
|
18
|
+
}));
|
|
19
|
+
pools.set(address, (p = s));
|
|
20
|
+
}
|
|
21
|
+
return p;
|
|
22
|
+
};
|
|
23
|
+
const dec = (n) => Math.max(0, n - 1);
|
|
24
|
+
client.on('connectionPoolCreated', (e) => {
|
|
25
|
+
const max = e.options?.maxPoolSize;
|
|
26
|
+
if (typeof max === 'number')
|
|
27
|
+
get(e.address).max = max;
|
|
28
|
+
});
|
|
29
|
+
client.on('connectionPoolClosed', (e) => {
|
|
30
|
+
pools.get(e.address)?.rec.stop();
|
|
31
|
+
pools.delete(e.address);
|
|
32
|
+
});
|
|
33
|
+
client.on('connectionCreated', (e) => { get(e.address).total++; });
|
|
34
|
+
client.on('connectionClosed', (e) => { const p = get(e.address); p.total = dec(p.total); });
|
|
35
|
+
client.on('connectionCheckOutStarted', (e) => { get(e.address).pending++; });
|
|
36
|
+
client.on('connectionCheckOutFailed', (e) => {
|
|
37
|
+
const p = get(e.address);
|
|
38
|
+
p.pending = dec(p.pending);
|
|
39
|
+
if (e.reason === 'timeout')
|
|
40
|
+
p.rec.timeout();
|
|
41
|
+
if (typeof e.durationMS === 'number')
|
|
42
|
+
p.rec.wait(e.durationMS / 1000);
|
|
43
|
+
});
|
|
44
|
+
client.on('connectionCheckedOut', (e) => {
|
|
45
|
+
const p = get(e.address);
|
|
46
|
+
p.pending = dec(p.pending);
|
|
47
|
+
p.used++;
|
|
48
|
+
if (typeof e.durationMS === 'number')
|
|
49
|
+
p.rec.wait(e.durationMS / 1000);
|
|
50
|
+
out.set(`${e.address}#${e.connectionId}`, Date.now());
|
|
51
|
+
});
|
|
52
|
+
client.on('connectionCheckedIn', (e) => {
|
|
53
|
+
const p = get(e.address);
|
|
54
|
+
p.used = dec(p.used);
|
|
55
|
+
const k = `${e.address}#${e.connectionId}`;
|
|
56
|
+
const t = out.get(k);
|
|
57
|
+
if (t !== undefined) {
|
|
58
|
+
out.delete(k);
|
|
59
|
+
p.rec.use((Date.now() - t) / 1000);
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* `instrumentMysql2Pool(pool)` on a mysql2 pool (callback or `.promise()` pool): used/idle/pending
|
|
65
|
+
* connections and the limit, plus use time (acquire to release). Name defaults to `host:port/db`.
|
|
66
|
+
* shortcut: no wait time or timeouts (mysql2 has no acquire timeout and does not report queue wait).
|
|
67
|
+
* Returns `stop()`.
|
|
68
|
+
*/
|
|
69
|
+
export function instrumentMysql2Pool(pool, name) {
|
|
70
|
+
const core = ('pool' in pool && pool.pool ? pool.pool : pool);
|
|
71
|
+
const c = core.config?.connectionConfig;
|
|
72
|
+
const pname = name ?? (c ? `${c.host ?? 'localhost'}:${c.port ?? 3306}${c.database ? `/${c.database}` : ''}` : 'mysql2');
|
|
73
|
+
const rec = observePool('mysql', pname, () => {
|
|
74
|
+
const all = core._allConnections?.length ?? 0;
|
|
75
|
+
const free = core._freeConnections?.length ?? 0;
|
|
76
|
+
return { used: all - free, idle: free, pending: core._connectionQueue?.length ?? 0, max: core.config?.connectionLimit };
|
|
77
|
+
});
|
|
78
|
+
const acquired = new WeakMap();
|
|
79
|
+
core.on('acquire', (conn) => { if (conn)
|
|
80
|
+
acquired.set(conn, Date.now()); });
|
|
81
|
+
core.on('release', (conn) => {
|
|
82
|
+
const t = conn && acquired.get(conn);
|
|
83
|
+
if (t) {
|
|
84
|
+
acquired.delete(conn);
|
|
85
|
+
rec.use((Date.now() - t) / 1000);
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
return rec.stop;
|
|
89
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { querySummary } from '../sql.js';
|
|
2
|
+
import type { DbOptions } from './db.js';
|
|
3
|
+
export { querySummary };
|
|
4
|
+
/**
|
|
5
|
+
* postgres.js has no OTel instrumentation; since 4.0 it publishes each query on the
|
|
6
|
+
* `postgres:query` diagnostics tracing channel. This turns those into CLIENT spans (child of the
|
|
7
|
+
* active span) with `db.system=postgresql`, the operation, a summary and `db.query.hash` (no
|
|
8
|
+
* statement text or parameters unless `opts.queryText`, then literal-free) and the SQLSTATE on
|
|
9
|
+
* failure. Works for ESM and CJS; returns an unsubscribe function (a no-op when this Node has no
|
|
10
|
+
* tracing channels).
|
|
11
|
+
*/
|
|
12
|
+
export declare function instrumentPostgres(opts?: DbOptions): () => void;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import * as dc from 'node:diagnostics_channel';
|
|
2
|
+
import { context, SpanStatusCode } from '@opentelemetry/api';
|
|
3
|
+
import { querySummary } from '../sql.js';
|
|
4
|
+
import { startDbSpan } from './db.js';
|
|
5
|
+
export { querySummary };
|
|
6
|
+
/**
|
|
7
|
+
* postgres.js has no OTel instrumentation; since 4.0 it publishes each query on the
|
|
8
|
+
* `postgres:query` diagnostics tracing channel. This turns those into CLIENT spans (child of the
|
|
9
|
+
* active span) with `db.system=postgresql`, the operation, a summary and `db.query.hash` (no
|
|
10
|
+
* statement text or parameters unless `opts.queryText`, then literal-free) and the SQLSTATE on
|
|
11
|
+
* failure. Works for ESM and CJS; returns an unsubscribe function (a no-op when this Node has no
|
|
12
|
+
* tracing channels).
|
|
13
|
+
*/
|
|
14
|
+
export function instrumentPostgres(opts) {
|
|
15
|
+
if (typeof dc.tracingChannel !== 'function')
|
|
16
|
+
return () => { };
|
|
17
|
+
const ch = dc.tracingChannel('postgres:query');
|
|
18
|
+
const spans = new WeakMap();
|
|
19
|
+
const handlers = {
|
|
20
|
+
start(c) {
|
|
21
|
+
spans.set(c, startDbSpan({
|
|
22
|
+
system: 'postgresql', sql: c.query, namespace: c.database, attributes: { 'db.system': 'postgresql' },
|
|
23
|
+
}, opts, context.active()));
|
|
24
|
+
},
|
|
25
|
+
end() { },
|
|
26
|
+
asyncStart() { },
|
|
27
|
+
error(c) {
|
|
28
|
+
const span = spans.get(c);
|
|
29
|
+
if (!span)
|
|
30
|
+
return;
|
|
31
|
+
const code = c.error?.code;
|
|
32
|
+
if (typeof code === 'string') {
|
|
33
|
+
span.setAttribute('db.response.status_code', code);
|
|
34
|
+
span.setAttribute('error.type', code);
|
|
35
|
+
}
|
|
36
|
+
// the message can quote values (e.g. a duplicate key), so it is not recorded
|
|
37
|
+
span.setStatus({ code: SpanStatusCode.ERROR, message: typeof code === 'string' ? `SQLSTATE ${code}` : undefined });
|
|
38
|
+
},
|
|
39
|
+
asyncEnd(c) {
|
|
40
|
+
const span = spans.get(c);
|
|
41
|
+
if (!span)
|
|
42
|
+
return;
|
|
43
|
+
spans.delete(c);
|
|
44
|
+
if (c.host)
|
|
45
|
+
span.setAttribute('server.address', c.host);
|
|
46
|
+
if (c.port)
|
|
47
|
+
span.setAttribute('server.port', c.port);
|
|
48
|
+
if (typeof c.count === 'number')
|
|
49
|
+
span.setAttribute('db.response.returned_rows', c.count);
|
|
50
|
+
span.end();
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
ch.subscribe(handlers);
|
|
54
|
+
return () => ch.unsubscribe(handlers);
|
|
55
|
+
}
|
|
@@ -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,84 @@
|
|
|
1
|
+
import * as nodePath from 'node:path';
|
|
2
|
+
import { observeEmbedded, patch, sizeOf, traced, tracedCb } from './db.js';
|
|
3
|
+
const STATEMENT = ['run', 'get', 'all'];
|
|
4
|
+
/** Trace `exec(sql)` and the run/get/all of statements `prepare` returns (their prototype is patched once). */
|
|
5
|
+
function patchSync(db, system, opts, sqlOf, nsOf) {
|
|
6
|
+
patch(db, 'exec', (orig) => function (sql, ...a) {
|
|
7
|
+
return traced({ system, sql, namespace: nsOf({ database: this }) }, opts, () => orig.call(this, sql, ...a));
|
|
8
|
+
});
|
|
9
|
+
patch(db, 'prepare', (orig) => function (...a) {
|
|
10
|
+
const stmt = orig.apply(this, a);
|
|
11
|
+
const proto = stmt && Object.getPrototypeOf(stmt);
|
|
12
|
+
if (proto) {
|
|
13
|
+
for (const m of STATEMENT) {
|
|
14
|
+
patch(proto, m, (o) => function (...args) {
|
|
15
|
+
return traced({ system, sql: sqlOf(this), namespace: nsOf(this) }, opts, () => o.apply(this, args));
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return stmt;
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
const base = (p) => (typeof p === 'string' && p && p !== ':memory:' ? nodePath.basename(p) : undefined);
|
|
23
|
+
/** `import * as sqlite from 'node:sqlite'; instrumentNodeSqlite(sqlite)`: spans for `DatabaseSync.exec`
|
|
24
|
+
* and prepared `run`/`get`/`all`. shortcut: `iterate()` is not traced (the span would end before the rows). */
|
|
25
|
+
export function instrumentNodeSqlite(sqlite, opts) {
|
|
26
|
+
patchSync(sqlite.DatabaseSync.prototype, 'sqlite', opts, (s) => s.sourceSQL, () => undefined);
|
|
27
|
+
}
|
|
28
|
+
/** `import Database from 'better-sqlite3'; instrumentBetterSqlite3(Database)`: spans for `exec` and
|
|
29
|
+
* prepared `run`/`get`/`all`, `db.namespace` = the database file name. */
|
|
30
|
+
export function instrumentBetterSqlite3(Database, opts) {
|
|
31
|
+
patchSync(Database.prototype, 'sqlite', opts, (s) => s.source, (s) => base(s.database?.name));
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Callback drivers (`sqlite3`, classic `duckdb`): trace `run`/`get`/`all`/`exec`/`each` on each
|
|
35
|
+
* prototype; the span ends in the callback, which runs in the caller's context.
|
|
36
|
+
* shortcut: a call without a callback ends its span when the call returns (no duration).
|
|
37
|
+
*/
|
|
38
|
+
export function patchCallbackDb(protos, system, opts) {
|
|
39
|
+
for (const proto of protos) {
|
|
40
|
+
for (const m of ['run', 'get', 'all', 'exec', 'each']) {
|
|
41
|
+
patch(proto, m, (orig) => function (...a) {
|
|
42
|
+
const op = { system, sql: typeof a[0] === 'string' ? a[0] : undefined, namespace: base(this.filename) };
|
|
43
|
+
const call = (args) => orig.apply(this, args);
|
|
44
|
+
// each(sql, ..., row, complete): only the completion callback ends the span
|
|
45
|
+
const cb = m !== 'each' || typeof a[a.length - 2] === 'function';
|
|
46
|
+
return cb ? tracedCb(op, opts, a, call) : traced(op, opts, () => call(a));
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/** `import sqlite3 from 'sqlite3'; instrumentSqlite3(sqlite3)`: spans for `Database` run/get/all/exec/each.
|
|
52
|
+
* shortcut: `Statement` objects from `db.prepare` are not traced. */
|
|
53
|
+
export function instrumentSqlite3(sqlite3, opts) {
|
|
54
|
+
patchCallbackDb([sqlite3.Database.prototype], 'sqlite', opts);
|
|
55
|
+
}
|
|
56
|
+
/** `instrumentLibsql(createClient({ url }))`: spans for `execute`, `batch`, `executeMultiple` and
|
|
57
|
+
* the same calls on transactions it opens. `db.system.name=sqlite`. Returns the client. */
|
|
58
|
+
export function instrumentLibsql(client, opts) {
|
|
59
|
+
const sqlOf = (s) => (typeof s === 'string' ? s : s?.sql);
|
|
60
|
+
const wrap = (c) => {
|
|
61
|
+
patch(c, 'execute', (orig) => function (stmt, ...a) {
|
|
62
|
+
return traced({ system: 'sqlite', sql: sqlOf(stmt) }, opts, () => orig.call(this, stmt, ...a));
|
|
63
|
+
});
|
|
64
|
+
patch(c, 'executeMultiple', (orig) => function (sql) {
|
|
65
|
+
return traced({ system: 'sqlite', sql }, opts, () => orig.call(this, sql));
|
|
66
|
+
});
|
|
67
|
+
patch(c, 'batch', (orig) => function (stmts, ...a) {
|
|
68
|
+
return traced({ system: 'sqlite', operation: 'BATCH',
|
|
69
|
+
attributes: { 'db.operation.batch.size': stmts?.length ?? 0 } }, opts, () => orig.call(this, stmts, ...a));
|
|
70
|
+
});
|
|
71
|
+
};
|
|
72
|
+
wrap(client);
|
|
73
|
+
patch(client, 'transaction', (orig) => async function (...a) {
|
|
74
|
+
const tx = await orig.apply(this, a);
|
|
75
|
+
if (tx)
|
|
76
|
+
wrap(tx);
|
|
77
|
+
return tx;
|
|
78
|
+
});
|
|
79
|
+
return client;
|
|
80
|
+
}
|
|
81
|
+
/** `wt.embedded.file.size` and `wt.embedded.wal.size` (the `-wal` file) of a SQLite database file. Returns `stop()`. */
|
|
82
|
+
export function observeSqliteFile(path) {
|
|
83
|
+
return observeEmbedded('sqlite', nodePath.basename(path), ['file.size', 'wal.size'], () => ({ 'file.size': sizeOf(path), 'wal.size': sizeOf(`${path}-wal`) }));
|
|
84
|
+
}
|