Add tracing support for PreparedStatementIO#2361
Merged
jatcwang merged 14 commits intotypelevel:mainfrom Jan 31, 2026
Merged
Conversation
iRevive
commented
Jan 16, 2026
iRevive
commented
Jan 16, 2026
iRevive
commented
Jan 16, 2026
a4dfbe5 to
182cb9f
Compare
iRevive
commented
Jan 22, 2026
| ) | ||
| ) | ||
|
|
||
| lazy val otel4s = project |
Contributor
Author
There was a problem hiding this comment.
This module is used to test the tracing integration. Because otel4s is still pre-1.0, we should probably avoid publishing this module yet.
Contributor
Author
There was a problem hiding this comment.
I disabled publishing for this module.
iRevive
commented
Jan 22, 2026
PreparedStatementIOPreparedStatementIO
Contributor
Author
|
@jatcwang could you please take a look? |
Collaborator
|
Thanks for the lovely PR :) |
Contributor
Author
|
Perfect, thank you! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hey everyone. I'm trying to implement a flexible (well, to some degree) instrumentation logic for doobie, to make it compatible with otel4s and be mostly compliant with OpenTelemetry DB Semantic Conventions.
I'm aware of otel4s-doobie and natchez-doobie. Yet, I wonder whether we can slightly extend the ADT to make it easier to write instrumentation.
OTel semantic conventions for DB operations require certain attributes. I expect to make them opt-in, i.e., users will need to explicitly enable features, such as query or query params capture.
We can break down mandatory (and semi-mandatory) attributes into a few categories.
Transactor initialization
db.system.name- mysql, postgresql, etcdb.namespace- the name of the databaseserver.address- the server addressserver.port- the server port (can be empty if we use a default)Prepared statement execution
db.operation.name- it can be SELECT / UPDATE / DELETE, but I would choose something easier, such as prepared statement op, e.g.executeBatch,executeUpdate,executeQuerydb.query.text- query textdb.query.parameter.{idx}- query paramserror.type- FQN of the error, e.g.java.sql.SQLExceptiondb.response.status_code- the db error code,java.sql.SQLException#getErrorCodeQuery metadata
db.query.summary- it can be either auto-derived or provided by a userThis one is tricky. I was thinking of using a
labelto attach structured information to the query.Where
queryWithAttributesis an extension API that will serializeAttributesas JSON, and then we can deserialize a label into the map of key-values inside thetraceoverride.With this approach, users can attach any identifiable information to the span: source info (file name, enclosing, lines), MDC, etc.
The implementation details
The instrumentation can be described as a natural transformation
F ~> F. It is suitable for both tracing and metrics.Yet, we need some additional meta information to reason about the operation.
In this sketch, I introduce a new trait, such as
TracingEvent. Each ADT (currently, there is only one) describes a specific instrumentation place.Then, within the
trace, the implementor can decide which calls to instrument and choose an appropriate naming strategy.Usage example
Create a transactor:
Run queries:
As you can see, all attributes are captured correctly:
The outcome
Please let me know what do you think and how feasible this idea is.