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:
39
pkg/metrics/metrics.go
Normal file
39
pkg/metrics/metrics.go
Normal 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
19
pkg/metrics/server.go
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user