Name and time every direct BeginTx transaction, and fail CI on an unnamed one

Split from #1606 (closed) (gap 1, the direct BeginTx sites). Parent epic: &24088. Blocked by #1719 (closed).

Decision (operator ruling 2026-10-25)

Q3: the 27 direct BeginTx sites are this separate issue, after #1719 (closed), so #1719 (closed) stays under 1000 lines. #1664 does not need this issue.

Problem

#1719 (closed) times RunInTx / runInSQLTx and the two direct background transactions (the S20 tombstone and the S22 drain fan-out). Another 27 sites open a transaction with a direct BeginTx and stay unmeasured. No test stops a new one.

Evidence

Research base: origin/main at e2c420456, 2026-10-25.

git grep -nE "(:=|=) .*BeginTx\(" origin/main -- '*.go' ':!*_test.go' finds 28 sites. One is internal/datastore/tx.go:76, which is the envelope itself. The rest include:

  • internal/datastore/: container_blob_linker.go:65, container_blob_unlinker.go:121, container_image.go:185, container_manifest_deleter.go:146, container_manifest_persister.go:236, container_virtual_upstreams.go:231, maven_virtual_upstreams.go:269, namespace_encryption_keys.go:1139 and :1175, namespaces.go:497, npm_leaf_deleter.go:433, npm_metadata_files.go:764, npm_package_management_deleter.go:100, npm_package_unpublish_deleter.go:122, npm_publish_committer.go:218, npm_remote_eviction.go:201, npm_version_deprecate_writer.go:73, npm_version_management_deleter.go:87, npm_version_unpublish_deleter.go:212, npm_virtual_upstreams.go:320, repositories.go:307, :1032 and :2142.
  • internal/storage/pg_session.go:1478. S06 already names this transaction as the statement upload_sessions_commit_tx. Decide in the MR whether it also gets a transaction name.
  • cmd/artifact-registry/bulk_enqueuer.go:110 (the bulk-delete acceptance transaction; the two raw statements inside it are #1724 (closed)).
  • internal/lifecycle/tombstone.go:116 and internal/accounting/drain_trigger.go:255 are covered by #1719 (closed).

Fix direction

  • Convert each site to runInSQLTx / RunInTx with a catalog name where its rollback discipline allows it. Otherwise, defer metrics.InstrumentTx(name) right after BeginTx, over the function's named err.
  • Add a source sweep, beside TestEveryStatementIsInstrumented (internal/datastore/queries_test.go:494), that rejects a BeginTx call that has no InstrumentTx in the same function. Run it over internal/ and cmd/.

Acceptance criteria

  • Every non-test BeginTx on origin/main books database_transaction_duration_seconds and database_transaction_outcomes_total under a catalog name.
  • The sweep test fails on a new unnamed BeginTx in internal/ or cmd/.
  • The sweep also checks the call shape: a direct defer metrics.InstrumentTx(name)(&<named error result>) in the function that calls BeginTx, and a metrics.CommitFailedError wrap of the Commit error in the same function (the transaction envelope excepted). Raised by arpitgogia in the !3546 (merged) review (deferred here by the operator).
  • The transaction label budget in internal/metrics/cardinality.go is updated and the cardinality audit passes.
  • Every comment that describes a rollback discipline you changed is updated (guardrail 16).

Size and fit

About 27 sites × 5-15 lines, plus about 27 catalog names, plus a sweep test of about 150 lines: 400-600 Go lines. This fits one smurfit unit.

Dependencies

  • Blocked by #1719 (closed) (InstrumentTx and the transaction catalog).
  • Overlaps #590 ("Teach runInSQLTx to take TxOptions and retire the bare BeginTx sites") and #589 (the composers' READ COMMITTED envelopes). If #590 lands first, convert sites through its options variant. If this lands first, #590 shrinks to the sites still on a bare BeginTx. Check both before starting.
Edited by Pawel Rozlach