open-telemetry / opentelemetry-java-instrumentation

OpenTelemetry auto-instrumentation and instrumentation libraries for Java
https://opentelemetry.io
Apache License 2.0
1.98k stars 864 forks source link

Propagate trace info to the database from SQL query text #11940

Open igor-suhorukov opened 3 months ago

igor-suhorukov commented 3 months ago

Is your feature request related to a problem? Please describe.

When tracing an application and troubleshooting application issues, it is often useful to trace a SQL query from the application in the database logs. This allows you to move from a query to its plan and understand why the DBMS acts in this way in a particular case. Some cloud solutions allow such tracing to the database level, distributing the tracing context as a comment in the SQL query. It would be useful to implement similar functionality in Opentelemetry java agent JDBC

Describe the solution you'd like

When running my application with the opentelemetry agent and the agent option to tracing in the SQL text enabled (some new experimental option), I expect in the logs of my DBMS something similar to:

SELECT * FROM nodes /*service.name='MyJavaApp',traceparent='00-00000000000000007fffffffffffffff-000000025cb0161a-00'*

There is already an implementation of such functionality in the project

Describe alternatives you've considered

Save session information from JDBC Connection in tracing object io.opentelemetry.instrumentation.jdbc.internal.DbRequest

Additional context

No response

trask commented 1 month ago

We'd welcome a contribution for this.

SQLCommenter was donated to OpenTelemetry but integration into the Java instrumentation project never happened: https://github.com/open-telemetry/opentelemetry-java-contrib/issues/205#issuecomment-1029306399

jackgopack4 commented 16 hours ago

This would be very helpful, at a bare minimum if opentelemetry-sqlcommenter could be integrated as a package in opentelemetry-java-contrib like mentioned above.