Skip to content

feat: add pool cancellation and transaction lifecycle metrics - #46

Open
torchello wants to merge 1 commit into
chmodas:trunkfrom
torchello:codex/upstream-pool-lifecycle
Open

torchello wants to merge 1 commit into
chmodas:trunkfrom
torchello:codex/upstream-pool-lifecycle

Conversation

@torchello

Copy link
Copy Markdown
Contributor

Pool wait metrics currently lose the time spent by callers that cancel acquisition, while connection use time cannot distinguish BEGIN from the lifetime and termination of a transaction. This adds separate client-side lifecycle measurements and optional workload attribution so applications can identify which work holds or waits for connections.

This PR is based directly on current trunk (SQLx 0.9 / OpenTelemetry 0.33). It is independent of #41 and adds no dependencies or dependency-version changes.

Measurements

  • sqlx_otel.connection.abandoned_wait.duration: records elapsed wait exactly once when a polled acquisition is dropped. Never-polled futures emit nothing; completed waits and pool timeouts retain their existing meanings.
  • sqlx_otel.transaction.begin.duration: measures BEGIN after acquisition, with ok, error, and abandoned outcomes.
  • sqlx_otel.transaction.duration: measures from successful BEGIN to the terminal client observation, with commit/rollback success, error, abandonment, and dropped outcomes. Its seconds-based histogram extends to one hour.

Outcomes deliberately describe what the client observed. An abandoned commit does not establish whether the server committed. Dropping a transaction queues rollback; it does not confirm completion, and can be an intentional guard-release path rather than an application error. No extra SQL, await, or SQLx ownership/Drop behavior is introduced.

Optional workload attribution

PoolAttribution::new("myapp.job.kind", "nightly_rollup").scope(future) adds one static key/value to wait, use, timeout, abandoned-wait, and transaction measurements. It is captured when acquisition first runs and stays with the lease through completion, even outside the original scope.

Attributions that reuse a connection-identity or transaction-outcome key are ignored, so they cannot overwrite canonical labels. The scope is restored after each poll, including panic unwinding. Spawned tasks require explicit PoolAttribution::inherit; pool-wide gauges/pending counts and query metrics remain untagged. Enabling a scope splits existing series, so consumers must aggregate across the added dimension to retain pool-wide totals.

Validation

  • Clippy (--all-targets --all-features -- -D warnings), formatting, and strict rustdoc passed.
  • Instrumented all-features suite: 604 tests passed, including SQLite, PostgreSQL, and MySQL with fresh disposable containers.
  • Async-std feature run: 37 lifecycle/pool tests passed.
  • Doctests: 14 passed; one pre-existing ignored example.
  • Combined LLVM coverage: 99.64% lines, 99.41% regions.

Tests cover cancellation versus timeouts, never-polled futures, interleaved/nested attribution scopes, spawned work, lease transfer, and real PostgreSQL boundary errors/cancellation. A response-gated commit test verifies that the server can commit while the client records commit_abandoned.

README, rustdoc, and changelog describe lifecycle boundaries, cardinality/propagation rules, and outcome limitations.

@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 99.60474% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 99.81%. Comparing base (e6c3de5) to head (dee0d68).

Files with missing lines Patch % Lines
src/attribution.rs 99.41% 1 Missing ⚠️
src/transaction_metrics.rs 99.64% 1 Missing ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Additional details and impacted files
@@            Coverage Diff             @@
##            trunk      #46      +/-   ##
==========================================
- Coverage   99.85%   99.81%   -0.04%     
==========================================
  Files          15       17       +2     
  Lines        2760     3253     +493     
==========================================
+ Hits         2756     3247     +491     
- Misses          4        6       +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

2 participants