Setup basic prom metrics endpoint (#624)

* Setup basic prom metrics endpoint
* Use default prom handler to expose go runtime metrics
This commit is contained in:
Dan Sosedoff
2022-12-20 10:13:42 -06:00
committed by GitHub
parent 837e25be74
commit b31e7f1ea7
9 changed files with 143 additions and 16 deletions

39
pkg/metrics/metrics.go Normal file
View File

@@ -0,0 +1,39 @@
package metrics
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
var (
sessionsGauge = promauto.NewGauge(prometheus.GaugeOpts{
Name: "pgweb_sessions_count",
Help: "Total number of database sessions",
})
queriesCounter = promauto.NewCounter(prometheus.CounterOpts{
Name: "pgweb_queries_count",
Help: "Total number of custom queries executed",
})
healtyGauge = promauto.NewGauge(prometheus.GaugeOpts{
Name: "pgweb_healty",
Help: "Server health status",
})
)
func IncrementQueriesCount() {
queriesCounter.Inc()
}
func SetSessionsCount(val int) {
sessionsGauge.Set(float64(val))
}
func SetHealty(val bool) {
healthy := 0.0
if val {
healthy = 1.0
}
healtyGauge.Set(float64(healthy))
}

19
pkg/metrics/server.go Normal file
View File

@@ -0,0 +1,19 @@
package metrics
import (
"net/http"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sirupsen/logrus"
)
func Handler() http.Handler {
return promhttp.Handler()
}
func StartServer(logger *logrus.Logger, path string, addr string) error {
logger.WithField("addr", addr).WithField("path", path).Info("starting prometheus metrics server")
http.Handle(path, Handler())
return http.ListenAndServe(addr, nil)
}