feat: add database latency metric

This commit is contained in:
Sam 2023-05-02 02:26:51 +02:00
parent 3f003b5353
commit 90c7dcf891
No known key found for this signature in database
GPG key ID: B4EF20DDE721CAA1

View file

@ -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",