dbtrace

package
v1.3.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 2, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

README

dbtrace

Per-request DB call counting + pool logging. Off by default.

Enable

Config (db_trace.*) Env Meaning
enabled RESOLVESPEC_DB_TRACE_ENABLED per-request logging
min_calls (5) ..._MIN_CALLS log if tx+pooled+raw >= N
min_duration (0) ..._MIN_DURATION or request took >= D
pool_log ..._POOL_LOG log pool stats on each dbmanager metrics publish

Wire: dbtrace.Configure(dbtrace.FromConfig(cfg.DBTrace)) and wrap handlers with dbtrace.Middleware (outside the auth middleware).

Log fields

  • tx transactions begun · tx_queries adapter queries inside RunInTransaction (share the tx connection)
  • pooled adapter queries outside a tx (each takes a pool connection)
  • raw direct *sql.DB calls, with kinds: auth.session, auth.activity, security.column, security.row, probe.pg_proc (lookup ModeAuto only), keystore.validate
  • Connections used ≈ tx + pooled + raw

Pool log

dbtrace pool <name>: open in_use idle max opened=+N waits=+N wait_time=+D — opened/waits are deltas since last publish.

Limits

  • tx attribution is per request, assumes sequential use of a request's context
  • auth.activity runs detached after the response: not in the request's log line
  • BeginTx/CommitTx (manual tx) not counted; only RunInTransaction
  • Raw counters cover the hot paths listed above only (not login/OAuth/passkey/TOTP)

Documentation

Overview

Package dbtrace counts database calls per request and logs the heavy ones. It is off until Configure enables it; the hot-path helpers are no-ops for contexts without a tracker.

Connection use per request ≈ tx + pooled + raw (each pooled/raw call takes its own pool connection; calls inside RunInTransaction share the tx's).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Configure

func Configure(o Options)

Configure sets the active options. Safe to call at any time.

func Enabled

func Enabled() bool

Enabled reports whether per-request tracing is on.

func Middleware

func Middleware(next http.Handler) http.Handler

Middleware tracks every request and logs those over the configured thresholds once the handler returns. Options are read per request, so Configure can toggle it at runtime. Raw calls made by detached goroutines after the response (e.g. session activity) are not included in the log line.

func PoolLogEnabled

func PoolLogEnabled() bool

PoolLogEnabled reports whether pool logging is on.

func Query

func Query(ctx context.Context)

Query counts one adapter query.

func Raw

func Raw(ctx context.Context, kind string)

Raw counts one direct *sql.DB call, labelled by what issued it.

func TxBegin

func TxBegin(ctx context.Context) func()

TxBegin counts a transaction and returns a func to call when it ends. Queries between the two are attributed to the transaction's connection.

Types

type Options

type Options struct {
	Enabled     bool
	MinCalls    int           // log when tx+pooled+raw >= MinCalls
	MinDuration time.Duration // or when the request took at least this (0 = off)
	PoolLog     bool          // log pool deltas on each dbmanager metrics publish
}

Options controls tracing.

func FromConfig

func FromConfig(c config.DBTraceConfig) Options

FromConfig maps the application config to Options.

type Tracker

type Tracker struct {
	// contains filtered or unexported fields
}

Tracker holds one request's counters.

func From

func From(ctx context.Context) *Tracker

From returns the Tracker on ctx, or nil.

func Start

func Start(ctx context.Context) (context.Context, *Tracker)

Start attaches a new Tracker to ctx.

func (*Tracker) Summary

func (t *Tracker) Summary() string

Summary renders the counters, e.g. "tx=1 tx_queries=3 pooled=2 raw=1 [auth.session=1]".

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL