Skip to content

fix(db): propagate transaction context into RunInTx callback#1368

Closed
valerijhegaj wants to merge 1 commit into
uptrace:masterfrom
valerijhegaj:1367
Closed

fix(db): propagate transaction context into RunInTx callback#1368
valerijhegaj wants to merge 1 commit into
uptrace:masterfrom
valerijhegaj:1367

Conversation

@valerijhegaj
Copy link
Copy Markdown
Contributor

fix: #1367

Summary

RunInTx was passing the original ctx into the callback instead of the transaction context (tx.ctx). This breaks context propagation for anything initialized during BeginTx.

Details

In Bun, OpenTelemetry instrumentation is typically added via query hooks. Those hooks rely on the context to carry the active span.
During BeginTx, Bun creates a wrapped context (tx.ctx) that includes the transaction span (BEGIN). However, the callback was invoked with the outer ctx, so all queries inside the transaction were executed outside of that span.
As a result:
queries inside the transaction are not children of the BEGIN span
trace structure is inconsistent (COMMIT/ROLLBACK are correct, queries are not)

Fix

Pass tx.ctx into the callback:
fn(tx.ctx, tx)
Applied to:

  • Conn.RunInTx
  • DB.RunInTx
  • Tx.RunInTx

Result

All queries inside a transaction now inherit the transaction span created at BEGIN, producing a correct and consistent trace tree.

@github-actions
Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had activity in the last 30 days. If there is no update within the next 7 days, this pr will be closed. Please feel free to give a status update now, ping for review, when it's ready. Thank you for your contributions!

@github-actions github-actions Bot added the stale label May 11, 2026
@github-actions github-actions Bot closed this May 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RunInTx sets incorrect otel references CHILD_OF for queries inside transaction

1 participant