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