@watchtowerobservability/node 0.0.0-stage → 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/LICENSE +202 -0
- package/README.md +304 -2
- package/dist/cjs/appsec.d.ts +95 -0
- package/dist/cjs/appsec.js +206 -0
- package/dist/cjs/buffer.d.ts +49 -0
- package/dist/cjs/buffer.js +134 -0
- package/dist/cjs/errors.d.ts +17 -0
- package/dist/cjs/errors.js +55 -0
- package/dist/cjs/index.d.ts +123 -0
- package/dist/cjs/index.js +315 -0
- package/dist/cjs/integrations/amqp.d.ts +13 -0
- package/dist/cjs/integrations/amqp.js +96 -0
- package/dist/cjs/integrations/azure-functions.d.ts +18 -0
- package/dist/cjs/integrations/azure-functions.js +97 -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/express.d.ts +25 -0
- package/dist/cjs/integrations/express.js +58 -0
- package/dist/cjs/integrations/fastify.d.ts +6 -0
- package/dist/cjs/integrations/fastify.js +44 -0
- package/dist/cjs/integrations/genai-agents.d.ts +36 -0
- package/dist/cjs/integrations/genai-agents.js +124 -0
- package/dist/cjs/integrations/genai-stream.d.ts +19 -0
- package/dist/cjs/integrations/genai-stream.js +135 -0
- package/dist/cjs/integrations/genai.d.ts +18 -0
- package/dist/cjs/integrations/genai.js +150 -0
- package/dist/cjs/integrations/graphile-worker.d.ts +22 -0
- package/dist/cjs/integrations/graphile-worker.js +50 -0
- package/dist/cjs/integrations/koa.d.ts +19 -0
- package/dist/cjs/integrations/koa.js +34 -0
- package/dist/cjs/integrations/lambda.d.ts +11 -0
- package/dist/cjs/integrations/lambda.js +68 -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/logs.d.ts +10 -0
- package/dist/cjs/integrations/logs.js +77 -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/nestjs.d.ts +16 -0
- package/dist/cjs/integrations/nestjs.js +30 -0
- package/dist/cjs/integrations/next.d.ts +11 -0
- package/dist/cjs/integrations/next.js +59 -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/prisma.d.ts +3 -0
- package/dist/cjs/integrations/prisma.js +33 -0
- package/dist/cjs/integrations/sqlite.d.ts +36 -0
- package/dist/cjs/integrations/sqlite.js +125 -0
- package/dist/cjs/integrations/util.d.ts +9 -0
- package/dist/cjs/integrations/util.js +37 -0
- package/dist/cjs/jest.d.ts +21 -0
- package/dist/cjs/jest.js +45 -0
- package/dist/cjs/load.d.ts +1 -0
- package/dist/cjs/load.js +11 -0
- package/dist/cjs/metrics.d.ts +10 -0
- package/dist/cjs/metrics.js +81 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/payload.d.ts +42 -0
- package/dist/cjs/payload.js +145 -0
- package/dist/cjs/pprof.d.ts +6 -0
- package/dist/cjs/pprof.js +88 -0
- package/dist/cjs/probes.d.ts +97 -0
- package/dist/cjs/probes.js +422 -0
- package/dist/cjs/profiling.d.ts +84 -0
- package/dist/cjs/profiling.js +263 -0
- package/dist/cjs/reporter-core.d.ts +32 -0
- package/dist/cjs/reporter-core.js +102 -0
- package/dist/cjs/sql.d.ts +19 -0
- package/dist/cjs/sql.js +332 -0
- package/dist/cjs/test-plan.d.ts +102 -0
- package/dist/cjs/test-plan.js +352 -0
- package/dist/cjs/vitest.d.ts +27 -0
- package/dist/cjs/vitest.js +52 -0
- package/dist/esm/appsec.d.ts +95 -0
- package/dist/esm/appsec.js +193 -0
- package/dist/esm/buffer.d.ts +49 -0
- package/dist/esm/buffer.js +92 -0
- package/dist/esm/errors.d.ts +17 -0
- package/dist/esm/errors.js +50 -0
- package/dist/esm/index.d.ts +123 -0
- package/dist/esm/index.js +264 -0
- package/dist/esm/integrations/amqp.d.ts +13 -0
- package/dist/esm/integrations/amqp.js +92 -0
- package/dist/esm/integrations/azure-functions.d.ts +18 -0
- package/dist/esm/integrations/azure-functions.js +60 -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/express.d.ts +25 -0
- package/dist/esm/integrations/express.js +54 -0
- package/dist/esm/integrations/fastify.d.ts +6 -0
- package/dist/esm/integrations/fastify.js +41 -0
- package/dist/esm/integrations/genai-agents.d.ts +36 -0
- package/dist/esm/integrations/genai-agents.js +118 -0
- package/dist/esm/integrations/genai-stream.d.ts +19 -0
- package/dist/esm/integrations/genai-stream.js +130 -0
- package/dist/esm/integrations/genai.d.ts +18 -0
- package/dist/esm/integrations/genai.js +138 -0
- package/dist/esm/integrations/graphile-worker.d.ts +22 -0
- package/dist/esm/integrations/graphile-worker.js +46 -0
- package/dist/esm/integrations/koa.d.ts +19 -0
- package/dist/esm/integrations/koa.js +31 -0
- package/dist/esm/integrations/lambda.d.ts +11 -0
- package/dist/esm/integrations/lambda.js +32 -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/logs.d.ts +10 -0
- package/dist/esm/integrations/logs.js +40 -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/nestjs.d.ts +16 -0
- package/dist/esm/integrations/nestjs.js +26 -0
- package/dist/esm/integrations/next.d.ts +11 -0
- package/dist/esm/integrations/next.js +22 -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/prisma.d.ts +3 -0
- package/dist/esm/integrations/prisma.js +30 -0
- package/dist/esm/integrations/sqlite.d.ts +36 -0
- package/dist/esm/integrations/sqlite.js +84 -0
- package/dist/esm/integrations/util.d.ts +9 -0
- package/dist/esm/integrations/util.js +32 -0
- package/dist/esm/jest.d.ts +21 -0
- package/dist/esm/jest.js +41 -0
- package/dist/esm/load.d.ts +1 -0
- package/dist/esm/load.js +5 -0
- package/dist/esm/metrics.d.ts +10 -0
- package/dist/esm/metrics.js +44 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/payload.d.ts +42 -0
- package/dist/esm/payload.js +131 -0
- package/dist/esm/pprof.d.ts +6 -0
- package/dist/esm/pprof.js +85 -0
- package/dist/esm/probes.d.ts +97 -0
- package/dist/esm/probes.js +412 -0
- package/dist/esm/profiling.d.ts +84 -0
- package/dist/esm/profiling.js +251 -0
- package/dist/esm/reporter-core.d.ts +32 -0
- package/dist/esm/reporter-core.js +63 -0
- package/dist/esm/sql.d.ts +19 -0
- package/dist/esm/sql.js +324 -0
- package/dist/esm/test-plan.d.ts +102 -0
- package/dist/esm/test-plan.js +336 -0
- package/dist/esm/vitest.d.ts +27 -0
- package/dist/esm/vitest.js +48 -0
- package/package.json +379 -4
|
@@ -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,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.instrumentDrizzle = instrumentDrizzle;
|
|
4
|
+
exports.kyselyLog = kyselyLog;
|
|
5
|
+
exports.instrumentTypeORM = instrumentTypeORM;
|
|
6
|
+
exports.sequelizeLogging = sequelizeLogging;
|
|
7
|
+
exports.mikroOrmLoggerFactory = mikroOrmLoggerFactory;
|
|
8
|
+
// ORM query spans: summary (`SELECT users`), `db.query.hash` and the N+1 hint from the shared core.
|
|
9
|
+
// The statement text is never recorded raw (with `queryText`, literal-free only).
|
|
10
|
+
const db_js_1 = require("./db.js");
|
|
11
|
+
const PREPARED = ['execute', 'all', 'get', 'run', 'values'];
|
|
12
|
+
/**
|
|
13
|
+
* `instrumentDrizzle(db, 'postgresql')`: a CLIENT span per query a Drizzle database runs (`system`
|
|
14
|
+
* is the `db.system.name`: postgresql, mysql, sqlite, ...). Call once per `drizzle(...)` instance.
|
|
15
|
+
* shortcut: `db.transaction(tx => ...)` runs on its own session and gets no spans; instrument the
|
|
16
|
+
* driver as well if transactions matter.
|
|
17
|
+
*/
|
|
18
|
+
function instrumentDrizzle(db, system, opts) {
|
|
19
|
+
const d = db;
|
|
20
|
+
const session = d.session ?? d._?.session;
|
|
21
|
+
if (!session)
|
|
22
|
+
return;
|
|
23
|
+
(0, db_js_1.patch)(session, 'prepareQuery', (orig) => function (query, ...rest) {
|
|
24
|
+
const pq = orig.call(this, query, ...rest);
|
|
25
|
+
if (pq && typeof pq === 'object') {
|
|
26
|
+
for (const m of PREPARED) {
|
|
27
|
+
(0, db_js_1.patch)(pq, m, (o) => function (...a) {
|
|
28
|
+
return (0, db_js_1.traced)({ system, sql: query?.sql }, opts, () => o.apply(this, a));
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return pq;
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
/** `new Kysely({ dialect, log: kyselyLog('postgresql') })`; `next` still gets every event. */
|
|
36
|
+
function kyselyLog(system, next, opts) {
|
|
37
|
+
return (e) => {
|
|
38
|
+
(0, db_js_1.recordDbSpan)({ system, sql: e.query?.sql }, opts, e.queryDurationMillis, e.level === 'error' ? e.error ?? new Error() : undefined);
|
|
39
|
+
next?.(e);
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
const TYPEORM = {
|
|
43
|
+
postgres: 'postgresql', 'aurora-postgres': 'postgresql', cockroachdb: 'cockroachdb',
|
|
44
|
+
mysql: 'mysql', 'aurora-mysql': 'mysql', mariadb: 'mariadb', mssql: 'microsoft.sql_server',
|
|
45
|
+
oracle: 'oracle.db', sap: 'sap.hana', spanner: 'gcp.spanner', 'better-sqlite3': 'sqlite', sqlite: 'sqlite',
|
|
46
|
+
sqljs: 'sqlite', capacitor: 'sqlite', cordova: 'sqlite', expo: 'sqlite', nativescript: 'sqlite', 'react-native': 'sqlite',
|
|
47
|
+
};
|
|
48
|
+
/** `instrumentTypeORM(dataSource)` (before or after `initialize`): a CLIENT span per `QueryRunner.query`. */
|
|
49
|
+
function instrumentTypeORM(dataSource, opts) {
|
|
50
|
+
const { type, database } = dataSource.options;
|
|
51
|
+
const system = TYPEORM[type] ?? type;
|
|
52
|
+
const namespace = typeof database === 'string' ? database : undefined;
|
|
53
|
+
(0, db_js_1.patch)(dataSource.driver, 'createQueryRunner', (orig) => function (...args) {
|
|
54
|
+
const runner = orig.apply(this, args);
|
|
55
|
+
if (runner && typeof runner === 'object') {
|
|
56
|
+
(0, db_js_1.patch)(runner, 'query', (q) => function (sql, ...rest) {
|
|
57
|
+
return (0, db_js_1.traced)({ system, sql, namespace }, opts, () => q.call(this, sql, ...rest));
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
return runner;
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
const EXECUTED = /^Execut(?:ed|ing) \([^)]*\):\s*/;
|
|
64
|
+
/**
|
|
65
|
+
* `new Sequelize(url, { benchmark: true, logging: sequelizeLogging('postgresql') })`: a span per
|
|
66
|
+
* executed query (only lines with a timing, so `benchmark: true` is required). `next` gets every call.
|
|
67
|
+
* shortcut: failed queries are not logged by Sequelize with a timing, so they get no span.
|
|
68
|
+
*/
|
|
69
|
+
function sequelizeLogging(system, next, opts) {
|
|
70
|
+
return (sql, ...rest) => {
|
|
71
|
+
if (typeof rest[0] === 'number' && typeof sql === 'string') {
|
|
72
|
+
(0, db_js_1.recordDbSpan)({ system, sql: sql.replace(EXECUTED, '') }, opts, rest[0]);
|
|
73
|
+
}
|
|
74
|
+
next?.(sql, ...rest);
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* `MikroORM.init({ loggerFactory: mikroOrmLoggerFactory(DefaultLogger, 'postgresql') })`: a span per
|
|
79
|
+
* query MikroORM reports (with its `took`), then the `Base` logger logs as usual.
|
|
80
|
+
*/
|
|
81
|
+
function mikroOrmLoggerFactory(Base, system, opts) {
|
|
82
|
+
return (options) => {
|
|
83
|
+
const logger = new Base(options);
|
|
84
|
+
const logQuery = logger.logQuery.bind(logger);
|
|
85
|
+
logger.logQuery = (c) => {
|
|
86
|
+
if (c && typeof c.query === 'string' && typeof c.took === 'number') {
|
|
87
|
+
(0, db_js_1.recordDbSpan)({ system, sql: c.query }, opts, c.took, c.level === 'error' ? new Error() : undefined);
|
|
88
|
+
}
|
|
89
|
+
logQuery(c);
|
|
90
|
+
};
|
|
91
|
+
return logger;
|
|
92
|
+
};
|
|
93
|
+
}
|
|
@@ -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,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.instrumentPgBoss = instrumentPgBoss;
|
|
4
|
+
const api_1 = require("@opentelemetry/api");
|
|
5
|
+
const db_js_1 = require("./db.js");
|
|
6
|
+
const messaging_js_1 = require("./messaging.js");
|
|
7
|
+
const attrs = (queue, type, op) => ({
|
|
8
|
+
'messaging.system': 'pg-boss',
|
|
9
|
+
'messaging.destination.name': queue,
|
|
10
|
+
'messaging.operation.type': type,
|
|
11
|
+
'messaging.operation.name': op,
|
|
12
|
+
});
|
|
13
|
+
/** Run `call` and set the returned job id on the active (producer) span. */
|
|
14
|
+
function withId(call) {
|
|
15
|
+
const span = api_1.trace.getActiveSpan();
|
|
16
|
+
return call().then((id) => {
|
|
17
|
+
if (typeof id === 'string')
|
|
18
|
+
span?.setAttribute('messaging.message.id', id);
|
|
19
|
+
return id;
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* `instrumentPgBoss(boss)`: PRODUCER spans on `send` and `insert` (context in job data under `_wt`,
|
|
24
|
+
* object data only), and a CONSUMER span around each `work` handler call, child of the producer for
|
|
25
|
+
* a single job and linked to every job's producer for a batch. `_wt` is removed before the handler
|
|
26
|
+
* sees `job.data`. `messaging.system` is `pg-boss` (OTel has no value for it).
|
|
27
|
+
* shortcut: sendAfter/sendThrottled/sendDebounced/sendSingleton bypass `send`; their jobs carry no context.
|
|
28
|
+
*/
|
|
29
|
+
function instrumentPgBoss(boss) {
|
|
30
|
+
(0, db_js_1.patch)(boss, 'send', (orig) => function (nameOrReq, data, ...rest) {
|
|
31
|
+
if (typeof nameOrReq === 'object' && nameOrReq) {
|
|
32
|
+
const req = nameOrReq;
|
|
33
|
+
return (0, messaging_js_1.produce)(`send ${req.name}`, attrs(req.name, 'send', 'send'), () => withId(() => orig.call(this, { ...req, data: (0, messaging_js_1.injectData)(req.data) }, data, ...rest)));
|
|
34
|
+
}
|
|
35
|
+
return (0, messaging_js_1.produce)(`send ${nameOrReq}`, attrs(nameOrReq, 'send', 'send'), () => withId(() => orig.call(this, nameOrReq, (0, messaging_js_1.injectData)(data), ...rest)));
|
|
36
|
+
});
|
|
37
|
+
// v9: insert(jobs[]); v10: insert(name, jobs[])
|
|
38
|
+
(0, db_js_1.patch)(boss, 'insert', (orig) => function (a, b, ...rest) {
|
|
39
|
+
const jobs = (Array.isArray(a) ? a : b);
|
|
40
|
+
const queue = typeof a === 'string' ? a : jobs?.[0]?.name ?? '';
|
|
41
|
+
const at = { ...attrs(queue, 'send', 'insert'), 'messaging.batch.message_count': jobs?.length ?? 0 };
|
|
42
|
+
return (0, messaging_js_1.produce)(`send ${queue}`, at, () => {
|
|
43
|
+
const out = jobs?.map((j) => ({ ...j, data: (0, messaging_js_1.injectData)(j.data) }));
|
|
44
|
+
return Array.isArray(a) ? orig.call(this, out, b, ...rest) : orig.call(this, a, out, ...rest);
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
(0, db_js_1.patch)(boss, 'work', (orig) => function (name, ...rest) {
|
|
48
|
+
const i = rest.length - 1;
|
|
49
|
+
const handler = rest[i];
|
|
50
|
+
if (typeof handler !== 'function')
|
|
51
|
+
return orig.call(this, name, ...rest);
|
|
52
|
+
rest[i] = function (arg) {
|
|
53
|
+
const jobs = Array.isArray(arg) ? arg : [arg];
|
|
54
|
+
const a = attrs(name, 'process', 'work');
|
|
55
|
+
if (jobs.length === 1 && jobs[0]?.id)
|
|
56
|
+
a['messaging.message.id'] = jobs[0].id;
|
|
57
|
+
if (Array.isArray(arg))
|
|
58
|
+
a['messaging.batch.message_count'] = arg.length;
|
|
59
|
+
return (0, messaging_js_1.consume)(`process ${name}`, a, jobs.map((j) => (0, messaging_js_1.takeContext)(j?.data)), () => handler.call(this, arg));
|
|
60
|
+
};
|
|
61
|
+
return orig.call(this, name, ...rest);
|
|
62
|
+
});
|
|
63
|
+
}
|
|
@@ -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,94 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.observePool = void 0;
|
|
4
|
+
exports.instrumentMongoPool = instrumentMongoPool;
|
|
5
|
+
exports.instrumentMysql2Pool = instrumentMysql2Pool;
|
|
6
|
+
// Connection-pool metrics (OTel db semconv) for drivers whose pools upstream does not cover.
|
|
7
|
+
const db_js_1 = require("./db.js");
|
|
8
|
+
Object.defineProperty(exports, "observePool", { enumerable: true, get: function () { return db_js_1.observePool; } });
|
|
9
|
+
/**
|
|
10
|
+
* `instrumentMongoPool(client)` on a `MongoClient` (before or after `connect`): pool gauges per
|
|
11
|
+
* server address from the driver's CMAP events, wait time (driver >= 6.9 reports `durationMS`),
|
|
12
|
+
* use time (check-out to check-in) and check-out timeouts. Call after `init`.
|
|
13
|
+
*/
|
|
14
|
+
function instrumentMongoPool(client) {
|
|
15
|
+
const pools = new Map();
|
|
16
|
+
const out = new Map(); // `${address}#${connectionId}` -> checked out at (ms)
|
|
17
|
+
const get = (address) => {
|
|
18
|
+
let p = pools.get(address);
|
|
19
|
+
if (!p) {
|
|
20
|
+
const s = { used: 0, pending: 0, total: 0 };
|
|
21
|
+
s.rec = (0, db_js_1.observePool)('mongodb', address, () => ({
|
|
22
|
+
used: s.used, idle: Math.max(0, s.total - s.used), pending: s.pending, max: s.max,
|
|
23
|
+
}));
|
|
24
|
+
pools.set(address, (p = s));
|
|
25
|
+
}
|
|
26
|
+
return p;
|
|
27
|
+
};
|
|
28
|
+
const dec = (n) => Math.max(0, n - 1);
|
|
29
|
+
client.on('connectionPoolCreated', (e) => {
|
|
30
|
+
const max = e.options?.maxPoolSize;
|
|
31
|
+
if (typeof max === 'number')
|
|
32
|
+
get(e.address).max = max;
|
|
33
|
+
});
|
|
34
|
+
client.on('connectionPoolClosed', (e) => {
|
|
35
|
+
pools.get(e.address)?.rec.stop();
|
|
36
|
+
pools.delete(e.address);
|
|
37
|
+
});
|
|
38
|
+
client.on('connectionCreated', (e) => { get(e.address).total++; });
|
|
39
|
+
client.on('connectionClosed', (e) => { const p = get(e.address); p.total = dec(p.total); });
|
|
40
|
+
client.on('connectionCheckOutStarted', (e) => { get(e.address).pending++; });
|
|
41
|
+
client.on('connectionCheckOutFailed', (e) => {
|
|
42
|
+
const p = get(e.address);
|
|
43
|
+
p.pending = dec(p.pending);
|
|
44
|
+
if (e.reason === 'timeout')
|
|
45
|
+
p.rec.timeout();
|
|
46
|
+
if (typeof e.durationMS === 'number')
|
|
47
|
+
p.rec.wait(e.durationMS / 1000);
|
|
48
|
+
});
|
|
49
|
+
client.on('connectionCheckedOut', (e) => {
|
|
50
|
+
const p = get(e.address);
|
|
51
|
+
p.pending = dec(p.pending);
|
|
52
|
+
p.used++;
|
|
53
|
+
if (typeof e.durationMS === 'number')
|
|
54
|
+
p.rec.wait(e.durationMS / 1000);
|
|
55
|
+
out.set(`${e.address}#${e.connectionId}`, Date.now());
|
|
56
|
+
});
|
|
57
|
+
client.on('connectionCheckedIn', (e) => {
|
|
58
|
+
const p = get(e.address);
|
|
59
|
+
p.used = dec(p.used);
|
|
60
|
+
const k = `${e.address}#${e.connectionId}`;
|
|
61
|
+
const t = out.get(k);
|
|
62
|
+
if (t !== undefined) {
|
|
63
|
+
out.delete(k);
|
|
64
|
+
p.rec.use((Date.now() - t) / 1000);
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* `instrumentMysql2Pool(pool)` on a mysql2 pool (callback or `.promise()` pool): used/idle/pending
|
|
70
|
+
* connections and the limit, plus use time (acquire to release). Name defaults to `host:port/db`.
|
|
71
|
+
* shortcut: no wait time or timeouts (mysql2 has no acquire timeout and does not report queue wait).
|
|
72
|
+
* Returns `stop()`.
|
|
73
|
+
*/
|
|
74
|
+
function instrumentMysql2Pool(pool, name) {
|
|
75
|
+
const core = ('pool' in pool && pool.pool ? pool.pool : pool);
|
|
76
|
+
const c = core.config?.connectionConfig;
|
|
77
|
+
const pname = name ?? (c ? `${c.host ?? 'localhost'}:${c.port ?? 3306}${c.database ? `/${c.database}` : ''}` : 'mysql2');
|
|
78
|
+
const rec = (0, db_js_1.observePool)('mysql', pname, () => {
|
|
79
|
+
const all = core._allConnections?.length ?? 0;
|
|
80
|
+
const free = core._freeConnections?.length ?? 0;
|
|
81
|
+
return { used: all - free, idle: free, pending: core._connectionQueue?.length ?? 0, max: core.config?.connectionLimit };
|
|
82
|
+
});
|
|
83
|
+
const acquired = new WeakMap();
|
|
84
|
+
core.on('acquire', (conn) => { if (conn)
|
|
85
|
+
acquired.set(conn, Date.now()); });
|
|
86
|
+
core.on('release', (conn) => {
|
|
87
|
+
const t = conn && acquired.get(conn);
|
|
88
|
+
if (t) {
|
|
89
|
+
acquired.delete(conn);
|
|
90
|
+
rec.use((Date.now() - t) / 1000);
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
return rec.stop;
|
|
94
|
+
}
|
|
@@ -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,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,3 @@
|
|
|
1
|
+
/** prisma.$extends(watchtowerExtension()): a CLIENT span per model operation (no query args recorded: they hold data).
|
|
2
|
+
* shortcut: $queryRaw/$executeRaw are not wrapped; add query.$queryRaw hooks if raw SQL spans are needed. */
|
|
3
|
+
export declare function watchtowerExtension(): object;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.watchtowerExtension = watchtowerExtension;
|
|
4
|
+
const api_1 = require("@opentelemetry/api");
|
|
5
|
+
/** prisma.$extends(watchtowerExtension()): a CLIENT span per model operation (no query args recorded: they hold data).
|
|
6
|
+
* shortcut: $queryRaw/$executeRaw are not wrapped; add query.$queryRaw hooks if raw SQL spans are needed. */
|
|
7
|
+
function watchtowerExtension() {
|
|
8
|
+
return {
|
|
9
|
+
name: 'watchtower',
|
|
10
|
+
query: {
|
|
11
|
+
$allModels: {
|
|
12
|
+
$allOperations({ model, operation, query, args }) {
|
|
13
|
+
return api_1.trace.getTracer('watchtower').startActiveSpan(`prisma ${model}.${operation}`, {
|
|
14
|
+
kind: api_1.SpanKind.CLIENT,
|
|
15
|
+
attributes: { 'db.system.name': 'prisma', 'db.collection.name': model, 'db.operation.name': operation },
|
|
16
|
+
}, async (span) => {
|
|
17
|
+
try {
|
|
18
|
+
return await query(args);
|
|
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
|
+
},
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
}
|
|
@@ -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
|
+
}
|