2021-10-27 18:49:33 +03:00
|
|
|
package wrapper // import "go.unistack.org/micro-wrapper-sql/v3"
|
2021-03-15 15:49:54 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"time"
|
|
|
|
|
2021-10-27 18:49:33 +03:00
|
|
|
"go.unistack.org/micro/v3/meter"
|
2021-03-15 15:49:54 +03:00
|
|
|
)
|
|
|
|
|
2021-10-27 18:49:33 +03:00
|
|
|
type Statser interface {
|
|
|
|
Stats() sql.DBStats
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewStatsMeter(ctx context.Context, db Statser, opts ...Option) {
|
|
|
|
options := NewOptions(opts...)
|
|
|
|
|
|
|
|
m := options.Meter.Clone(
|
|
|
|
meter.LabelPrefix(options.MeterLabelPrefix),
|
|
|
|
meter.MetricPrefix(options.MeterMetricPrefix),
|
|
|
|
meter.Labels(
|
|
|
|
labelHost, options.DatabaseHost,
|
|
|
|
labelDatabase, options.DatabaseName,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
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()
|
|
|
|
m.Counter(MaxOpenConnections).Set(uint64(stats.MaxOpenConnections))
|
|
|
|
m.Counter(OpenConnections).Set(uint64(stats.OpenConnections))
|
|
|
|
m.Counter(InuseConnections).Set(uint64(stats.InUse))
|
|
|
|
m.Counter(IdleConnections).Set(uint64(stats.Idle))
|
|
|
|
m.Counter(WaitConnections).Set(uint64(stats.WaitCount))
|
|
|
|
m.FloatCounter(BlockedSeconds).Set(stats.WaitDuration.Seconds())
|
|
|
|
m.Counter(MaxIdleClosed).Set(uint64(stats.MaxIdleClosed))
|
|
|
|
m.Counter(MaxLifetimeClosed).Set(uint64(stats.MaxLifetimeClosed))
|
2021-03-15 15:49:54 +03:00
|
|
|
}
|
|
|
|
}
|
2021-10-27 18:49:33 +03:00
|
|
|
}()
|
2021-03-15 15:49:54 +03:00
|
|
|
}
|