From 90c7dcf891323e49e67d2f7e93606a34283f33f2 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 2 May 2023 02:26:51 +0200 Subject: [PATCH] feat: add database latency metric --- backend/db/metrics.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/backend/db/metrics.go b/backend/db/metrics.go index 144c3bc..b8ca51d 100644 --- a/backend/db/metrics.go +++ b/backend/db/metrics.go @@ -2,6 +2,7 @@ package db import ( "context" + "time" "codeberg.org/u1f320/pronouns.cc/backend/log" "emperror.dev/errors" @@ -38,6 +39,19 @@ func (db *DB) initMetrics() (err error) { 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{ Name: "pronouns_api_requests_total", Help: "The total number of API requests since the last restart",