Files
mtusi2026lab7/internal/telemetry/db.go
T
Vladislav Kunetskiy 3d216fbd50 База lab7: контракт 1.7.0 и реализация lab1–lab6
Перенесены cmd/internal/config, compose.yaml, Dockerfile, скрипты, student-tests и evidence из mtusi-messenger (master, lab6). Skeleton lab7 (openapi/infrastructure 1.7.0, trust-matrix.md, security/THREAT_MODEL.md, infra/openbao, tests/security.py) сохранён; make test-security добавлен в Makefile.
2026-11-09 13:03:49 +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
}