From 578f0754f8344e2144271f01404e2e42d2cc8cad Mon Sep 17 00:00:00 2001 From: Abhilash Date: Mon, 23 Feb 2026 12:49:45 +0530 Subject: [PATCH 1/3] fix: added safety promise checks in redis, mongodb --- .../tracing/instrumentation/databases/ioredis.js | 15 ++++++++++----- .../tracing/instrumentation/databases/mongodb.js | 7 ++++++- .../tracing/instrumentation/databases/redis.js | 7 +++++-- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/packages/core/src/tracing/instrumentation/databases/ioredis.js b/packages/core/src/tracing/instrumentation/databases/ioredis.js index e88a1c69e3..9e2923297c 100644 --- a/packages/core/src/tracing/instrumentation/databases/ioredis.js +++ b/packages/core/src/tracing/instrumentation/databases/ioredis.js @@ -97,11 +97,16 @@ function instrumentSendCommand(original) { span.stack = tracingUtil.getStackTrace(wrappedInternalSendCommand); callback = cls.ns.bind(onResult); - command.promise.then( - // make sure that the first parameter is never truthy - callback.bind(null, null), - callback - ); + if (typeof command.promise?.then === 'function') { + command.promise.then( + // make sure that the first parameter is never truthy + callback.bind(null, null), + callback + ); + } else { + tracingUtil.handleUnexpectedReturnValue(command.promise, exports.spanName, `command "${command.name}"`); + callback(null); + } return original.apply(client, argsForOriginal); diff --git a/packages/core/src/tracing/instrumentation/databases/mongodb.js b/packages/core/src/tracing/instrumentation/databases/mongodb.js index 7582e1917c..9160de9d5d 100644 --- a/packages/core/src/tracing/instrumentation/databases/mongodb.js +++ b/packages/core/src/tracing/instrumentation/databases/mongodb.js @@ -457,7 +457,7 @@ function handleCallbackOrPromise(ctx, originalArgs, originalFunction, span) { const resultPromise = originalFunction.apply(ctx, originalArgs); - if (resultPromise && resultPromise.then) { + if (resultPromise && typeof resultPromise.then === 'function') { resultPromise .then(result => { span.d = Date.now() - span.ts; @@ -471,6 +471,11 @@ function handleCallbackOrPromise(ctx, originalArgs, originalFunction, span) { span.transmit(); return err; }); + } else { + tracingUtil.handleUnexpectedReturnValue(resultPromise, exports.spanName, 'command'); + + span.d = Date.now() - span.ts; + span.transmit(); } return resultPromise; diff --git a/packages/core/src/tracing/instrumentation/databases/redis.js b/packages/core/src/tracing/instrumentation/databases/redis.js index 4f88ec8c85..21a14665ee 100644 --- a/packages/core/src/tracing/instrumentation/databases/redis.js +++ b/packages/core/src/tracing/instrumentation/databases/redis.js @@ -342,11 +342,10 @@ function instrumentCommand(original, command, address, cbStyle) { if (typeof userProvidedCallback !== 'function') { userProvidedCallback = null; modifiedArgs.push(callback); - return original.apply(origCtx, modifiedArgs); } else { modifiedArgs[modifiedArgs.length - 1] = callback; - return original.apply(origCtx, modifiedArgs); } + return original.apply(origCtx, modifiedArgs); } else { const promise = original.apply(origCtx, origArgs); if (typeof promise?.then === 'function') { @@ -361,6 +360,7 @@ function instrumentCommand(original, command, address, cbStyle) { }); } else { // UNKNOWN CASE + tracingUtil.handleUnexpectedReturnValue(promise, exports.spanName, `command "${command}"`); onResult(); } return promise; @@ -492,6 +492,9 @@ function instrumentMultiExec(origCtx, origArgs, original, address, isAtomic, cbS onResult(error); return error; }); + } else { + tracingUtil.handleUnexpectedReturnValue(promise, exports.spanName, 'multi/pipeline operation'); + onResult(); } return promise; From 3a24aa5f942c8fba90bcba95e79193c4251ec799 Mon Sep 17 00:00:00 2001 From: Abhilash Date: Tue, 24 Feb 2026 18:18:23 +0530 Subject: [PATCH 2/3] chore: cleanup --- packages/core/src/tracing/instrumentation/databases/ioredis.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/tracing/instrumentation/databases/ioredis.js b/packages/core/src/tracing/instrumentation/databases/ioredis.js index 9e2923297c..29e6542706 100644 --- a/packages/core/src/tracing/instrumentation/databases/ioredis.js +++ b/packages/core/src/tracing/instrumentation/databases/ioredis.js @@ -105,7 +105,7 @@ function instrumentSendCommand(original) { ); } else { tracingUtil.handleUnexpectedReturnValue(command.promise, exports.spanName, `command "${command.name}"`); - callback(null); + onResult(); } return original.apply(client, argsForOriginal); From d1beec0ba54089ad4e2d099a1d058cb5b1e61aba Mon Sep 17 00:00:00 2001 From: Abhilash Date: Tue, 24 Feb 2026 18:21:57 +0530 Subject: [PATCH 3/3] chore: cleanup mongodb --- .../instrumentation/databases/mongodb.js | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/packages/core/src/tracing/instrumentation/databases/mongodb.js b/packages/core/src/tracing/instrumentation/databases/mongodb.js index 9160de9d5d..fa473347e5 100644 --- a/packages/core/src/tracing/instrumentation/databases/mongodb.js +++ b/packages/core/src/tracing/instrumentation/databases/mongodb.js @@ -434,6 +434,11 @@ function stringifyWhenNecessary(obj) { return tracingUtil.shortenDatabaseStatement(JSON.stringify(obj)); } +function finishSpan(span) { + span.d = Date.now() - span.ts; + span.transmit(); +} + function createWrappedCallback(span, originalCallback) { return cls.ns.bind(function (error) { if (error) { @@ -441,9 +446,7 @@ function createWrappedCallback(span, originalCallback) { tracingUtil.setErrorDetails(span, error, 'mongo'); } - span.d = Date.now() - span.ts; - span.transmit(); - + finishSpan(span); return originalCallback.apply(this, arguments); }); } @@ -460,22 +463,18 @@ function handleCallbackOrPromise(ctx, originalArgs, originalFunction, span) { if (resultPromise && typeof resultPromise.then === 'function') { resultPromise .then(result => { - span.d = Date.now() - span.ts; - span.transmit(); + finishSpan(span); return result; }) .catch(err => { span.ec = 1; tracingUtil.setErrorDetails(span, err, 'mongo'); - span.d = Date.now() - span.ts; - span.transmit(); + finishSpan(span); return err; }); } else { tracingUtil.handleUnexpectedReturnValue(resultPromise, exports.spanName, 'command'); - - span.d = Date.now() - span.ts; - span.transmit(); + finishSpan(span); } return resultPromise;