@sentienguard/apm 1.0.22-debug.1 → 1.0.23-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 +0 -6
- package/src/mongodb.js +1 -36
- package/src/spanExporter.js +0 -6
- package/src/traceSpanExporter.js +7 -0
- package/src/traceTransport.js +18 -1
- package/src/tracing.js +1 -1
- package/src/transport.js +0 -8
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -80,12 +80,6 @@ function initialize() {
|
|
|
80
80
|
instrumentDependencies();
|
|
81
81
|
}
|
|
82
82
|
|
|
83
|
-
// #region agent log
|
|
84
|
-
try {
|
|
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()}));
|
|
86
|
-
} catch {}
|
|
87
|
-
// #endregion
|
|
88
|
-
|
|
89
83
|
// Auto-instrument MongoDB if available
|
|
90
84
|
autoInstrumentMongoDB();
|
|
91
85
|
|
package/src/mongodb.js
CHANGED
|
@@ -106,12 +106,6 @@ function handleCommandStarted(event) {
|
|
|
106
106
|
|
|
107
107
|
const commandName = event.commandName;
|
|
108
108
|
|
|
109
|
-
// #region agent log
|
|
110
|
-
try {
|
|
111
|
-
console.log('[SG-APM-DBG]', JSON.stringify({sessionId:'ecc573',runId:'mongo-cmd',hypothesisId:'C',location:'mongodb.js:handleCommandStarted',message:'commandStarted event fired',data:{commandName,databaseName:event.databaseName,ignored:IGNORED_COMMANDS.has(commandName),requestId:String(event.requestId||'')},timestamp:Date.now()}));
|
|
112
|
-
} catch {}
|
|
113
|
-
// #endregion
|
|
114
|
-
|
|
115
109
|
// Skip ignored commands
|
|
116
110
|
if (IGNORED_COMMANDS.has(commandName)) {
|
|
117
111
|
return;
|
|
@@ -269,42 +263,26 @@ function collectPoolStats() {
|
|
|
269
263
|
* also populate require.cache via the CJS interop layer.
|
|
270
264
|
*/
|
|
271
265
|
function tryDetectMongoose() {
|
|
272
|
-
let cacheKeyCount = 0;
|
|
273
|
-
let mongooseKeyMatched = null;
|
|
274
|
-
let cacheError = null;
|
|
275
266
|
try {
|
|
276
267
|
// Strategy 1: Scan require.cache for mongoose
|
|
277
268
|
const cache = require.cache || {};
|
|
278
269
|
const cacheKeys = Object.keys(cache);
|
|
279
|
-
cacheKeyCount = cacheKeys.length;
|
|
280
270
|
const mongooseKey = cacheKeys.find(
|
|
281
271
|
k => /[\\/]mongoose[\\/](?:lib[\\/])?index\.js$/.test(k) &&
|
|
282
272
|
!k.includes('node_modules/mongoose/node_modules')
|
|
283
273
|
);
|
|
284
|
-
mongooseKeyMatched = mongooseKey || null;
|
|
285
274
|
if (mongooseKey && cache[mongooseKey]?.exports) {
|
|
286
275
|
const mod = cache[mongooseKey].exports;
|
|
287
276
|
// Verify it's actually mongoose (has connection property)
|
|
288
277
|
if (mod.connection) {
|
|
289
278
|
debug('Detected mongoose via require.cache');
|
|
290
|
-
// #region agent log
|
|
291
|
-
try {
|
|
292
|
-
console.log('[SG-APM-DBG]', JSON.stringify({sessionId:'ecc573',runId:'mongo-detect',hypothesisId:'D',location:'mongodb.js:tryDetectMongoose',message:'mongoose detected via require.cache',data:{isBun:typeof globalThis.Bun!=='undefined',cacheKeyCount,matchedKey:mongooseKey,readyState:mod.connection?.readyState??null},timestamp:Date.now()}));
|
|
293
|
-
} catch {}
|
|
294
|
-
// #endregion
|
|
295
279
|
return mod;
|
|
296
280
|
}
|
|
297
281
|
}
|
|
298
282
|
} catch (e) {
|
|
299
|
-
|
|
283
|
+
// require.cache not available (unlikely in Node.js)
|
|
300
284
|
}
|
|
301
285
|
|
|
302
|
-
// #region agent log
|
|
303
|
-
try {
|
|
304
|
-
console.log('[SG-APM-DBG]', JSON.stringify({sessionId:'ecc573',runId:'mongo-detect',hypothesisId:'D',location:'mongodb.js:tryDetectMongoose',message:'mongoose NOT detected (require.cache scan)',data:{isBun:typeof globalThis.Bun!=='undefined',hasRequire:typeof require!=='undefined',hasRequireCache:typeof require!=='undefined' && !!require.cache,cacheKeyCount,mongooseKeyMatched,cacheError,globalThisMongoose:!!globalThis.mongoose},timestamp:Date.now()}));
|
|
305
|
-
} catch {}
|
|
306
|
-
// #endregion
|
|
307
|
-
|
|
308
286
|
// Strategy 2: Fallback — check globalThis (in case user set it manually)
|
|
309
287
|
try {
|
|
310
288
|
if (globalThis.mongoose?.connection) {
|
|
@@ -328,14 +306,6 @@ function ensureMonitorCommands(client) {
|
|
|
328
306
|
client.options?.monitorCommands ||
|
|
329
307
|
client.s?.options?.monitorCommands;
|
|
330
308
|
|
|
331
|
-
// #region agent log
|
|
332
|
-
try {
|
|
333
|
-
const optsRoot = client.options ? Object.keys(client.options).slice(0, 30) : null;
|
|
334
|
-
const optsS = client.s?.options ? Object.keys(client.s.options).slice(0, 30) : null;
|
|
335
|
-
console.log('[SG-APM-DBG]', JSON.stringify({sessionId:'ecc573',runId:'mongo-init',hypothesisId:'C',location:'mongodb.js:ensureMonitorCommands',message:'monitorCommands inspection on live MongoClient',data:{alreadyEnabled:!!alreadyEnabled,hasClientOptions:!!client.options,hasClientSOptions:!!client.s?.options,clientOptionsFrozen:client.options ? Object.isFrozen(client.options) : null,clientSOptionsFrozen:client.s?.options ? Object.isFrozen(client.s.options) : null,optsRootKeys:optsRoot,optsSKeys:optsS},timestamp:Date.now()}));
|
|
336
|
-
} catch {}
|
|
337
|
-
// #endregion
|
|
338
|
-
|
|
339
309
|
if (alreadyEnabled) {
|
|
340
310
|
debug('monitorCommands already enabled');
|
|
341
311
|
return true;
|
|
@@ -389,11 +359,6 @@ function wrapMongooseConnectForMonitorCommands(mongoose) {
|
|
|
389
359
|
mongoose.connect = function sentienguardConnect(uri, options, ...rest) {
|
|
390
360
|
const opts = { ...(options || {}), monitorCommands: true };
|
|
391
361
|
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
362
|
return origConnect(uri, opts, ...rest);
|
|
398
363
|
};
|
|
399
364
|
}
|
package/src/spanExporter.js
CHANGED
|
@@ -146,12 +146,6 @@ export function classifyAndRecordSpan(span) {
|
|
|
146
146
|
let error = isErrorSpan(span);
|
|
147
147
|
const kind = span.kind;
|
|
148
148
|
|
|
149
|
-
// #region agent log
|
|
150
|
-
try {
|
|
151
|
-
console.log('[SG-APM-DBG]', JSON.stringify({sessionId:'ecc573',runId:'span',hypothesisId:'A,E',location:'spanExporter.js:classifyAndRecordSpan',message:'span received by exporter',data:{kind,name:span.name,latencyMs:Math.round(latency),httpMethod:attr(attrs,HTTP_METHOD)||null,httpRoute:attr(attrs,HTTP_ROUTE)||null,httpTarget:attr(attrs,HTTP_TARGET)||null,httpStatus:attr(attrs,HTTP_STATUS)||null,dbSystem:attr(attrs,DB_SYSTEM)||null,dbCollection:attr(attrs,DB_COLLECTION)||null,dbOperation:attr(attrs,DB_OPERATION)||null,peerName:attr(attrs,NET_PEER_NAME)||null,error},timestamp:Date.now()}));
|
|
152
|
-
} catch {}
|
|
153
|
-
// #endregion
|
|
154
|
-
|
|
155
149
|
if (kind === SpanKind.SERVER) {
|
|
156
150
|
const method = attr(attrs, HTTP_METHOD) || 'GET';
|
|
157
151
|
let route = attr(attrs, HTTP_ROUTE) || attr(attrs, HTTP_TARGET) || '/';
|
package/src/traceSpanExporter.js
CHANGED
|
@@ -146,6 +146,13 @@ export class SentienGuardTraceSpanExporter {
|
|
|
146
146
|
}
|
|
147
147
|
}
|
|
148
148
|
|
|
149
|
+
// #region agent log
|
|
150
|
+
try {
|
|
151
|
+
const first = serialized[0] || null;
|
|
152
|
+
console.log('[SG-APM-DBG]', JSON.stringify({sessionId:'ecc573',runId:'trace-export',hypothesisId:'F,H,I',location:'traceSpanExporter.js:export',message:'export() called',data:{receivedCount:Array.isArray(spans)?spans.length:0,sampledCount:serialized.length,sampleRate:rate,firstName:first?.name||null,firstKind:first?.kind||null,firstTraceId:first?.trace_id||null},timestamp:Date.now()}));
|
|
153
|
+
} catch {}
|
|
154
|
+
// #endregion
|
|
155
|
+
|
|
149
156
|
if (serialized.length) {
|
|
150
157
|
enqueueSpans(serialized);
|
|
151
158
|
}
|
package/src/traceTransport.js
CHANGED
|
@@ -97,14 +97,25 @@ async function flushOnce(batch) {
|
|
|
97
97
|
};
|
|
98
98
|
|
|
99
99
|
try {
|
|
100
|
-
|
|
100
|
+
const startedAt = Date.now();
|
|
101
|
+
const res = await sendToBackend(payload);
|
|
101
102
|
consecutiveFailures = 0;
|
|
102
103
|
lastFailureAtMs = 0;
|
|
103
104
|
debug(`Trace flush ok: spans=${batch.length}`);
|
|
105
|
+
// #region agent log
|
|
106
|
+
try {
|
|
107
|
+
console.log('[SG-APM-DBG]', JSON.stringify({sessionId:'ecc573',runId:'trace-send',hypothesisId:'G',location:'traceTransport.js:flushOnce',message:'sendToBackend SUCCESS',data:{spans:batch.length,statusCode:res?.statusCode||null,durationMs:Date.now()-startedAt,endpoint:cfg.tracesEndpoint},timestamp:Date.now()}));
|
|
108
|
+
} catch {}
|
|
109
|
+
// #endregion
|
|
104
110
|
} catch (err) {
|
|
105
111
|
consecutiveFailures++;
|
|
106
112
|
lastFailureAtMs = Date.now();
|
|
107
113
|
warn(`Trace flush failed (attempt ${consecutiveFailures}): ${err.message}`);
|
|
114
|
+
// #region agent log
|
|
115
|
+
try {
|
|
116
|
+
console.log('[SG-APM-DBG]', JSON.stringify({sessionId:'ecc573',runId:'trace-send',hypothesisId:'G',location:'traceTransport.js:flushOnce',message:'sendToBackend FAILED',data:{spans:batch.length,error:err?.message||String(err),consecutiveFailures,endpoint:cfg.tracesEndpoint},timestamp:Date.now()}));
|
|
117
|
+
} catch {}
|
|
118
|
+
// #endregion
|
|
108
119
|
if (consecutiveFailures >= MAX_CONSECUTIVE_FAILURES) {
|
|
109
120
|
// Stop retrying aggressively; drop future spans until backend recovers.
|
|
110
121
|
warn('Trace flush: max failures reached; dropping spans under backpressure');
|
|
@@ -154,6 +165,12 @@ export function enqueueSpans(serializedSpans) {
|
|
|
154
165
|
const cfg = getConfig();
|
|
155
166
|
const maxQueue = cfg.tracing?.maxQueueSize || 2048;
|
|
156
167
|
|
|
168
|
+
// #region agent log
|
|
169
|
+
try {
|
|
170
|
+
console.log('[SG-APM-DBG]', JSON.stringify({sessionId:'ecc573',runId:'trace-enqueue',hypothesisId:'F,G',location:'traceTransport.js:enqueueSpans',message:'enqueueSpans called',data:{incomingCount:Array.isArray(serializedSpans)?serializedSpans.length:0,queueLenBefore:queue.length,maxQueue,sdkEnabled:isEnabled(),scheduled,consecutiveFailures},timestamp:Date.now()}));
|
|
171
|
+
} catch {}
|
|
172
|
+
// #endregion
|
|
173
|
+
|
|
157
174
|
if (!Array.isArray(serializedSpans) || serializedSpans.length === 0) return;
|
|
158
175
|
if (!isEnabled()) return;
|
|
159
176
|
|
package/src/tracing.js
CHANGED
|
@@ -195,7 +195,7 @@ export function startTracing() {
|
|
|
195
195
|
debug('OpenTelemetry tracing started (W3C Trace Context + HTTP/Express)');
|
|
196
196
|
// #region agent log
|
|
197
197
|
try {
|
|
198
|
-
console.log('[SG-APM-DBG]', JSON.stringify({sessionId:'ecc573',runId:'init',hypothesisId:'
|
|
198
|
+
console.log('[SG-APM-DBG]', JSON.stringify({sessionId:'ecc573',runId:'trace-init',hypothesisId:'F,H,J',location:'tracing.js:startTracing',message:'tracing config snapshot',data:{isBun:typeof globalThis.Bun!=='undefined',tracingEnabled:cfg.tracing?.enabled,sampleRate:cfg.tracing?.sampleRate,maxQueueSize:cfg.tracing?.maxQueueSize,maxBatchSize:cfg.tracing?.maxBatchSize,endpoint:cfg.endpoint,tracesEndpoint:cfg.tracesEndpoint,service:cfg.service,environment:cfg.environment,apiKeyPresent:!!cfg.apiKey,apiKeyLen:cfg.apiKey?.length||0},timestamp:Date.now()}));
|
|
199
199
|
} catch {}
|
|
200
200
|
// #endregion
|
|
201
201
|
return true;
|
package/src/transport.js
CHANGED
|
@@ -112,14 +112,6 @@ async function flush() {
|
|
|
112
112
|
|
|
113
113
|
const aggregator = getAggregator();
|
|
114
114
|
|
|
115
|
-
// #region agent log
|
|
116
|
-
try {
|
|
117
|
-
const stats = aggregator.getStats();
|
|
118
|
-
const pool = aggregator.mongodbPoolStats || null;
|
|
119
|
-
console.log('[SG-APM-DBG]', JSON.stringify({sessionId:'ecc573',runId:'flush',hypothesisId:'A,B,C,D,E',location:'transport.js:flush',message:'flush() invoked - aggregator state',data:{hasData:aggregator.hasData(),...stats,poolUpdated:!!pool?.lastUpdated,poolActive:pool?.active??null,poolIdle:pool?.idle??null,poolTotal:pool?.total??null,isBun:typeof globalThis.Bun!=='undefined'},timestamp:Date.now()}));
|
|
120
|
-
} catch {}
|
|
121
|
-
// #endregion
|
|
122
|
-
|
|
123
115
|
// Skip if no data
|
|
124
116
|
if (!aggregator.hasData()) {
|
|
125
117
|
debug('No data to flush');
|