Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions packages/core/src/tracing/instrumentation/databases/ioredis.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
20 changes: 12 additions & 8 deletions packages/core/src/tracing/instrumentation/databases/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,16 +434,19 @@ 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) {
span.ec = 1;
tracingUtil.setErrorDetails(span, error, 'mongo');
}

span.d = Date.now() - span.ts;
span.transmit();

finishSpan(span);
return originalCallback.apply(this, arguments);
});
}
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand All @@ -361,6 +360,7 @@ function instrumentCommand(original, command, address, cbStyle) {
});
} else {
// UNKNOWN CASE
tracingUtil.handleUnexpectedReturnValue(promise, exports.spanName, `command "${command}"`);
onResult();
}
return promise;
Expand Down Expand Up @@ -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;
Expand Down