mirror of https://github.com/statping/statping
prometheus metric updates
parent
d4a557d2f8
commit
1f14750311
|
@ -126,11 +126,7 @@ func Routine() {
|
|||
time.Sleep(5 * time.Second)
|
||||
continue
|
||||
}
|
||||
stats := database.DB().Stats()
|
||||
metrics.Database("connections", float64(stats.OpenConnections))
|
||||
metrics.Database("in_use", float64(stats.InUse))
|
||||
metrics.Database("idle", float64(stats.Idle))
|
||||
|
||||
metrics.CollectDatabase(database.DB().Stats())
|
||||
time.Sleep(5 * time.Second)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package checkins
|
|||
|
||||
import (
|
||||
"github.com/statping/statping/database"
|
||||
"github.com/statping/statping/types/metrics"
|
||||
"github.com/statping/statping/utils"
|
||||
)
|
||||
|
||||
|
@ -13,6 +14,10 @@ func SetDB(database database.Database) {
|
|||
dbHits = database.Model(&CheckinHit{})
|
||||
}
|
||||
|
||||
func (c *Checkin) AfterFind() {
|
||||
metrics.Query("checkin", "find")
|
||||
}
|
||||
|
||||
func Find(id int64) (*Checkin, error) {
|
||||
var checkin Checkin
|
||||
q := db.Where("id = ?", id).Find(&checkin)
|
||||
|
|
|
@ -3,6 +3,7 @@ package core
|
|||
import (
|
||||
"github.com/pkg/errors"
|
||||
"github.com/statping/statping/database"
|
||||
"github.com/statping/statping/types/metrics"
|
||||
"github.com/statping/statping/types/null"
|
||||
"github.com/statping/statping/utils"
|
||||
)
|
||||
|
@ -13,6 +14,10 @@ func SetDB(database database.Database) {
|
|||
db = database.Model(&Core{})
|
||||
}
|
||||
|
||||
func (c *Core) AfterFind() {
|
||||
metrics.Query("core", "find")
|
||||
}
|
||||
|
||||
func Select() (*Core, error) {
|
||||
var c Core
|
||||
// SelectCore will return the CoreApp global variable and the settings/configs for Statping
|
||||
|
|
|
@ -15,14 +15,27 @@ func DB() database.Database {
|
|||
return db
|
||||
}
|
||||
|
||||
func All() []*Failure {
|
||||
var failures []*Failure
|
||||
db.Find(&failures)
|
||||
return failures
|
||||
func (f *Failure) AfterFind() {
|
||||
metrics.Query("failure", "find")
|
||||
}
|
||||
|
||||
func (f *Failure) AfterUpdate() {
|
||||
metrics.Query("failure", "update")
|
||||
}
|
||||
|
||||
func (f *Failure) AfterDelete() {
|
||||
metrics.Query("failure", "delete")
|
||||
}
|
||||
|
||||
func (f *Failure) AfterCreate() {
|
||||
metrics.Inc("failure", f.Service)
|
||||
metrics.Query("failure", "create")
|
||||
}
|
||||
|
||||
func All() []*Failure {
|
||||
var failures []*Failure
|
||||
db.Find(&failures)
|
||||
return failures
|
||||
}
|
||||
|
||||
func (f *Failure) Create() error {
|
||||
|
|
|
@ -3,6 +3,7 @@ package groups
|
|||
import (
|
||||
"github.com/statping/statping/database"
|
||||
"github.com/statping/statping/types/errors"
|
||||
"github.com/statping/statping/types/metrics"
|
||||
"github.com/statping/statping/utils"
|
||||
"sort"
|
||||
)
|
||||
|
@ -16,6 +17,22 @@ func SetDB(database database.Database) {
|
|||
db = database.Model(&Group{})
|
||||
}
|
||||
|
||||
func (g *Group) AfterFind() {
|
||||
metrics.Query("group", "find")
|
||||
}
|
||||
|
||||
func (g *Group) AfterUpdate() {
|
||||
metrics.Query("group", "update")
|
||||
}
|
||||
|
||||
func (g *Group) AfterDelete() {
|
||||
metrics.Query("group", "delete")
|
||||
}
|
||||
|
||||
func (g *Group) AfterCreate() {
|
||||
metrics.Query("group", "create")
|
||||
}
|
||||
|
||||
func Find(id int64) (*Group, error) {
|
||||
var group Group
|
||||
q := db.Where("id = ?", id).Find(&group)
|
||||
|
|
|
@ -14,8 +14,21 @@ func SetDB(database database.Database) {
|
|||
db = database.Model(&Hit{})
|
||||
}
|
||||
|
||||
func (h *Hit) AfterFind() {
|
||||
metrics.Query("hit", "find")
|
||||
}
|
||||
|
||||
func (h *Hit) AfterUpdate() {
|
||||
metrics.Query("hit", "update")
|
||||
}
|
||||
|
||||
func (h *Hit) AfterDelete() {
|
||||
metrics.Query("hit", "delete")
|
||||
}
|
||||
|
||||
func (h *Hit) AfterCreate() {
|
||||
metrics.Inc("success", h.Service)
|
||||
metrics.Query("hit", "create")
|
||||
}
|
||||
|
||||
func (h *Hit) Create() error {
|
||||
|
|
|
@ -2,6 +2,7 @@ package incidents
|
|||
|
||||
import (
|
||||
"github.com/statping/statping/database"
|
||||
"github.com/statping/statping/types/metrics"
|
||||
"github.com/statping/statping/utils"
|
||||
)
|
||||
|
||||
|
@ -16,6 +17,38 @@ func SetDB(database database.Database) {
|
|||
dbUpdate = database.Model(&IncidentUpdate{})
|
||||
}
|
||||
|
||||
func (i *Incident) AfterFind() {
|
||||
metrics.Query("incident", "find")
|
||||
}
|
||||
|
||||
func (i *Incident) AfterCreate() {
|
||||
metrics.Query("incident", "create")
|
||||
}
|
||||
|
||||
func (i *Incident) AfterUpdate() {
|
||||
metrics.Query("incident", "update")
|
||||
}
|
||||
|
||||
func (i *Incident) AfterDelete() {
|
||||
metrics.Query("incident", "delete")
|
||||
}
|
||||
|
||||
func (i *IncidentUpdate) AfterFind() {
|
||||
metrics.Query("incident_update", "find")
|
||||
}
|
||||
|
||||
func (i *IncidentUpdate) AfterCreate() {
|
||||
metrics.Query("incident_update", "create")
|
||||
}
|
||||
|
||||
func (i *IncidentUpdate) AfterUpdate() {
|
||||
metrics.Query("incident_update", "update")
|
||||
}
|
||||
|
||||
func (i *IncidentUpdate) AfterDelete() {
|
||||
metrics.Query("incident_update", "delete")
|
||||
}
|
||||
|
||||
func FindUpdate(uid int64) (*IncidentUpdate, error) {
|
||||
var update IncidentUpdate
|
||||
q := dbUpdate.Where("id = ?", uid).Find(&update)
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package messages
|
||||
|
||||
import (
|
||||
"github.com/statping/statping/types/metrics"
|
||||
"github.com/statping/statping/utils"
|
||||
)
|
||||
|
||||
|
@ -12,3 +13,19 @@ func (m *Message) BeforeCreate() (err error) {
|
|||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (m *Message) AfterFind() {
|
||||
metrics.Query("message", "find")
|
||||
}
|
||||
|
||||
func (m *Message) AfterCreate() {
|
||||
metrics.Query("message", "create")
|
||||
}
|
||||
|
||||
func (m *Message) AfterUpdate() {
|
||||
metrics.Query("message", "update")
|
||||
}
|
||||
|
||||
func (m *Message) AfterDelete() {
|
||||
metrics.Query("message", "delete")
|
||||
}
|
||||
|
|
|
@ -1,39 +1,40 @@
|
|||
package metrics
|
||||
|
||||
import "github.com/prometheus/client_golang/prometheus"
|
||||
import (
|
||||
"database/sql"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
var (
|
||||
// service is online if set to 1, offline if 0
|
||||
databaseConnections = prometheus.NewGaugeVec(
|
||||
databaseStats = prometheus.NewGaugeVec(
|
||||
prometheus.GaugeOpts{
|
||||
Namespace: "statping",
|
||||
Name: "database_connections",
|
||||
Name: "database",
|
||||
Help: "If service is online",
|
||||
}, nil,
|
||||
}, []string{"metric"},
|
||||
)
|
||||
databaseInUse = prometheus.NewGaugeVec(
|
||||
prometheus.GaugeOpts{
|
||||
|
||||
queryStats = prometheus.NewCounterVec(
|
||||
prometheus.CounterOpts{
|
||||
Namespace: "statping",
|
||||
Name: "database_connections_in_use",
|
||||
Name: "query",
|
||||
Help: "If service is online",
|
||||
}, nil,
|
||||
)
|
||||
databaseIdle = prometheus.NewGaugeVec(
|
||||
prometheus.GaugeOpts{
|
||||
Namespace: "statping",
|
||||
Name: "database_connections_idle",
|
||||
Help: "If service is online",
|
||||
}, nil,
|
||||
}, []string{"type", "method"},
|
||||
)
|
||||
)
|
||||
|
||||
func Database(method string, value float64) {
|
||||
switch method {
|
||||
case "connections":
|
||||
databaseConnections.WithLabelValues().Set(value)
|
||||
case "in_use":
|
||||
databaseInUse.WithLabelValues().Set(value)
|
||||
case "idle":
|
||||
databaseIdle.WithLabelValues().Set(value)
|
||||
}
|
||||
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))
|
||||
}
|
||||
|
|
|
@ -42,7 +42,8 @@ func InitMetrics() {
|
|||
utilsHttpRequestDur,
|
||||
utilsHttpRequestBytes,
|
||||
httpDuration,
|
||||
databaseConnections,
|
||||
databaseStats,
|
||||
queryStats,
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
@ -1,10 +1,26 @@
|
|||
package notifications
|
||||
|
||||
import "github.com/statping/statping/utils"
|
||||
import (
|
||||
"github.com/statping/statping/types/metrics"
|
||||
"github.com/statping/statping/utils"
|
||||
)
|
||||
|
||||
// AfterFind for Notification will set the timezone
|
||||
func (n *Notification) AfterFind() (err error) {
|
||||
n.CreatedAt = utils.Now()
|
||||
n.UpdatedAt = utils.Now()
|
||||
metrics.Query("notifier", "find")
|
||||
return
|
||||
}
|
||||
|
||||
func (n *Notification) AfterCreate() {
|
||||
metrics.Query("notifier", "create")
|
||||
}
|
||||
|
||||
func (n *Notification) AfterUpdate() {
|
||||
metrics.Query("notifier", "update")
|
||||
}
|
||||
|
||||
func (n *Notification) AfterDelete() {
|
||||
metrics.Query("notifier", "delete")
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"fmt"
|
||||
"github.com/statping/statping/database"
|
||||
"github.com/statping/statping/types/errors"
|
||||
"github.com/statping/statping/types/metrics"
|
||||
"github.com/statping/statping/utils"
|
||||
"sort"
|
||||
)
|
||||
|
@ -14,6 +15,18 @@ var (
|
|||
allServices map[int64]*Service
|
||||
)
|
||||
|
||||
func (s *Service) AfterFind() {
|
||||
metrics.Query("service", "find")
|
||||
}
|
||||
|
||||
func (s *Service) AfterUpdate() {
|
||||
metrics.Query("service", "update")
|
||||
}
|
||||
|
||||
func (s *Service) AfterDelete() {
|
||||
metrics.Query("service", "delete")
|
||||
}
|
||||
|
||||
func init() {
|
||||
allServices = make(map[int64]*Service)
|
||||
}
|
||||
|
@ -65,6 +78,7 @@ func (s *Service) Create() error {
|
|||
|
||||
func (s *Service) AfterCreate() error {
|
||||
allServices[s.Id] = s
|
||||
metrics.Query("service", "create")
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@ package users
|
|||
|
||||
import (
|
||||
"github.com/statping/statping/database"
|
||||
"github.com/statping/statping/types/metrics"
|
||||
"github.com/statping/statping/utils"
|
||||
)
|
||||
|
||||
|
@ -14,6 +15,22 @@ func SetDB(database database.Database) {
|
|||
db = database.Model(&User{})
|
||||
}
|
||||
|
||||
func (u *User) AfterFind() {
|
||||
metrics.Query("user", "find")
|
||||
}
|
||||
|
||||
func (u *User) AfterCreate() {
|
||||
metrics.Query("user", "create")
|
||||
}
|
||||
|
||||
func (u *User) AfterUpdate() {
|
||||
metrics.Query("user", "update")
|
||||
}
|
||||
|
||||
func (u *User) AfterDelete() {
|
||||
metrics.Query("user", "delete")
|
||||
}
|
||||
|
||||
func Find(id int64) (*User, error) {
|
||||
var user User
|
||||
q := db.Where("id = ?", id).Find(&user)
|
||||
|
|
Loading…
Reference in New Issue