Skip to content
Merged
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
5 changes: 5 additions & 0 deletions packages/core/src/tracing/instrumentation/databases/pg.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,11 @@ function instrumentedQuery(ctx, originalQuery, argsForOriginalQuery) {
return originalCallback.apply(this, arguments);
};
argsForOriginalQuery[callbackIndex] = cls.ns.bind(wrappedCallback);
return originalQuery.apply(ctx, argsForOriginalQuery);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to add this change due to an issue with tracing error cases after introducing the promise.then undefined handling.

When a callback was executed and an error occurred, finishSpan was being called twice — once from the callback wrapper and once from the promise handlers. In this specific error scenario, both paths were getting executed. (We only encountered this now because the newly added non-promise case exposed this scenario.)

To prevent this duplication, if callbackIndex >= 0, we now return early and skip the promise checks entirely.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think this is even cleaner - no need to check again if there is a promise response.

}

const promise = originalQuery.apply(ctx, argsForOriginalQuery);

if (promise && typeof promise.then === 'function') {
promise
.then(value => {
Expand All @@ -95,6 +97,9 @@ function instrumentedQuery(ctx, originalQuery, argsForOriginalQuery) {
finishSpan(error, span);
return error;
});
} else {
tracingUtil.handleUnexpectedReturnValue(promise, exports.spanName, 'query');
finishSpan(null, span);
}
return promise;
});
Expand Down
18 changes: 18 additions & 0 deletions packages/core/src/tracing/tracingUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,3 +411,21 @@ exports.setErrorDetails = function setErrorDetails(span, error, technology) {
logger.error('Failed to set error details on span:', err);
}
};

/**
* Handles and logs a trace message when the instrumented function returns an unexpected value(not a promise)
*
* @param {*} returnValue - The return value from the instrumented function
* @param {string} spanName - The name of the span (e.g., 'redis', 'postgres', 'mysql')
* @param {string} operationContext - Additional context about the operation (e.g., 'query', 'command')
* @returns {boolean} - Returns true if the return value was unexpected (not a promise), false otherwise
*/
exports.handleUnexpectedReturnValue = function handleUnexpectedReturnValue(returnValue, spanName, operationContext) {
logger.trace(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed from debug to trace, this will not get logged unless the trace level is "TRACE"

`${spanName} instrumentation: Unexpected return value from ${operationContext}. ` +
`Expected a promise but got: ${typeof returnValue}. ` +
'This indicates an unsupported library behavior.'
);

return true;
};