@sentienguard/apm 1.0.20 → 1.0.22-debug.1
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/package.json +1 -1
- package/src/index.js +5 -2
- package/src/mongodb.js +42 -9
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -72,14 +72,17 @@ function initialize() {
|
|
|
72
72
|
const tracingOn = startTracing();
|
|
73
73
|
// Ensure fetch() dependency edges are captured (works in both tracing and legacy modes)
|
|
74
74
|
instrumentFetch();
|
|
75
|
-
|
|
75
|
+
// Bun: OTel instrumentation-http/-express don't actually patch Bun's HTTP layer even when
|
|
76
|
+
// NodeSDK.start() succeeds. Force-install legacy patches under Bun so we actually see traffic.
|
|
77
|
+
const isBunRuntime = typeof globalThis.Bun !== 'undefined';
|
|
78
|
+
if (!tracingOn || isBunRuntime) {
|
|
76
79
|
instrumentHttp();
|
|
77
80
|
instrumentDependencies();
|
|
78
81
|
}
|
|
79
82
|
|
|
80
83
|
// #region agent log
|
|
81
84
|
try {
|
|
82
|
-
console.log('[SG-APM-DBG]', JSON.stringify({sessionId:'ecc573',runId:'
|
|
85
|
+
console.log('[SG-APM-DBG]', JSON.stringify({sessionId:'ecc573',runId:'post-fix',hypothesisId:'A,B',location:'index.js:initialize',message:'SDK init runtime + instrumentation flow (Bun-aware)',data:{isBun:isBunRuntime,bunVersion:globalThis.Bun?.version||null,nodeVersion:process.versions?.node||null,tracingOn,legacyHttpInstalled:!tracingOn||isBunRuntime,service:config.service,endpoint:config.endpoint,argv0:process.argv0,execPath:process.execPath},timestamp:Date.now()}));
|
|
83
86
|
} catch {}
|
|
84
87
|
// #endregion
|
|
85
88
|
|
package/src/mongodb.js
CHANGED
|
@@ -377,6 +377,40 @@ function ensureMonitorCommands(client) {
|
|
|
377
377
|
}
|
|
378
378
|
}
|
|
379
379
|
|
|
380
|
+
/**
|
|
381
|
+
* Wrap mongoose.connect (and createConnection) to inject monitorCommands:true.
|
|
382
|
+
* The MongoDB driver reads monitorCommands at connect time; flipping it later is a no-op.
|
|
383
|
+
*/
|
|
384
|
+
function wrapMongooseConnectForMonitorCommands(mongoose) {
|
|
385
|
+
if (!mongoose || mongoose.__sentienguardConnectWrapped) return;
|
|
386
|
+
try {
|
|
387
|
+
if (typeof mongoose.connect === 'function') {
|
|
388
|
+
const origConnect = mongoose.connect.bind(mongoose);
|
|
389
|
+
mongoose.connect = function sentienguardConnect(uri, options, ...rest) {
|
|
390
|
+
const opts = { ...(options || {}), monitorCommands: true };
|
|
391
|
+
debug('Injecting monitorCommands:true into mongoose.connect()');
|
|
392
|
+
// #region agent log
|
|
393
|
+
try {
|
|
394
|
+
console.log('[SG-APM-DBG]', JSON.stringify({sessionId:'ecc573',runId:'post-fix',hypothesisId:'C',location:'mongodb.js:wrapMongooseConnect',message:'mongoose.connect called - injected monitorCommands:true',data:{hadOptions:!!options,userMonitorCommands:options?.monitorCommands??null},timestamp:Date.now()}));
|
|
395
|
+
} catch {}
|
|
396
|
+
// #endregion
|
|
397
|
+
return origConnect(uri, opts, ...rest);
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
if (typeof mongoose.createConnection === 'function') {
|
|
401
|
+
const origCreate = mongoose.createConnection.bind(mongoose);
|
|
402
|
+
mongoose.createConnection = function sentienguardCreateConnection(uri, options, ...rest) {
|
|
403
|
+
const opts = { ...(options || {}), monitorCommands: true };
|
|
404
|
+
return origCreate(uri, opts, ...rest);
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
mongoose.__sentienguardConnectWrapped = true;
|
|
408
|
+
debug('Wrapped mongoose.connect/createConnection with monitorCommands injector');
|
|
409
|
+
} catch (err) {
|
|
410
|
+
debug(`Failed to wrap mongoose.connect: ${err.message}`);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
|
|
380
414
|
/**
|
|
381
415
|
* Attach instrumentation to a detected mongoose instance.
|
|
382
416
|
* Handles waiting for the connection to be ready.
|
|
@@ -388,20 +422,19 @@ function attachToMongoose(mongoose) {
|
|
|
388
422
|
}
|
|
389
423
|
|
|
390
424
|
if (mongoose.connection.readyState === 1) {
|
|
391
|
-
// Already connected
|
|
425
|
+
// Already connected — too late to inject monitorCommands; instrument what we can.
|
|
392
426
|
instrumentMongoDB(mongoose);
|
|
393
|
-
} else if (mongoose.connection.readyState === 2) {
|
|
394
|
-
// Connecting — wait for it
|
|
395
|
-
mongoose.connection.once('connected', () => {
|
|
396
|
-
instrumentMongoDB(mongoose);
|
|
397
|
-
});
|
|
398
|
-
debug('Waiting for Mongoose connection to complete before instrumenting');
|
|
399
427
|
} else {
|
|
400
|
-
// Not connected
|
|
428
|
+
// Not yet connected (readyState 0 or 2): inject monitorCommands BEFORE connect runs.
|
|
429
|
+
wrapMongooseConnectForMonitorCommands(mongoose);
|
|
401
430
|
mongoose.connection.once('connected', () => {
|
|
402
431
|
instrumentMongoDB(mongoose);
|
|
403
432
|
});
|
|
404
|
-
debug(
|
|
433
|
+
debug(
|
|
434
|
+
mongoose.connection.readyState === 2
|
|
435
|
+
? 'Mongoose connecting — will instrument on connected event'
|
|
436
|
+
: 'Mongoose not yet connecting — will instrument on connected event'
|
|
437
|
+
);
|
|
405
438
|
}
|
|
406
439
|
}
|
|
407
440
|
|