@justanalyticsapp/node 0.2.1 → 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/dist/client.d.ts +23 -1
- package/dist/client.js +139 -1
- package/dist/client.js.map +1 -1
- package/dist/integrations/axios-integration.d.ts +53 -0
- package/dist/integrations/axios-integration.js +221 -0
- package/dist/integrations/axios-integration.js.map +1 -0
- package/dist/integrations/drizzle.d.ts +54 -0
- package/dist/integrations/drizzle.js +245 -0
- package/dist/integrations/drizzle.js.map +1 -0
- package/dist/integrations/http.d.ts +3 -1
- package/dist/integrations/http.js +12 -1
- package/dist/integrations/http.js.map +1 -1
- package/dist/integrations/mongodb.d.ts +50 -0
- package/dist/integrations/mongodb.js +260 -0
- package/dist/integrations/mongodb.js.map +1 -0
- package/dist/integrations/mysql.d.ts +56 -0
- package/dist/integrations/mysql.js +307 -0
- package/dist/integrations/mysql.js.map +1 -0
- package/dist/integrations/next.d.ts +41 -0
- package/dist/integrations/next.js +204 -0
- package/dist/integrations/next.js.map +1 -1
- package/dist/integrations/pg.d.ts +31 -3
- package/dist/integrations/pg.js +102 -22
- package/dist/integrations/pg.js.map +1 -1
- package/dist/integrations/postgres-js.d.ts +57 -0
- package/dist/integrations/postgres-js.js +311 -0
- package/dist/integrations/postgres-js.js.map +1 -0
- package/dist/integrations/prisma.d.ts +54 -0
- package/dist/integrations/prisma.js +186 -0
- package/dist/integrations/prisma.js.map +1 -0
- package/dist/integrations/redis.d.ts +6 -0
- package/dist/integrations/redis.js +16 -19
- package/dist/integrations/redis.js.map +1 -1
- package/dist/integrations/upstash-redis.d.ts +43 -0
- package/dist/integrations/upstash-redis.js +170 -0
- package/dist/integrations/upstash-redis.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @file packages/node-sdk/src/integrations/mongodb.ts
|
|
4
|
+
* @description MongoDB auto-instrumentation integration for the JustAnalytics Node.js SDK.
|
|
5
|
+
*
|
|
6
|
+
* Monkey-patches `mongodb` Collection.prototype methods to automatically create
|
|
7
|
+
* `db.query` spans for all MongoDB operations. This also covers Mongoose since
|
|
8
|
+
* it uses the `mongodb` driver internally.
|
|
9
|
+
*
|
|
10
|
+
* Follows the same monkey-patching pattern as pg.ts and redis.ts:
|
|
11
|
+
* - Integration class with `enable()`/`disable()` lifecycle methods
|
|
12
|
+
* - Original function preservation for clean teardown
|
|
13
|
+
* - Fail-open design: if instrumentation code fails, the original operation executes normally
|
|
14
|
+
*
|
|
15
|
+
* SECURITY: Query filter objects are JSON-stringified with values replaced by '?'
|
|
16
|
+
* to prevent leaking sensitive data into span attributes.
|
|
17
|
+
*/
|
|
18
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
|
+
exports.MongodbIntegration = void 0;
|
|
20
|
+
exports.sanitizeMongoFilter = sanitizeMongoFilter;
|
|
21
|
+
const span_1 = require("../span");
|
|
22
|
+
const context_1 = require("../context");
|
|
23
|
+
const id_1 = require("../utils/id");
|
|
24
|
+
/** MongoDB Collection methods to instrument */
|
|
25
|
+
const COLLECTION_METHODS = [
|
|
26
|
+
'find',
|
|
27
|
+
'findOne',
|
|
28
|
+
'insertOne',
|
|
29
|
+
'insertMany',
|
|
30
|
+
'updateOne',
|
|
31
|
+
'updateMany',
|
|
32
|
+
'deleteOne',
|
|
33
|
+
'deleteMany',
|
|
34
|
+
'aggregate',
|
|
35
|
+
'countDocuments',
|
|
36
|
+
'distinct',
|
|
37
|
+
'findOneAndUpdate',
|
|
38
|
+
'findOneAndDelete',
|
|
39
|
+
'findOneAndReplace',
|
|
40
|
+
'bulkWrite',
|
|
41
|
+
];
|
|
42
|
+
/**
|
|
43
|
+
* Sanitize a MongoDB filter/query object by replacing all values with '?'.
|
|
44
|
+
* Only preserves top-level keys and operator keys (starting with '$').
|
|
45
|
+
*/
|
|
46
|
+
function sanitizeMongoFilter(filter, maxLength = 1024) {
|
|
47
|
+
try {
|
|
48
|
+
if (!filter || typeof filter !== 'object') {
|
|
49
|
+
return filter === undefined ? '{}' : '?';
|
|
50
|
+
}
|
|
51
|
+
const sanitized = sanitizeObject(filter, 3);
|
|
52
|
+
const result = JSON.stringify(sanitized);
|
|
53
|
+
if (result.length > maxLength) {
|
|
54
|
+
return result.substring(0, maxLength - 3) + '...';
|
|
55
|
+
}
|
|
56
|
+
return result;
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
return '{}';
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
function sanitizeObject(obj, maxDepth) {
|
|
63
|
+
if (maxDepth <= 0)
|
|
64
|
+
return '?';
|
|
65
|
+
if (Array.isArray(obj)) {
|
|
66
|
+
return obj.length > 0 ? ['?'] : [];
|
|
67
|
+
}
|
|
68
|
+
const result = {};
|
|
69
|
+
for (const key of Object.keys(obj)) {
|
|
70
|
+
const value = obj[key];
|
|
71
|
+
if (key.startsWith('$') && typeof value === 'object' && value !== null) {
|
|
72
|
+
// Preserve MongoDB operator structure
|
|
73
|
+
result[key] = sanitizeObject(value, maxDepth - 1);
|
|
74
|
+
}
|
|
75
|
+
else if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
|
|
76
|
+
result[key] = sanitizeObject(value, maxDepth - 1);
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
result[key] = '?';
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return result;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* MongodbIntegration monkey-patches mongodb Collection.prototype methods
|
|
86
|
+
* to auto-create db.query spans for all MongoDB operations.
|
|
87
|
+
*/
|
|
88
|
+
class MongodbIntegration {
|
|
89
|
+
constructor(serviceName, options, onSpanEnd) {
|
|
90
|
+
this._enabled = false;
|
|
91
|
+
this._instrumentedDriver = null;
|
|
92
|
+
// Store original Collection methods for restoration
|
|
93
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
94
|
+
this._collectionProto = null;
|
|
95
|
+
this._originals = new Map();
|
|
96
|
+
this._serviceName = serviceName;
|
|
97
|
+
this._onSpanEnd = onSpanEnd;
|
|
98
|
+
const opts = options || {};
|
|
99
|
+
this._options = {
|
|
100
|
+
enabled: opts.enabled !== false,
|
|
101
|
+
maxStatementLength: opts.maxStatementLength ?? 1024,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
get instrumentedDriver() {
|
|
105
|
+
return this._instrumentedDriver;
|
|
106
|
+
}
|
|
107
|
+
enable() {
|
|
108
|
+
if (this._enabled)
|
|
109
|
+
return;
|
|
110
|
+
if (!this._options.enabled)
|
|
111
|
+
return;
|
|
112
|
+
try {
|
|
113
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
114
|
+
let mongodb;
|
|
115
|
+
try {
|
|
116
|
+
mongodb = require('mongodb');
|
|
117
|
+
}
|
|
118
|
+
catch {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
const Collection = mongodb.Collection;
|
|
122
|
+
if (!Collection || !Collection.prototype) {
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
this._collectionProto = Collection.prototype;
|
|
126
|
+
for (const method of COLLECTION_METHODS) {
|
|
127
|
+
const original = Collection.prototype[method];
|
|
128
|
+
if (typeof original !== 'function')
|
|
129
|
+
continue;
|
|
130
|
+
this._originals.set(method, original);
|
|
131
|
+
Collection.prototype[method] = this._wrapMethod(original, method);
|
|
132
|
+
}
|
|
133
|
+
this._instrumentedDriver = 'mongodb';
|
|
134
|
+
this._enabled = true;
|
|
135
|
+
}
|
|
136
|
+
catch {
|
|
137
|
+
this._restoreOriginals();
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
disable() {
|
|
141
|
+
if (!this._enabled)
|
|
142
|
+
return;
|
|
143
|
+
this._restoreOriginals();
|
|
144
|
+
this._instrumentedDriver = null;
|
|
145
|
+
this._enabled = false;
|
|
146
|
+
}
|
|
147
|
+
_wrapMethod(original, method) {
|
|
148
|
+
const integration = this;
|
|
149
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
150
|
+
return function patchedMethod(...args) {
|
|
151
|
+
try {
|
|
152
|
+
const parentSpan = (0, context_1.getActiveSpan)();
|
|
153
|
+
const traceId = parentSpan?.traceId ?? (0, id_1.generateTraceId)();
|
|
154
|
+
const parentSpanId = parentSpan?.id ?? null;
|
|
155
|
+
// Get collection name and database name from the Collection instance
|
|
156
|
+
const collectionName = this.collectionName || this.s?.namespace?.collection || 'unknown';
|
|
157
|
+
const dbName = this.dbName || this.s?.namespace?.db || this.s?.db?.databaseName || undefined;
|
|
158
|
+
const operationName = `db.query ${method}`;
|
|
159
|
+
// Sanitize the first argument (usually the filter/document)
|
|
160
|
+
const filterStr = args.length > 0
|
|
161
|
+
? sanitizeMongoFilter(args[0], integration._options.maxStatementLength)
|
|
162
|
+
: '{}';
|
|
163
|
+
const statement = `${method} ${collectionName} ${filterStr}`;
|
|
164
|
+
const truncatedStatement = statement.length > integration._options.maxStatementLength
|
|
165
|
+
? statement.substring(0, integration._options.maxStatementLength - 3) + '...'
|
|
166
|
+
: statement;
|
|
167
|
+
const attributes = {
|
|
168
|
+
'db.system': 'mongodb',
|
|
169
|
+
'db.operation': method,
|
|
170
|
+
'db.mongodb.collection': collectionName,
|
|
171
|
+
'db.statement': truncatedStatement,
|
|
172
|
+
};
|
|
173
|
+
if (dbName)
|
|
174
|
+
attributes['db.name'] = dbName;
|
|
175
|
+
const span = new span_1.Span({
|
|
176
|
+
operationName,
|
|
177
|
+
serviceName: integration._serviceName,
|
|
178
|
+
kind: 'client',
|
|
179
|
+
traceId,
|
|
180
|
+
parentSpanId,
|
|
181
|
+
attributes,
|
|
182
|
+
});
|
|
183
|
+
try {
|
|
184
|
+
const result = original.apply(this, args);
|
|
185
|
+
// MongoDB methods return promises (or cursors with toArray)
|
|
186
|
+
if (result && typeof result.then === 'function') {
|
|
187
|
+
return result.then((value) => {
|
|
188
|
+
span.end();
|
|
189
|
+
integration._onSpanEnd(span);
|
|
190
|
+
return value;
|
|
191
|
+
}, (error) => {
|
|
192
|
+
span.setStatus('error', error instanceof Error ? error.message : String(error));
|
|
193
|
+
span.end();
|
|
194
|
+
integration._onSpanEnd(span);
|
|
195
|
+
throw error;
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
// For methods that return cursors (e.g., find, aggregate), we
|
|
199
|
+
// instrument the cursor's toArray/next if possible, or just end span now
|
|
200
|
+
if (result && typeof result === 'object' && 'toArray' in result) {
|
|
201
|
+
// It's a cursor -- wrap toArray to capture full duration
|
|
202
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
203
|
+
const cursor = result;
|
|
204
|
+
const originalToArray = cursor.toArray;
|
|
205
|
+
if (typeof originalToArray === 'function') {
|
|
206
|
+
cursor.toArray = function patchedToArray(...toArrayArgs) {
|
|
207
|
+
const toArrayResult = originalToArray.apply(cursor, toArrayArgs);
|
|
208
|
+
if (toArrayResult && typeof toArrayResult.then === 'function') {
|
|
209
|
+
return toArrayResult.then((value) => {
|
|
210
|
+
span.end();
|
|
211
|
+
integration._onSpanEnd(span);
|
|
212
|
+
return value;
|
|
213
|
+
}, (error) => {
|
|
214
|
+
span.setStatus('error', error instanceof Error ? error.message : String(error));
|
|
215
|
+
span.end();
|
|
216
|
+
integration._onSpanEnd(span);
|
|
217
|
+
throw error;
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
span.end();
|
|
221
|
+
integration._onSpanEnd(span);
|
|
222
|
+
return toArrayResult;
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
// No toArray -- end span immediately
|
|
227
|
+
span.end();
|
|
228
|
+
integration._onSpanEnd(span);
|
|
229
|
+
}
|
|
230
|
+
return result;
|
|
231
|
+
}
|
|
232
|
+
// Sync result (unlikely for MongoDB)
|
|
233
|
+
span.end();
|
|
234
|
+
integration._onSpanEnd(span);
|
|
235
|
+
return result;
|
|
236
|
+
}
|
|
237
|
+
catch (error) {
|
|
238
|
+
span.setStatus('error', error instanceof Error ? error.message : String(error));
|
|
239
|
+
span.end();
|
|
240
|
+
integration._onSpanEnd(span);
|
|
241
|
+
throw error;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
catch {
|
|
245
|
+
return original.apply(this, args);
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
_restoreOriginals() {
|
|
250
|
+
if (this._collectionProto) {
|
|
251
|
+
for (const [method, original] of this._originals) {
|
|
252
|
+
this._collectionProto[method] = original;
|
|
253
|
+
}
|
|
254
|
+
this._originals.clear();
|
|
255
|
+
this._collectionProto = null;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
exports.MongodbIntegration = MongodbIntegration;
|
|
260
|
+
//# sourceMappingURL=mongodb.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mongodb.js","sourceRoot":"","sources":["../../src/integrations/mongodb.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;GAeG;;;AA4CH,kDAgBC;AA1DD,kCAA+B;AAC/B,wCAA2C;AAC3C,oCAA8C;AAiB9C,+CAA+C;AAC/C,MAAM,kBAAkB,GAAG;IACzB,MAAM;IACN,SAAS;IACT,WAAW;IACX,YAAY;IACZ,WAAW;IACX,YAAY;IACZ,WAAW;IACX,YAAY;IACZ,WAAW;IACX,gBAAgB;IAChB,UAAU;IACV,kBAAkB;IAClB,kBAAkB;IAClB,mBAAmB;IACnB,WAAW;CACH,CAAC;AAEX;;;GAGG;AACH,SAAgB,mBAAmB,CAAC,MAAe,EAAE,YAAoB,IAAI;IAC3E,IAAI,CAAC;QACH,IAAI,CAAC,MAAM,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE,CAAC;YAC1C,OAAO,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC;QAC3C,CAAC;QAED,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I,EAAE,IAAI,CAAC,CAAC;YACpC,CAAC;QACH,CAAC,CAAC;IACJ,CAAC;IAEO,iBAAiB;QACvB,IAAI,IAAI,CAAC,gBAAgB,EAAE,CAAC;YAC1B,KAAK,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,IAAI,IAAI,CAAC,UAAU,EAAE,CAAC;gBACjD,IAAI,CAAC,gBAAgB,CAAC,MAAM,CAAC,GAAG,QAAQ,CAAC;YAC3C,CAAC;YACD,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,CAAC;YACxB,IAAI,CAAC,gBAAgB,GAAG,IAAI,CAAC;QAC/B,CAAC;IACH,CAAC;CACF;AA1MD,gDA0MC"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file packages/node-sdk/src/integrations/mysql.ts
|
|
3
|
+
* @description MySQL auto-instrumentation integration for the JustAnalytics Node.js SDK.
|
|
4
|
+
*
|
|
5
|
+
* Monkey-patches `mysql2` (Connection.prototype.query and .execute) and/or
|
|
6
|
+
* legacy `mysql` (Connection.prototype.query) to automatically create `db.query`
|
|
7
|
+
* spans for all MySQL operations.
|
|
8
|
+
*
|
|
9
|
+
* Follows the same monkey-patching pattern as pg.ts:
|
|
10
|
+
* - Integration class with `enable()`/`disable()` lifecycle methods
|
|
11
|
+
* - Original function preservation for clean teardown
|
|
12
|
+
* - Integration with AsyncLocalStorage context for automatic parent-child span relationships
|
|
13
|
+
* - Fail-open design: if instrumentation code fails, the original query executes normally
|
|
14
|
+
*
|
|
15
|
+
* SQL sanitization reuses the same utilities from pg.ts.
|
|
16
|
+
*/
|
|
17
|
+
import { Span } from '../span';
|
|
18
|
+
/**
|
|
19
|
+
* Configuration for the MySQL auto-instrumentation integration.
|
|
20
|
+
*/
|
|
21
|
+
export interface MysqlIntegrationOptions {
|
|
22
|
+
/** Enable/disable the integration (default: true) */
|
|
23
|
+
enabled?: boolean;
|
|
24
|
+
/** Maximum character length for the db.statement attribute (default: 2048) */
|
|
25
|
+
maxQueryLength?: number;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* MysqlIntegration monkey-patches mysql2 and/or mysql to auto-create
|
|
29
|
+
* db.query spans for all MySQL operations.
|
|
30
|
+
*/
|
|
31
|
+
export declare class MysqlIntegration {
|
|
32
|
+
private _enabled;
|
|
33
|
+
private _options;
|
|
34
|
+
private _serviceName;
|
|
35
|
+
private _onSpanEnd;
|
|
36
|
+
/** Which driver was instrumented */
|
|
37
|
+
private _instrumentedDriver;
|
|
38
|
+
private _mysql2ConnectionProto;
|
|
39
|
+
private _originalMysql2Query;
|
|
40
|
+
private _originalMysql2Execute;
|
|
41
|
+
private _mysqlConnectionProto;
|
|
42
|
+
private _originalMysqlQuery;
|
|
43
|
+
constructor(serviceName: string, options: MysqlIntegrationOptions | undefined, onSpanEnd: (span: Span) => void);
|
|
44
|
+
get instrumentedDriver(): string | null;
|
|
45
|
+
enable(): void;
|
|
46
|
+
disable(): void;
|
|
47
|
+
private _tryPatchMysql2;
|
|
48
|
+
private _tryPatchMysql;
|
|
49
|
+
/**
|
|
50
|
+
* Wrap a mysql/mysql2 query or execute method to create db.query spans.
|
|
51
|
+
*/
|
|
52
|
+
private _wrapQuery;
|
|
53
|
+
private _restoreMysql2;
|
|
54
|
+
private _restoreMysql;
|
|
55
|
+
private _restoreOriginals;
|
|
56
|
+
}
|
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @file packages/node-sdk/src/integrations/mysql.ts
|
|
4
|
+
* @description MySQL auto-instrumentation integration for the JustAnalytics Node.js SDK.
|
|
5
|
+
*
|
|
6
|
+
* Monkey-patches `mysql2` (Connection.prototype.query and .execute) and/or
|
|
7
|
+
* legacy `mysql` (Connection.prototype.query) to automatically create `db.query`
|
|
8
|
+
* spans for all MySQL operations.
|
|
9
|
+
*
|
|
10
|
+
* Follows the same monkey-patching pattern as pg.ts:
|
|
11
|
+
* - Integration class with `enable()`/`disable()` lifecycle methods
|
|
12
|
+
* - Original function preservation for clean teardown
|
|
13
|
+
* - Integration with AsyncLocalStorage context for automatic parent-child span relationships
|
|
14
|
+
* - Fail-open design: if instrumentation code fails, the original query executes normally
|
|
15
|
+
*
|
|
16
|
+
* SQL sanitization reuses the same utilities from pg.ts.
|
|
17
|
+
*/
|
|
18
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
|
+
exports.MysqlIntegration = void 0;
|
|
20
|
+
const span_1 = require("../span");
|
|
21
|
+
const context_1 = require("../context");
|
|
22
|
+
const id_1 = require("../utils/id");
|
|
23
|
+
const pg_1 = require("./pg");
|
|
24
|
+
/**
|
|
25
|
+
* Normalize mysql/mysql2 query arguments into a consistent shape.
|
|
26
|
+
*
|
|
27
|
+
* Both drivers support:
|
|
28
|
+
* - query(sql, callback)
|
|
29
|
+
* - query(sql, values, callback)
|
|
30
|
+
* - query(sql) -- promise (mysql2 only)
|
|
31
|
+
* - query(sql, values) -- promise (mysql2 only)
|
|
32
|
+
* - query({ sql: string, ... })
|
|
33
|
+
*/
|
|
34
|
+
function normalizeMysqlArgs(args) {
|
|
35
|
+
let text = '';
|
|
36
|
+
let callback = null;
|
|
37
|
+
if (typeof args[0] === 'string') {
|
|
38
|
+
text = args[0];
|
|
39
|
+
}
|
|
40
|
+
else if (typeof args[0] === 'object' && args[0] !== null && 'sql' in args[0]) {
|
|
41
|
+
text = args[0].sql;
|
|
42
|
+
}
|
|
43
|
+
// Find callback (last function argument)
|
|
44
|
+
for (let i = args.length - 1; i >= 1; i--) {
|
|
45
|
+
if (typeof args[i] === 'function') {
|
|
46
|
+
callback = args[i];
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
// Also check first arg callback style: query(callback) is unusual but defensive
|
|
51
|
+
if (!callback && args.length === 1 && typeof args[0] === 'function') {
|
|
52
|
+
// Not a query call we can instrument
|
|
53
|
+
text = '';
|
|
54
|
+
}
|
|
55
|
+
return { text, callback };
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* MysqlIntegration monkey-patches mysql2 and/or mysql to auto-create
|
|
59
|
+
* db.query spans for all MySQL operations.
|
|
60
|
+
*/
|
|
61
|
+
class MysqlIntegration {
|
|
62
|
+
constructor(serviceName, options, onSpanEnd) {
|
|
63
|
+
this._enabled = false;
|
|
64
|
+
/** Which driver was instrumented */
|
|
65
|
+
this._instrumentedDriver = null;
|
|
66
|
+
// mysql2 originals
|
|
67
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
68
|
+
this._mysql2ConnectionProto = null;
|
|
69
|
+
this._originalMysql2Query = null;
|
|
70
|
+
this._originalMysql2Execute = null;
|
|
71
|
+
// legacy mysql originals
|
|
72
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
73
|
+
this._mysqlConnectionProto = null;
|
|
74
|
+
this._originalMysqlQuery = null;
|
|
75
|
+
this._serviceName = serviceName;
|
|
76
|
+
this._onSpanEnd = onSpanEnd;
|
|
77
|
+
const opts = options || {};
|
|
78
|
+
this._options = {
|
|
79
|
+
enabled: opts.enabled !== false,
|
|
80
|
+
maxQueryLength: opts.maxQueryLength ?? 2048,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
get instrumentedDriver() {
|
|
84
|
+
return this._instrumentedDriver;
|
|
85
|
+
}
|
|
86
|
+
enable() {
|
|
87
|
+
if (this._enabled)
|
|
88
|
+
return;
|
|
89
|
+
if (!this._options.enabled)
|
|
90
|
+
return;
|
|
91
|
+
const drivers = [];
|
|
92
|
+
// Try mysql2 first (most popular)
|
|
93
|
+
if (this._tryPatchMysql2()) {
|
|
94
|
+
drivers.push('mysql2');
|
|
95
|
+
}
|
|
96
|
+
// Try legacy mysql
|
|
97
|
+
if (this._tryPatchMysql()) {
|
|
98
|
+
drivers.push('mysql');
|
|
99
|
+
}
|
|
100
|
+
if (drivers.length === 0)
|
|
101
|
+
return;
|
|
102
|
+
this._instrumentedDriver = drivers.join(', ');
|
|
103
|
+
this._enabled = true;
|
|
104
|
+
}
|
|
105
|
+
disable() {
|
|
106
|
+
if (!this._enabled)
|
|
107
|
+
return;
|
|
108
|
+
this._restoreOriginals();
|
|
109
|
+
this._instrumentedDriver = null;
|
|
110
|
+
this._enabled = false;
|
|
111
|
+
}
|
|
112
|
+
_tryPatchMysql2() {
|
|
113
|
+
try {
|
|
114
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
115
|
+
let mysql2;
|
|
116
|
+
try {
|
|
117
|
+
mysql2 = require('mysql2');
|
|
118
|
+
}
|
|
119
|
+
catch {
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
// mysql2 exposes Connection via the module
|
|
123
|
+
const Connection = mysql2.Connection;
|
|
124
|
+
if (!Connection || !Connection.prototype) {
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
this._mysql2ConnectionProto = Connection.prototype;
|
|
128
|
+
// Patch query
|
|
129
|
+
if (typeof Connection.prototype.query === 'function') {
|
|
130
|
+
this._originalMysql2Query = Connection.prototype.query;
|
|
131
|
+
Connection.prototype.query = this._wrapQuery(this._originalMysql2Query, 'mysql2');
|
|
132
|
+
}
|
|
133
|
+
// Patch execute (prepared statements)
|
|
134
|
+
if (typeof Connection.prototype.execute === 'function') {
|
|
135
|
+
this._originalMysql2Execute = Connection.prototype.execute;
|
|
136
|
+
Connection.prototype.execute = this._wrapQuery(this._originalMysql2Execute, 'mysql2');
|
|
137
|
+
}
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
catch {
|
|
141
|
+
this._restoreMysql2();
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
_tryPatchMysql() {
|
|
146
|
+
try {
|
|
147
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
148
|
+
let mysql;
|
|
149
|
+
try {
|
|
150
|
+
mysql = require('mysql');
|
|
151
|
+
}
|
|
152
|
+
catch {
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
// Legacy mysql creates connections via createConnection/createPool
|
|
156
|
+
// The Connection prototype is internal; try to access it
|
|
157
|
+
const tempConn = mysql.createConnection ? mysql.createConnection({ host: '__probe__' }) : null;
|
|
158
|
+
if (tempConn && tempConn.constructor && tempConn.constructor.prototype) {
|
|
159
|
+
const proto = tempConn.constructor.prototype;
|
|
160
|
+
if (typeof proto.query === 'function') {
|
|
161
|
+
this._mysqlConnectionProto = proto;
|
|
162
|
+
this._originalMysqlQuery = proto.query;
|
|
163
|
+
proto.query = this._wrapQuery(this._originalMysqlQuery, 'mysql');
|
|
164
|
+
// Destroy the probe connection (it was never connected)
|
|
165
|
+
try {
|
|
166
|
+
tempConn.destroy();
|
|
167
|
+
}
|
|
168
|
+
catch { /* ignore */ }
|
|
169
|
+
return true;
|
|
170
|
+
}
|
|
171
|
+
try {
|
|
172
|
+
tempConn.destroy();
|
|
173
|
+
}
|
|
174
|
+
catch { /* ignore */ }
|
|
175
|
+
}
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
catch {
|
|
179
|
+
this._restoreMysql();
|
|
180
|
+
return false;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Wrap a mysql/mysql2 query or execute method to create db.query spans.
|
|
185
|
+
*/
|
|
186
|
+
_wrapQuery(original, driver) {
|
|
187
|
+
const integration = this;
|
|
188
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
189
|
+
return function patchedQuery(...args) {
|
|
190
|
+
try {
|
|
191
|
+
const { text, callback } = normalizeMysqlArgs(args);
|
|
192
|
+
if (!text) {
|
|
193
|
+
return original.apply(this, args);
|
|
194
|
+
}
|
|
195
|
+
const parentSpan = (0, context_1.getActiveSpan)();
|
|
196
|
+
const traceId = parentSpan?.traceId ?? (0, id_1.generateTraceId)();
|
|
197
|
+
const parentSpanId = parentSpan?.id ?? null;
|
|
198
|
+
const operation = (0, pg_1.extractSqlOperation)(text);
|
|
199
|
+
const table = (0, pg_1.extractTableName)(text);
|
|
200
|
+
const sanitizedSql = (0, pg_1.sanitizeSql)(text, integration._options.maxQueryLength);
|
|
201
|
+
const operationName = operation !== 'UNKNOWN' ? `db.query ${operation}` : 'db.query';
|
|
202
|
+
const attributes = {
|
|
203
|
+
'db.system': 'mysql',
|
|
204
|
+
'db.statement': sanitizedSql,
|
|
205
|
+
'db.driver': driver,
|
|
206
|
+
};
|
|
207
|
+
if (operation !== 'UNKNOWN')
|
|
208
|
+
attributes['db.operation'] = operation;
|
|
209
|
+
if (table)
|
|
210
|
+
attributes['db.sql.table'] = table;
|
|
211
|
+
// Extract connection info
|
|
212
|
+
const config = this.config || this.connectionConfig || {};
|
|
213
|
+
if (config.database)
|
|
214
|
+
attributes['db.name'] = config.database;
|
|
215
|
+
if (config.user)
|
|
216
|
+
attributes['db.user'] = config.user;
|
|
217
|
+
if (config.host) {
|
|
218
|
+
const host = config.host || 'localhost';
|
|
219
|
+
const port = config.port || 3306;
|
|
220
|
+
attributes['db.connection_string'] = `mysql://${config.user || ''}:***@${host}:${port}/${config.database || ''}`;
|
|
221
|
+
}
|
|
222
|
+
const span = new span_1.Span({
|
|
223
|
+
operationName,
|
|
224
|
+
serviceName: integration._serviceName,
|
|
225
|
+
kind: 'client',
|
|
226
|
+
traceId,
|
|
227
|
+
parentSpanId,
|
|
228
|
+
attributes,
|
|
229
|
+
});
|
|
230
|
+
if (callback) {
|
|
231
|
+
// Callback-style: wrap callback to end span
|
|
232
|
+
const wrappedArgs = [...args];
|
|
233
|
+
const cbIndex = wrappedArgs.findIndex((a) => typeof a === 'function');
|
|
234
|
+
if (cbIndex !== -1) {
|
|
235
|
+
const originalCb = wrappedArgs[cbIndex];
|
|
236
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
237
|
+
wrappedArgs[cbIndex] = function wrappedCallback(err, ...rest) {
|
|
238
|
+
if (err) {
|
|
239
|
+
span.setStatus('error', err instanceof Error ? err.message : String(err));
|
|
240
|
+
}
|
|
241
|
+
span.end();
|
|
242
|
+
integration._onSpanEnd(span);
|
|
243
|
+
return originalCb.call(this, err, ...rest);
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
return original.apply(this, wrappedArgs);
|
|
247
|
+
}
|
|
248
|
+
else {
|
|
249
|
+
// Promise-style (mysql2 supports this)
|
|
250
|
+
try {
|
|
251
|
+
const result = original.apply(this, args);
|
|
252
|
+
if (result && typeof result.then === 'function') {
|
|
253
|
+
return result.then((value) => {
|
|
254
|
+
span.end();
|
|
255
|
+
integration._onSpanEnd(span);
|
|
256
|
+
return value;
|
|
257
|
+
}, (error) => {
|
|
258
|
+
span.setStatus('error', error instanceof Error ? error.message : String(error));
|
|
259
|
+
span.end();
|
|
260
|
+
integration._onSpanEnd(span);
|
|
261
|
+
throw error;
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
span.end();
|
|
265
|
+
integration._onSpanEnd(span);
|
|
266
|
+
return result;
|
|
267
|
+
}
|
|
268
|
+
catch (error) {
|
|
269
|
+
span.setStatus('error', error instanceof Error ? error.message : String(error));
|
|
270
|
+
span.end();
|
|
271
|
+
integration._onSpanEnd(span);
|
|
272
|
+
throw error;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
catch {
|
|
277
|
+
return original.apply(this, args);
|
|
278
|
+
}
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
_restoreMysql2() {
|
|
282
|
+
if (this._mysql2ConnectionProto) {
|
|
283
|
+
if (this._originalMysql2Query) {
|
|
284
|
+
this._mysql2ConnectionProto.query = this._originalMysql2Query;
|
|
285
|
+
this._originalMysql2Query = null;
|
|
286
|
+
}
|
|
287
|
+
if (this._originalMysql2Execute) {
|
|
288
|
+
this._mysql2ConnectionProto.execute = this._originalMysql2Execute;
|
|
289
|
+
this._originalMysql2Execute = null;
|
|
290
|
+
}
|
|
291
|
+
this._mysql2ConnectionProto = null;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
_restoreMysql() {
|
|
295
|
+
if (this._mysqlConnectionProto && this._originalMysqlQuery) {
|
|
296
|
+
this._mysqlConnectionProto.query = this._originalMysqlQuery;
|
|
297
|
+
this._originalMysqlQuery = null;
|
|
298
|
+
this._mysqlConnectionProto = null;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
_restoreOriginals() {
|
|
302
|
+
this._restoreMysql2();
|
|
303
|
+
this._restoreMysql();
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
exports.MysqlIntegration = MysqlIntegration;
|
|
307
|
+
//# sourceMappingURL=mysql.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mysql.js","sourceRoot":"","sources":["../../src/integrations/mysql.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;GAeG;;;AAEH,kCAA+B;AAC/B,wCAA2C;AAC3C,oCAA8C;AAC9C,6BAA0E;AAiB1E;;;;;;;;;GASG;AACH,SAAS,kBAAkB,CAAC,IAAe;IAIzC,IAAI,IAAI,GAAW,EAAE,CAAC;IACtB,IAAI,QAAQ,GAA+C,IAAI,CAAC;IAEhE,IAAI,OAAO,IAAI,CAAC,CAAC,CAAC,KAAK,QAAQ,EAAE,CAAC;QAChC,IAAI,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC;IACjB,CAAC;SAAM,IAAI,OAAO,IAAI,CAAC,CAAC,CAAC,KAAK,QAAQ,IAAI,IAAI,CAAC,CAAC,CAAC,KAAK,IAAI,IAAI,KAAK,IAAK,IAAI,CAAC,CAAC,CAA6B,EAAE,CAAC;QAC5G,IAAI,GAAI,IAAI,CAAC,CAAC,CAA6B,CAAC,GAAa,CAAC;IAC5D,CAAC;IAED,yCAAyC;IACzC,KAAK,IAAI,CAAC,GAAG,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;QAC1C,IAAI,OAAO,IAAI,CA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