From 65ee51cd2ffeb6f1d707e7ecd536826e7adf644a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E8=80=80?= Date: Mon, 18 Nov 2024 15:15:49 +0800 Subject: [PATCH 1/8] Modify mongoose call lean().exec() throw is not a function --- src/plugins/MongoosePlugin.ts | 41 ++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/src/plugins/MongoosePlugin.ts b/src/plugins/MongoosePlugin.ts index 234abd5..d9ecf28 100644 --- a/src/plugins/MongoosePlugin.ts +++ b/src/plugins/MongoosePlugin.ts @@ -128,7 +128,46 @@ class MongoosePlugin implements SwPlugin { (span as any).mongooseInCall = false; if (!hasCB) { - if (ret && typeof ret.then === 'function') { + if (ret && typeof ret === 'object' && ret.constructor.name === 'Query') { + console.log("2"); + // Mongoose Query object + const originalThen = ret.then; + const originalExec = ret.exec; + const originalLean = ret.lean; + + // Preserve the query chain methods + ret.then = function (...args) { + return SwPlugin_1.wrapPromise(span, originalThen.apply(this, args)); + }; + + ret.exec = function (...args) { + return SwPlugin_1.wrapPromise(span, originalExec.apply(this, args)); + }; + + ret.lean = function (...args) { + const leanQuery = originalLean.apply(this, args); + // Preserve other chain methods on lean result + leanQuery.then = ret.then; + leanQuery.exec = ret.exec; + return leanQuery; + }; + + // Wrap other common query methods that might be chained + const chainMethods = ['select', 'sort', 'skip', 'limit', 'populate']; + chainMethods.forEach(method => { + if (ret[method]) { + const original = ret[method]; + ret[method] = function (...args) { + const result = original.apply(this, args); + result.then = ret.then; + result.exec = ret.exec; + result.lean = ret.lean; + return result; + }; + } + }); + return ret; + } else if (ret && typeof ret.then === 'function') { // generic Promise check ret = wrapPromise(span, ret); } else { From bac3f420c4822f42286d93931b361a9672f21b03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E8=80=80?= Date: Mon, 18 Nov 2024 15:20:55 +0800 Subject: [PATCH 2/8] Remove debug code --- src/plugins/MongoosePlugin.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/plugins/MongoosePlugin.ts b/src/plugins/MongoosePlugin.ts index d9ecf28..ad3ba21 100644 --- a/src/plugins/MongoosePlugin.ts +++ b/src/plugins/MongoosePlugin.ts @@ -129,7 +129,6 @@ class MongoosePlugin implements SwPlugin { if (!hasCB) { if (ret && typeof ret === 'object' && ret.constructor.name === 'Query') { - console.log("2"); // Mongoose Query object const originalThen = ret.then; const originalExec = ret.exec; From 20bbae49f62930329ec11157b8aee75a13e84c51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E8=80=80?= Date: Tue, 19 Nov 2024 12:04:37 +0800 Subject: [PATCH 3/8] Update MongoosePlugin.ts --- src/plugins/MongoosePlugin.ts | 74 +++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 30 deletions(-) diff --git a/src/plugins/MongoosePlugin.ts b/src/plugins/MongoosePlugin.ts index ad3ba21..93f0242 100644 --- a/src/plugins/MongoosePlugin.ts +++ b/src/plugins/MongoosePlugin.ts @@ -130,42 +130,56 @@ class MongoosePlugin implements SwPlugin { if (!hasCB) { if (ret && typeof ret === 'object' && ret.constructor.name === 'Query') { // Mongoose Query object - const originalThen = ret.then; - const originalExec = ret.exec; - const originalLean = ret.lean; - - // Preserve the query chain methods - ret.then = function (...args) { - return SwPlugin_1.wrapPromise(span, originalThen.apply(this, args)); + const chainMethods = ['then', 'exec', 'lean', 'select', 'sort', 'skip', 'limit', 'populate']; + + // Store the original methods for chaining + const originalMethods: { [key: string]: Function } = {}; + const originalThen: Function = ret.then + const originalExec: Function = ret.exec + const originalLean: Function = ret.lean + ret.then = function (...args: any) { + return wrapPromise(span, originalThen.apply(this, args)); }; - ret.exec = function (...args) { - return SwPlugin_1.wrapPromise(span, originalExec.apply(this, args)); + ret.exec = function (...args: any) { + return wrapPromise(span, originalExec.apply(this, args)); }; - ret.lean = function (...args) { - const leanQuery = originalLean.apply(this, args); - // Preserve other chain methods on lean result - leanQuery.then = ret.then; - leanQuery.exec = ret.exec; - return leanQuery; + ret.lean = function (...args: any) { + const leanQuery = originalLean.apply(this, args); + // Preserve other chain methods on lean result + leanQuery.then = ret.then; + leanQuery.exec = ret.exec; + return leanQuery; }; - // Wrap other common query methods that might be chained - const chainMethods = ['select', 'sort', 'skip', 'limit', 'populate']; - chainMethods.forEach(method => { - if (ret[method]) { - const original = ret[method]; - ret[method] = function (...args) { - const result = original.apply(this, args); - result.then = ret.then; - result.exec = ret.exec; - result.lean = ret.lean; - return result; - }; - } + // Preserve the original methods + chainMethods.forEach((method) => { + if (ret[method]) { + originalMethods[method] = ret[method]; + } }); - return ret; + + // Override the methods to wrap them with tracing functionality + chainMethods.forEach((method) => { + if (originalMethods[method]) { + ret[method] = function (...args: any[]) { + const result = originalMethods[method].apply(this, args); + + // If it's a query, wrap its `then`, `exec`, and `lean` methods to preserve the trace + if (method === 'then' || method === 'exec' || method === 'lean') { + return wrapPromise(span, result); + } + + // Ensure chaining continues correctly for other methods (e.g., select, sort) + result.then = ret.then; + result.exec = ret.exec; + result.lean = ret.lean; + return result; + }; + } + }); + } else if (ret && typeof ret.then === 'function') { // generic Promise check ret = wrapPromise(span, ret); @@ -180,7 +194,7 @@ class MongoosePlugin implements SwPlugin { span.async(); return ret; - } catch (err) { + } catch (err: any) { span.error(err); span.stop(); From 9075b1ea1d989a0eb8af3bb3a440b7cd16f55632 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E8=80=80?= Date: Tue, 19 Nov 2024 13:42:29 +0800 Subject: [PATCH 4/8] Update MongoosePlugin.ts --- src/plugins/MongoosePlugin.ts | 52 +++++++++++------------------------ 1 file changed, 16 insertions(+), 36 deletions(-) diff --git a/src/plugins/MongoosePlugin.ts b/src/plugins/MongoosePlugin.ts index 93f0242..6a97911 100644 --- a/src/plugins/MongoosePlugin.ts +++ b/src/plugins/MongoosePlugin.ts @@ -130,48 +130,28 @@ class MongoosePlugin implements SwPlugin { if (!hasCB) { if (ret && typeof ret === 'object' && ret.constructor.name === 'Query') { // Mongoose Query object - const chainMethods = ['then', 'exec', 'lean', 'select', 'sort', 'skip', 'limit', 'populate']; - - // Store the original methods for chaining - const originalMethods: { [key: string]: Function } = {}; - const originalThen: Function = ret.then - const originalExec: Function = ret.exec - const originalLean: Function = ret.lean - ret.then = function (...args: any) { - return wrapPromise(span, originalThen.apply(this, args)); - }; - - ret.exec = function (...args: any) { - return wrapPromise(span, originalExec.apply(this, args)); - }; + const chainMethods = ['select', 'sort', 'skip', 'limit', 'populate']; - ret.lean = function (...args: any) { - const leanQuery = originalLean.apply(this, args); - // Preserve other chain methods on lean result + // 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; }; - - // Preserve the original methods + // Wrap other common query methods that might be chained chainMethods.forEach((method) => { if (ret[method]) { - originalMethods[method] = ret[method]; - } - }); - - // Override the methods to wrap them with tracing functionality - chainMethods.forEach((method) => { - if (originalMethods[method]) { - ret[method] = function (...args: any[]) { - const result = originalMethods[method].apply(this, args); - - // If it's a query, wrap its `then`, `exec`, and `lean` methods to preserve the trace - if (method === 'then' || method === 'exec' || method === 'lean') { - return wrapPromise(span, result); - } - - // Ensure chaining continues correctly for other methods (e.g., select, sort) + 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; @@ -179,7 +159,7 @@ class MongoosePlugin implements SwPlugin { }; } }); - + return ret; } else if (ret && typeof ret.then === 'function') { // generic Promise check ret = wrapPromise(span, ret); From 18469b36612e09d1ce5a9a50c09085574abc9c28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E8=80=80?= Date: Tue, 19 Nov 2024 13:45:36 +0800 Subject: [PATCH 5/8] Update MongoosePlugin.ts Remove catch type --- src/plugins/MongoosePlugin.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins/MongoosePlugin.ts b/src/plugins/MongoosePlugin.ts index 6a97911..6d53599 100644 --- a/src/plugins/MongoosePlugin.ts +++ b/src/plugins/MongoosePlugin.ts @@ -174,7 +174,7 @@ class MongoosePlugin implements SwPlugin { span.async(); return ret; - } catch (err: any) { + } catch (err) { span.error(err); span.stop(); From f4280f5acf24c50f933b01e5c4f56eb14b99ac0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E8=80=80?= Date: Tue, 19 Nov 2024 13:49:24 +0800 Subject: [PATCH 6/8] Update MongoosePlugin.ts --- src/plugins/MongoosePlugin.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins/MongoosePlugin.ts b/src/plugins/MongoosePlugin.ts index 6d53599..2688e66 100644 --- a/src/plugins/MongoosePlugin.ts +++ b/src/plugins/MongoosePlugin.ts @@ -159,7 +159,7 @@ class MongoosePlugin implements SwPlugin { }; } }); - return ret; + } else if (ret && typeof ret.then === 'function') { // generic Promise check ret = wrapPromise(span, ret); From 7381125469734f4fcf56eb178413df176863f202 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E8=80=80?= Date: Tue, 19 Nov 2024 13:49:54 +0800 Subject: [PATCH 7/8] 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 2688e66..e9cda0e 100644 --- a/src/plugins/MongoosePlugin.ts +++ b/src/plugins/MongoosePlugin.ts @@ -159,7 +159,6 @@ class MongoosePlugin implements SwPlugin { }; } }); - } else if (ret && typeof ret.then === 'function') { // generic Promise check ret = wrapPromise(span, ret); From a68d4da671a8ba5a742c9efd5852fbea9f8b38d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E8=80=80?= Date: Thu, 21 Nov 2024 10:36:54 +0800 Subject: [PATCH 8/8] Modify ci error --- src/plugins/MongoosePlugin.ts | 70 ++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 33 deletions(-) diff --git a/src/plugins/MongoosePlugin.ts b/src/plugins/MongoosePlugin.ts index e9cda0e..26523f0 100644 --- a/src/plugins/MongoosePlugin.ts +++ b/src/plugins/MongoosePlugin.ts @@ -128,40 +128,44 @@ class MongoosePlugin implements SwPlugin { (span as any).mongooseInCall = false; if (!hasCB) { - if (ret && typeof ret === 'object' && ret.constructor.name === 'Query') { - // 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 if (ret && typeof ret.then === 'function') { + 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();