forked from mtusi2026/mtusi2026lab7
41 lines
1.3 KiB
Go
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
|
|
}
|