Skip to content

feat(node): Add postgresjs instrumentation #16665

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jun 26, 2025

Conversation

onurtemizkan
Copy link
Collaborator

@onurtemizkan onurtemizkan commented Jun 20, 2025

Resolves: #15621
Adds instrumentation for https://github.com/porsager/postgres

Sampled event: (Emitted from the integration tests added here): Link

This implementation patches connection and query classes to create database transactions:

  • From connection, we pick up the database name, url and port to use in the db query spans
  • For each query instance, we create a db span
  • This implementation does not create a separate span for each cursor used

Initially, I implemented a way to capture db.operation (as command is available when the query resolves) but it seems the ingestion extracts the operation anyway, so I removed it.

Also added sanitization/normalization for raw query, which we use as the span description, also seems to be normalized by the ingestion engine. We can remove it too if it's not worth having, as it creates a possibly-unnecessary performance overhead on the SDK side.

Copy link
Contributor

github-actions bot commented Jun 20, 2025

size-limit report 📦

Path Size % Change Change
@sentry/browser 23.99 kB - -
@sentry/browser - with treeshaking flags 23.76 kB - -
@sentry/browser (incl. Tracing) 38.84 kB - -
@sentry/browser (incl. Tracing, Replay) 76.98 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 70.07 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 81.74 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 93.83 kB - -
@sentry/browser (incl. Feedback) 40.75 kB - -
@sentry/browser (incl. sendFeedback) 28.7 kB - -
@sentry/browser (incl. FeedbackAsync) 33.59 kB - -
@sentry/react 25.76 kB - -
@sentry/react (incl. Tracing) 40.84 kB - -
@sentry/vue 28.36 kB - -
@sentry/vue (incl. Tracing) 40.7 kB - -
@sentry/svelte 24.01 kB - -
CDN Bundle 25.5 kB - -
CDN Bundle (incl. Tracing) 38.92 kB - -
CDN Bundle (incl. Tracing, Replay) 74.83 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 80.29 kB - -
CDN Bundle - uncompressed 74.5 kB - -
CDN Bundle (incl. Tracing) - uncompressed 115.42 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 229.47 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 242.3 kB - -
@sentry/nextjs (client) 42.47 kB - -
@sentry/sveltekit (client) 39.33 kB - -
@sentry/node 154.09 kB +2.11% +3.18 kB 🔺
@sentry/node - without tracing 98.63 kB - -
@sentry/aws-serverless 124.4 kB -0.01% -2 B 🔽

View base workflow run

@onurtemizkan onurtemizkan force-pushed the onur/postgres.js-instrumentation branch 2 times, most recently from 4e076b1 to 5cae445 Compare June 21, 2025 12:01
@onurtemizkan onurtemizkan force-pushed the onur/postgres.js-instrumentation branch from bcf73bf to ff9248e Compare June 23, 2025 22:42
@onurtemizkan onurtemizkan marked this pull request as ready for review June 23, 2025 23:14
@onurtemizkan onurtemizkan requested review from Lms24 and AbhiPrasad June 23, 2025 23:14
Copy link
Member

@AbhiPrasad AbhiPrasad left a comment

Choose a reason for hiding this comment

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

Let's make sure we collect as much as possible as per https://opentelemetry.io/docs/specs/semconv/database/database-spans/

Similar to the pg instrumentation, let's implement the requireParentSpan option and set it as true.

We should also implement requestHook so we can attach span origin.

`;

await sql`
UPDATE "User" SET "name" = 'Foo' WHERE "email" = 'bar@baz.com';

Choose a reason for hiding this comment

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

We'd perhaps want to leave the email in a constant since we are repeating it over and over.
it doesn't really matter since it's a dummy email, what I'm thinking is: what if at some point we want to run the test on a specific email? (if we ever want to do that)

It would be easier to just change the value of the constant over changing every single instance of it.

Just my thought 😁

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Makes sense, updated. Thanks 👍

@onurtemizkan
Copy link
Collaborator Author

@AbhiPrasad

  • Added requireParentSpan and requestHook for when it's needed. edcc016

  • Added Sentry origin inline instead of using the hook. edcc016

  • (re)Added a few more OTEL attributes 9798064:

    • ATTR_DB_OPERATION_NAME - this one is set inside resolve as it's only available there.
    • ATTR_DB_QUERY_TEXT - This is the sanitized query we use as the span description.
    • ATTR_DB_RESPONSE_STATUS_CODE - Error code inside reject, set when it's available.
    • ATTR_ERROR_TYPE: Set inside reject
  • Table / Relation name is not available anywhere without a need for parsing, so I could not set ATTR_DB_COLLECTION_NAME It's extracted by the ingestion engine as far as I can see.

  • ATTR_DB_QUERY_SUMMARY needs parsing as well, so I left it out. But it's also parsed by the ingestion engine and set as normalized_description

Copy link
Member

@AbhiPrasad AbhiPrasad left a comment

Choose a reason for hiding this comment

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

Nice!

@AbhiPrasad
Copy link
Member

ah @onurtemizkan we have to export this from all relevant sdks

@AbhiPrasad AbhiPrasad merged commit 510ba3e into develop Jun 26, 2025
163 checks passed
@AbhiPrasad AbhiPrasad deleted the onur/postgres.js-instrumentation branch June 26, 2025 14:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add instrumentation for postgres package
3 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy