diff --git a/packages/core/src/tracing/instrumentation/databases/ioredis.js b/packages/core/src/tracing/instrumentation/databases/ioredis.js index e88a1c69e3..29e6542706 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}"`); + onResult(); + } 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..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); }); } @@ -457,20 +460,21 @@ 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; - 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'); + finishSpan(span); } 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;