From a649f2f90300daae2eedd5bdcc0e2e44afcf132f Mon Sep 17 00:00:00 2001 From: pancho horrillo Date: Thu, 31 Jan 2019 14:39:39 +0100 Subject: [PATCH] Add a prometheus registry to the Server struct This way the Server can be shut down leaving no state behind. We need this in tests, when Servers are created and shut down repeatedly. --- api/metricshttp/metricshttp.go | 5 +++-- server/server.go | 25 ++++++++++++++----------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/api/metricshttp/metricshttp.go b/api/metricshttp/metricshttp.go index 5c78764e8..cf897b0d1 100644 --- a/api/metricshttp/metricshttp.go +++ b/api/metricshttp/metricshttp.go @@ -3,11 +3,12 @@ package metricshttp import ( "net/http" + "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" ) -func NewMetricsHttp() *http.ServeMux { +func NewMetricsHTTP(r *prometheus.Registry) *http.ServeMux { mux := http.NewServeMux() - mux.Handle("/metrics", promhttp.Handler()) + mux.Handle("/metrics", promhttp.HandlerFor(r, promhttp.HandlerOpts{})) return mux } diff --git a/server/server.go b/server/server.go index 9b48e90bb..680995d11 100644 --- a/server/server.go +++ b/server/server.go @@ -44,6 +44,7 @@ import ( "github.com/bbva/qed/sign" "github.com/bbva/qed/storage/badger" "github.com/bbva/qed/util" + "github.com/prometheus/client_golang/prometheus" ) // Server encapsulates the data and login to start/stop a QED server @@ -51,16 +52,17 @@ type Server struct { conf *Config bootstrap bool // Set bootstrap to true when bringing up the first node as a master - httpServer *http.Server - mgmtServer *http.Server - raftBalloon *raftwal.RaftBalloon - tamperingServer *http.Server - profilingServer *http.Server - metricsServer *http.Server - signer sign.Signer - sender *sender.Sender - agent *gossip.Agent - agentsQueue chan *protocol.Snapshot + httpServer *http.Server + mgmtServer *http.Server + raftBalloon *raftwal.RaftBalloon + tamperingServer *http.Server + profilingServer *http.Server + metricsServer *http.Server + prometheusRegistry *prometheus.Registry + signer sign.Signer + sender *sender.Sender + agent *gossip.Agent + agentsQueue chan *protocol.Snapshot } func serverInfo(conf *Config) http.HandlerFunc { @@ -167,7 +169,8 @@ func NewServer(conf *Config) (*Server, error) { server.profilingServer = newHTTPServer("localhost:6060", nil) } if conf.EnableMetrics { - metricsMux := metricshttp.NewMetricsHttp() + server.prometheusRegistry = prometheus.NewRegistry() + metricsMux := metricshttp.NewMetricsHTTP(server.prometheusRegistry) server.metricsServer = newHTTPServer("localhost:9990", metricsMux) }