postgres

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package postgres provides pgx pool construction, query tracing, and per-request DB stats.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewPool

func NewPool(ctx context.Context, databaseURL string) (*pgxpool.Pool, error)

NewPool creates a pgxpool.Pool with OTel tracing and structured query logging.

func NewReqDBStatsContext

func NewReqDBStatsContext(ctx context.Context) context.Context

NewReqDBStatsContext returns a new context with an empty ReqDBStats attached.

func SetQueryObserver

func SetQueryObserver(o QueryObserver)

SetQueryObserver sets the global query observer (typically a Prometheus histogram).

func WithHTTPMethod

func WithHTTPMethod(ctx context.Context, method string) context.Context

WithHTTPMethod stores the HTTP method in the context for query metrics labelling.

Types

type QueryObserver

type QueryObserver interface {
	ObserveQuery(ctx context.Context, method, route, outcome string, dur time.Duration)
}

QueryObserver receives per-query metrics (wired by main for Prometheus).

type QueryObserverFunc

type QueryObserverFunc func(ctx context.Context, method, route, outcome string, dur time.Duration)

QueryObserverFunc adapts a plain function to QueryObserver.

func (QueryObserverFunc) ObserveQuery

func (f QueryObserverFunc) ObserveQuery(ctx context.Context, method, route, outcome string, dur time.Duration)

ObserveQuery implements QueryObserver.

type ReqDBStats

type ReqDBStats struct {
	QueryCount    int
	TotalDuration time.Duration
	ErrorCount    int
	// contains filtered or unexported fields
}

ReqDBStats accumulates per-request database query statistics.

func ReqDBStatsFromContext

func ReqDBStatsFromContext(ctx context.Context) (*ReqDBStats, bool)

ReqDBStatsFromContext extracts the ReqDBStats from the context, if present.

func (*ReqDBStats) AddQuery

func (s *ReqDBStats) AddQuery(dur time.Duration, err error)

AddQuery records a single query execution.

Jump to

Keyboard shortcuts

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