dd-trace-go icon indicating copy to clipboard operation
dd-trace-go copied to clipboard

Full DBM propagation for pgx

Open ewhauser opened this issue 1 year ago • 14 comments

pgx needs support for DBM trace propagation (https://docs.datadoghq.com/database_monitoring/connect_dbm_and_apm/?tab=go) like lib/pq

ewhauser avatar May 25 '24 15:05 ewhauser

👋 Thanks for submitting this and sorry for the delay. I've pinged the internal team owning this, hopefully they'll get to it ASAP.

felixge avatar Jun 06 '24 05:06 felixge

Hello @ewhauser, thanks for opening an issue!

What instrumentation are you referring to? We currently don't have any instrumentation named dbx.

If your issue is about supporting a new instrumentation, please feel free to open a new issue with more details about your request, including a link to the github repository of the library.

We accept external PRs from external contributors, but if you want us to tackle the work feel free to reach out Datadog Support so we can best prioritize that work!

rarguelloF avatar Jun 06 '24 14:06 rarguelloF

Apologies, that was I typo. I updated it to be pgx.

ewhauser avatar Jun 06 '24 14:06 ewhauser

Hi @ewhauser, which DBMS types would you want this for? (Postgres, MySQL, SQL Server, Oracle)

caseycull avatar Jun 06 '24 17:06 caseycull

Postgres

ewhauser avatar Jun 06 '24 18:06 ewhauser

Hey, correlating queries to traces and seeing calling services in the advanced DB monitoring in Datadog could be really beneficial to us. Is there a plan to support this? Is there a custom way to add a comment to the query so it will be picked up? Thanks

doron-cohen avatar Jun 28 '24 09:06 doron-cohen

any update about it, it is a great feature for us

fcarrero avatar Aug 18 '24 20:08 fcarrero

Hi @caseycull and @rarguelloF, thanks for getting the ball rolling on this! Any updates you can share? Would also find this really useful. We use Postgres

callmemc avatar Sep 04 '24 15:09 callmemc

Any update regarding this?

taltcher avatar Jan 31 '25 20:01 taltcher

any updates on this one?

leszczynskimikolaj avatar Feb 10 '25 16:02 leszczynskimikolaj

👋 Sorry for the late reply, and thanks for showing interest in this feature!

We would be interested on implementing this, however there are some limitations from pgx.

Our integration is based on the pgx.QueryTracer interfaces, which don't allow to intercept/modify the query. This is necessary for DBM to work, which uses sqlcommenter to inject comments in queries and correlate those with traces. There's an old issue requesting support for this, but it is closed as not planned.

A workaround that you could use for now, if you are not using any postgres specific functionality, is to use the database/sql interface of pgx (see https://github.com/jackc/pgx/wiki/Getting-started-with-pgx-through-database-sql) and switch to use the dd-trace-go database/sql integration.

Also, I encourage everyone interested in this feature to:

  • Comment on the above issue (or create a new one) in pgx showing your interest in this feature (sqlcommenter support). This would probably require to disable prepared statements when the feature is enabled, which depends on whether @jackc would be interested or not.
  • Create a feature request in Datadog using this form (no login required) - this will help us with prioritization to implement this feature, whether we go with a "pgx native" approach or a custom one (an alternative integration wrapping pgx types).

rarguelloF avatar Feb 14 '25 12:02 rarguelloF

Hey @rarguelloF thanks for the answer. I am pretty sure we have some way to use sqlcommenter in some hook of our ORM library. Is there an API to dd-trace-go to allow commenting the query with sqlcommenter from the current span? That would actually solve that for us.

doron-cohen avatar Feb 14 '25 15:02 doron-cohen

@doron-cohen you would need to comment the query itself (which in this case created/managed by pgx) adding information from the span. The limitation of our pgx integration is that is based on the pgx.QueryTracer hooks, which allows us to create and close spans before / after the query is executed, but does not allow to modify them.

What we would need concretely is a way to modify the query after the span is created, but before the query is sent to the server. You can check this code from the database/sql integration to see an example.

rarguelloF avatar Feb 18 '25 15:02 rarguelloF

@rarguelloF pgx supports adding QueryRewriter interface which gets passed as an arg. RewriteQuery is called after TraceQueryEnd as seen here so you could create a wrapper for pgx.Conn that inserts a QueryRewriter that injects trace information as the first arg to method calls to the underlying pgx.Conn

ennyjfrick avatar May 19 '25 18:05 ennyjfrick

@rarguelloF @ennyjfrick any update about it ?

fcarrero avatar Jun 25 '25 15:06 fcarrero