Files
mtusi2026lab7/internal/telemetry/db.go
T
Vladislav Kunetskiy 1b6e96ccc0
Application / Go invariants and static checks (pull_request) Canceled after 0s
Application / Lab7 verified TLS and retained features (pull_request) Canceled after 0s
Skeleton contracts / contracts (pull_request) Canceled after 0s
lab7: безопасность, mTLS, OpenBao и PKI lifecycle
2026-11-10 14:39:57 +03:00

41 lines
1.3 KiB
Go

package telemetry
import (
"context"
"github.com/exaring/otelpgx"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/metric"
)
// NewPGXTracer returns a pgx tracer that records database spans without SQL
// text or connection details (those can contain identifiers and DSNs).
func NewPGXTracer() pgx.QueryTracer {
return otelpgx.NewTracer(
otelpgx.WithTracerProvider(otel.GetTracerProvider()),
otelpgx.WithDisableSQLStatementInAttributes(),
otelpgx.WithDisableConnectionDetailsInAttributes(),
)
}
// RegisterDBPool exposes pgxpool gauges. pgxpool has no current "waiting"
// counter; EmptyAcquireCount/WaitTime are the observable contention signal.
func RegisterDBPool(stats func() *pgxpool.Stat) error {
initMetrics()
_, err := otel.Meter("messenger").RegisterCallback(func(_ context.Context, o metric.Observer) error {
s := stats()
if s == nil {
return nil
}
o.ObserveInt64(dbUsed, int64(s.AcquiredConns()))
o.ObserveInt64(dbIdle, int64(s.IdleConns()))
o.ObserveInt64(dbMax, int64(s.MaxConns()))
o.ObserveInt64(dbEmptyAcquire, s.EmptyAcquireCount())
o.ObserveFloat64(dbEmptyWait, s.EmptyAcquireWaitTime().Seconds())
return nil
}, dbUsed, dbIdle, dbMax, dbEmptyAcquire, dbEmptyWait)
return err
}