micro-wrapper-sql/stats.go
Vasiliy Tolstov dbce4a1d9e
Some checks failed
dependabot-automerge / automerge (pull_request) Has been skipped
autoapprove / autoapprove (pull_request) Failing after 4s
automerge / automerge (pull_request) Failing after 4s
codeql / analyze (go) (pull_request) Failing after 6s
prbuild / test (pull_request) Failing after 5s
prbuild / lint (pull_request) Failing after 6s
use generated interface
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-06-10 13:09:25 +03:00

42 lines
1.2 KiB
Go

package wrapper // import "go.unistack.org/micro-wrapper-sql-1/v3"
import (
"context"
"database/sql"
"time"
)
type Statser interface {
Stats() sql.DBStats
}
func NewStatsMeter(ctx context.Context, db Statser, opts ...Option) {
options := NewOptions(opts...)
go func() {
ticker := time.NewTicker(options.MeterStatsInterval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
if db == nil {
return
}
stats := db.Stats()
options.Meter.Counter(MaxOpenConnections).Set(uint64(stats.MaxOpenConnections))
options.Meter.Counter(OpenConnections).Set(uint64(stats.OpenConnections))
options.Meter.Counter(InuseConnections).Set(uint64(stats.InUse))
options.Meter.Counter(IdleConnections).Set(uint64(stats.Idle))
options.Meter.Counter(WaitConnections).Set(uint64(stats.WaitCount))
options.Meter.FloatCounter(BlockedSeconds).Set(stats.WaitDuration.Seconds())
options.Meter.Counter(MaxIdleClosed).Set(uint64(stats.MaxIdleClosed))
options.Meter.Counter(MaxIdletimeClosed).Set(uint64(stats.MaxIdleTimeClosed))
options.Meter.Counter(MaxLifetimeClosed).Set(uint64(stats.MaxLifetimeClosed))
}
}
}()
}