feat: add database latency metric
This commit is contained in:
parent
3f003b5353
commit
90c7dcf891
|
@ -2,6 +2,7 @@ package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
"codeberg.org/u1f320/pronouns.cc/backend/log"
|
"codeberg.org/u1f320/pronouns.cc/backend/log"
|
||||||
"emperror.dev/errors"
|
"emperror.dev/errors"
|
||||||
|
@ -38,6 +39,19 @@ func (db *DB) initMetrics() (err error) {
|
||||||
return errors.Wrap(err, "registering member count gauge")
|
return errors.Wrap(err, "registering member count gauge")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = prometheus.Register(prometheus.NewGaugeFunc(prometheus.GaugeOpts{
|
||||||
|
Name: "pronouns_database_latency",
|
||||||
|
Help: "The latency to the database in nanoseconds",
|
||||||
|
}, func() float64 {
|
||||||
|
start := time.Now()
|
||||||
|
_, err = db.Exec(context.Background(), "SELECT 1")
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("pinging database: %v", err)
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
return float64(time.Since(start))
|
||||||
|
}))
|
||||||
|
|
||||||
db.TotalRequests = promauto.NewCounter(prometheus.CounterOpts{
|
db.TotalRequests = promauto.NewCounter(prometheus.CounterOpts{
|
||||||
Name: "pronouns_api_requests_total",
|
Name: "pronouns_api_requests_total",
|
||||||
Help: "The total number of API requests since the last restart",
|
Help: "The total number of API requests since the last restart",
|
||||||
|
|
Loading…
Reference in New Issue