Implement process uptime metric (#666)
* Implement process uptime metric * Add clarification comment * Add another clarification comment
This commit is contained in:
parent
941c0acea3
commit
08bbb1537e
@ -61,6 +61,7 @@ func SetupRoutes(router *gin.Engine) {
|
|||||||
|
|
||||||
func SetupMetrics(engine *gin.Engine) {
|
func SetupMetrics(engine *gin.Engine) {
|
||||||
if command.Opts.MetricsEnabled && command.Opts.MetricsAddr == "" {
|
if command.Opts.MetricsEnabled && command.Opts.MetricsAddr == "" {
|
||||||
engine.GET("/metrics", gin.WrapH(metrics.Handler()))
|
// NOTE: We're not supporting the MetricsPath CLI option here to avoid the route conflicts.
|
||||||
|
engine.GET("/metrics", gin.WrapH(metrics.NewHandler()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -307,6 +307,8 @@ func Run() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Start a separate metrics http server. If metrics addr is not provided, we
|
||||||
|
// add the metrics endpoint in the existing application server (see api.go).
|
||||||
if options.MetricsEnabled && options.MetricsAddr != "" {
|
if options.MetricsEnabled && options.MetricsAddr != "" {
|
||||||
go startMetricsServer()
|
go startMetricsServer()
|
||||||
}
|
}
|
||||||
|
25
pkg/metrics/handler.go
Normal file
25
pkg/metrics/handler.go
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package metrics
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Handler struct {
|
||||||
|
startTime time.Time
|
||||||
|
promHandler http.Handler
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||||
|
uptimeGauge.Set(time.Since(h.startTime).Seconds())
|
||||||
|
h.promHandler.ServeHTTP(rw, req)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHandler() http.Handler {
|
||||||
|
return Handler{
|
||||||
|
startTime: time.Now(),
|
||||||
|
promHandler: promhttp.Handler(),
|
||||||
|
}
|
||||||
|
}
|
@ -20,6 +20,11 @@ var (
|
|||||||
Name: "pgweb_healthy",
|
Name: "pgweb_healthy",
|
||||||
Help: "Server health status",
|
Help: "Server health status",
|
||||||
})
|
})
|
||||||
|
|
||||||
|
uptimeGauge = promauto.NewGauge(prometheus.GaugeOpts{
|
||||||
|
Name: "pgweb_uptime",
|
||||||
|
Help: "Server application uptime in seconds",
|
||||||
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
func IncrementQueriesCount() {
|
func IncrementQueriesCount() {
|
||||||
|
@ -3,17 +3,12 @@ package metrics
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Handler() http.Handler {
|
|
||||||
return promhttp.Handler()
|
|
||||||
}
|
|
||||||
|
|
||||||
func StartServer(logger *logrus.Logger, path string, addr string) error {
|
func StartServer(logger *logrus.Logger, path string, addr string) error {
|
||||||
logger.WithField("addr", addr).WithField("path", path).Info("starting prometheus metrics server")
|
logger.WithField("addr", addr).WithField("path", path).Info("starting prometheus metrics server")
|
||||||
|
|
||||||
http.Handle(path, Handler())
|
http.Handle(path, NewHandler())
|
||||||
return http.ListenAndServe(addr, nil)
|
return http.ListenAndServe(addr, nil)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user