Files
dbx/options.go
Aleksey Shakhmatov 2c9af28548
Some checks failed
CI / test (push) Failing after 13s
Add production features: slog adapter, scan helpers, slow query logging, pool stats, tracer passthrough, test tx isolation
- slog.go: SlogLogger adapts *slog.Logger to dbx.Logger interface
- scan.go: Collect[T] and CollectOne[T] generic helpers using pgx.RowToStructByName
- cluster.go: slow query logging via Config.SlowQueryThreshold (Warn level in queryEnd)
- stats.go: PoolStats with Cluster.Stats() aggregating pool stats across all nodes
- config.go/node.go: NodeConfig.Tracer passthrough for pgx.QueryTracer (OpenTelemetry)
- options.go: WithSlowQueryThreshold and WithTracer functional options
- dbxtest/tx.go: RunInTx runs callback in always-rolled-back transaction for test isolation

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-23 00:19:26 +03:00

65 lines
1.4 KiB
Go

package dbx
import (
"time"
"github.com/jackc/pgx/v5"
)
// Option is a functional option for NewCluster.
type Option func(*Config)
// WithLogger sets the logger for the cluster.
func WithLogger(l Logger) Option {
return func(c *Config) {
c.Logger = l
}
}
// WithMetrics sets the metrics hook for the cluster.
func WithMetrics(m *MetricsHook) Option {
return func(c *Config) {
c.Metrics = m
}
}
// WithRetry overrides the retry configuration.
func WithRetry(r RetryConfig) Option {
return func(c *Config) {
c.Retry = r
}
}
// WithHealthCheck overrides the health check configuration.
func WithHealthCheck(h HealthCheckConfig) Option {
return func(c *Config) {
c.HealthCheck = h
}
}
// WithSlowQueryThreshold sets the threshold for slow query warnings.
// Queries taking longer than d will be logged at Warn level.
func WithSlowQueryThreshold(d time.Duration) Option {
return func(c *Config) {
c.SlowQueryThreshold = d
}
}
// WithTracer sets the pgx.QueryTracer on master and all replica configs.
// This enables OpenTelemetry integration via libraries like otelpgx.
func WithTracer(t pgx.QueryTracer) Option {
return func(c *Config) {
c.Master.Tracer = t
for i := range c.Replicas {
c.Replicas[i].Tracer = t
}
}
}
// ApplyOptions applies functional options to a Config.
func ApplyOptions(cfg *Config, opts ...Option) {
for _, o := range opts {
o(cfg)
}
}