Skip to content

Conversation

@aryamohanan
Copy link
Contributor

Which problem is this PR solving?

closes #3258

Fixes crash in the pg instrumentation when using SQL template literal library (sql-template-strings).

TypeError: Cannot read properties of undefined (reading 'indexOf') at parseNormalizedOperationName

The issue was introduced by #3196 , where spreading query objects caused SQL template objects to lose their text getter, leading to parseNormalizedOperationName() receiving undefined and throwing the error.

Short description of the changes

  • Preserve the original SQL template object when building queryConfig instead of spreading it
  • Add a check to assign values from the second argument only if missing
  • Add a test mocking SQL template objects to verify that client.query(SQL\...`)` works
    correctly and spans are recorded

Comment on lines 333 to 350
// TODO: remove the `as ...` casts below when the TS version is upgraded.
// Newer TS versions will use the result of firstArgIsQueryObjectWithText
// to properly narrow arg0, but TS 4.3.5 does not.
const queryConfig = firstArgIsString
? {
text: arg0 as string,
values: Array.isArray(args[1]) ? args[1] : undefined,
}
: firstArgIsQueryObjectWithText
? {
...(arg0 as any),
values:
(arg0 as any).values ??
(Array.isArray(args[1]) ? args[1] : undefined),
? (() => {
const query = arg0 as any;
// If the query object has no values yet, use the second argument as values
if (query.values === undefined && Array.isArray(args[1])) {
query.values = args[1];
}
: undefined;
return query;
})()
: undefined;

Choose a reason for hiding this comment

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

I already did not like this ternary soup, so if you don't mind I'd like to use this opportunity to refactor it into something more readable rather than adding an IIFE into the mix 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Absolutely, a clean refactor sounds like the right move 👍

Comment on lines 376 to 390

Choose a reason for hiding this comment

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

Another thing, there is another spread happening here. Could you add a test checking SQL template literals with added comments too ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Definitely, I’ll expand the test to cover SQL template literals with added comments too. Appreciate you catching the spread.

Copy link
Member

@raphael-theriault-swi raphael-theriault-swi left a comment

Choose a reason for hiding this comment

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

Thanks for working on this !

@raphael-theriault-swi raphael-theriault-swi merged commit 2e5a9d6 into open-telemetry:main Dec 2, 2025
23 checks passed
@otelbot
Copy link
Contributor

otelbot bot commented Dec 2, 2025

Thank you for your contribution @aryamohanan! 🎉 We would like to hear from you about your experience contributing to OpenTelemetry by taking a few minutes to fill out this survey.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Postgres instrumentation crashes when sql-template-strings is used

3 participants