statping/types/metrics/database.go

41 lines
1.3 KiB
Go
Raw Normal View History

2020-06-16 09:42:21 +00:00
package metrics
2020-06-16 11:02:14 +00:00
import (
"database/sql"
"github.com/prometheus/client_golang/prometheus"
)
2020-06-16 09:42:21 +00:00
var (
// service is online if set to 1, offline if 0
2020-06-16 11:02:14 +00:00
databaseStats = prometheus.NewGaugeVec(
2020-06-16 09:42:21 +00:00
prometheus.GaugeOpts{
Namespace: "statping",
2020-06-16 11:02:14 +00:00
Name: "database",
2020-06-16 09:42:21 +00:00
Help: "If service is online",
2020-06-16 11:02:14 +00:00
}, []string{"metric"},
2020-06-16 09:42:21 +00:00
)
2020-06-16 11:02:14 +00:00
queryStats = prometheus.NewCounterVec(
prometheus.CounterOpts{
2020-06-16 09:42:21 +00:00
Namespace: "statping",
2020-06-16 11:02:14 +00:00
Name: "query",
2020-06-16 09:42:21 +00:00
Help: "If service is online",
2020-06-16 11:02:14 +00:00
}, []string{"type", "method"},
2020-06-16 09:42:21 +00:00
)
)
2020-06-16 11:02:14 +00:00
func Query(objType, method string) {
queryStats.WithLabelValues(objType, method).Inc()
}
func CollectDatabase(stats sql.DBStats) {
databaseStats.WithLabelValues("max_open_connections").Set(float64(stats.MaxOpenConnections))
databaseStats.WithLabelValues("open_connections").Set(float64(stats.OpenConnections))
databaseStats.WithLabelValues("in_use_connections").Set(float64(stats.InUse))
databaseStats.WithLabelValues("idle_connections").Set(float64(stats.Idle))
databaseStats.WithLabelValues("wait_count").Set(float64(stats.WaitCount))
databaseStats.WithLabelValues("wait_duration_seconds").Set(stats.WaitDuration.Seconds())
databaseStats.WithLabelValues("idle_connections_closed").Set(float64(stats.MaxIdleClosed))
databaseStats.WithLabelValues("lifetime_connections_closed").Set(float64(stats.MaxLifetimeClosed))
2020-06-16 09:42:21 +00:00
}