From 5661567d374026103646096617a287962b750169 Mon Sep 17 00:00:00 2001 From: shivendoodeshmukh Date: Fri, 22 Aug 2025 12:29:08 +0530 Subject: [PATCH 1/4] Update MongoosePlugin.ts --- src/plugins/MongoosePlugin.ts | 37 ++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/plugins/MongoosePlugin.ts b/src/plugins/MongoosePlugin.ts index 234abd5..26523f0 100644 --- a/src/plugins/MongoosePlugin.ts +++ b/src/plugins/MongoosePlugin.ts @@ -130,7 +130,42 @@ class MongoosePlugin implements SwPlugin { if (!hasCB) { if (ret && typeof ret.then === 'function') { // generic Promise check - ret = wrapPromise(span, ret); + + if (ret.constructor.name != 'Query') { + ret = wrapPromise(span, ret); + } else { + // Mongoose Query object + const chainMethods = ['select', 'sort', 'skip', 'limit', 'populate']; + + // Mongoose Query object + const originalThen = ret.then; + const originalExec = ret.exec; + const originalLean = ret.lean; + + // Preserve the query chain methods using arrow functions to maintain context + ret.then = (...args: any[]) => wrapPromise(span, originalThen.apply(ret, args)); + ret.exec = (...args: any[]) => wrapPromise(span, originalExec.apply(ret, args)); + ret.lean = (...args: any[]) => { + const leanQuery = originalLean.apply(ret, args); + // Preserve other chain methods on the lean result + leanQuery.then = ret.then; + leanQuery.exec = ret.exec; + return leanQuery; + }; + // Wrap other common query methods that might be chained + chainMethods.forEach((method) => { + if (ret[method]) { + const originalMethod = ret[method]; + ret[method] = (...args: any[]) => { + const result = originalMethod.apply(ret, args); + result.then = ret.then; + result.exec = ret.exec; + result.lean = ret.lean; + return result; + }; + } + }); + } } else { // no callback passed in and no Promise or Cursor returned, play it safe span.stop(); From dbbb29eb5cf17d54dff9ef995ab33a8a70eb5e54 Mon Sep 17 00:00:00 2001 From: shivendoodeshmukh Date: Fri, 22 Aug 2025 12:29:45 +0530 Subject: [PATCH 2/4] Update MongoosePlugin.ts --- src/plugins/MongoosePlugin.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/plugins/MongoosePlugin.ts b/src/plugins/MongoosePlugin.ts index 26523f0..e5348d4 100644 --- a/src/plugins/MongoosePlugin.ts +++ b/src/plugins/MongoosePlugin.ts @@ -118,7 +118,6 @@ class MongoosePlugin implements SwPlugin { arguments[arguments.length - 1] = function () { // in case of immediate synchronous callback from mongoose (span as any).mongooseInCall = false; - wrappedCallback.apply(this, arguments as any); }; } From 781d3c2d0fe58a35093b0c947d97679f7e4fa2be Mon Sep 17 00:00:00 2001 From: shivendoodeshmukh Date: Fri, 22 Aug 2025 15:12:14 +0530 Subject: [PATCH 3/4] Update client.ts --- tests/plugins/mongoose/client.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/plugins/mongoose/client.ts b/tests/plugins/mongoose/client.ts index 25ff2b3..c3f4ed3 100644 --- a/tests/plugins/mongoose/client.ts +++ b/tests/plugins/mongoose/client.ts @@ -21,6 +21,7 @@ import * as http from 'http'; import agent from '../../../src'; process.env.SW_AGENT_LOGGING_LEVEL = 'ERROR'; +process.env.SW_AGENT_DISABLE_PLUGINS = 'MongoDBPlugin'; agent.start({ serviceName: 'client', From 13bf85b3f403af78ff6efaf03e69a80d5e1c3c6d Mon Sep 17 00:00:00 2001 From: shivendoodeshmukh Date: Fri, 22 Aug 2025 15:23:48 +0530 Subject: [PATCH 4/4] Update expected.data.yaml --- tests/plugins/mongoose/expected.data.yaml | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/tests/plugins/mongoose/expected.data.yaml b/tests/plugins/mongoose/expected.data.yaml index 452d72b..a2400d8 100644 --- a/tests/plugins/mongoose/expected.data.yaml +++ b/tests/plugins/mongoose/expected.data.yaml @@ -35,7 +35,7 @@ segmentItems: tags: - { key: db.type, value: MongoDB } - { key: db.instance, value: admin } - - { key: db.statement, value: 'collection("tests")' } + - { key: db.statement, 'collection("tests")' } - operationName: Mongoose/ensureIndexes operationId: 0 parentSpanId: 0 @@ -50,6 +50,21 @@ segmentItems: tags: - { key: db.type, value: MongoDB } - { key: db.instance, value: admin } + - operationName: MongoDB/find # This is the required span + operationId: 0 + parentSpanId: 0 + spanId: 4 + spanLayer: Database + startTime: gt 0 + endTime: gt 0 + componentId: 9 + spanType: Exit + peer: mongo:27017 + skipAnalysis: false + tags: + - { key: db.type, value: MongoDB } + - { key: db.instance, value: admin } + - { key: db.statement, 'tests.find({})' } - operationName: Mongoose/find operationId: 0 parentSpanId: 0 @@ -64,7 +79,7 @@ segmentItems: tags: - { key: db.type, value: MongoDB } - { key: db.instance, value: admin } - - { key: db.statement, value: 'tests.find({})' } + # The mongoose find span might not have the db.statement, which is fine - operationName: GET:/mongoose operationId: 0 parentSpanId: -1