diff --git a/packages/core/src/tracing/instrumentation/databases/pg.js b/packages/core/src/tracing/instrumentation/databases/pg.js index eed210ad70..839724eb02 100644 --- a/packages/core/src/tracing/instrumentation/databases/pg.js +++ b/packages/core/src/tracing/instrumentation/databases/pg.js @@ -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); } const promise = originalQuery.apply(ctx, argsForOriginalQuery); + if (promise && typeof promise.then === 'function') { promise .then(value => { @@ -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; }); diff --git a/packages/core/src/tracing/tracingUtil.js b/packages/core/src/tracing/tracingUtil.js index 17cd2684c3..e8d3efa461 100644 --- a/packages/core/src/tracing/tracingUtil.js +++ b/packages/core/src/tracing/tracingUtil.js @@ -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( + `${spanName} instrumentation: Unexpected return value from ${operationContext}. ` + + `Expected a promise but got: ${typeof returnValue}. ` + + 'This indicates an unsupported library behavior.' + ); + + return true; +};