diff --git a/cmd/agent_auditor.go b/cmd/agent_auditor.go index 603dc8a6a..2e24a5446 100644 --- a/cmd/agent_auditor.go +++ b/cmd/agent_auditor.go @@ -21,6 +21,7 @@ import ( "github.com/bbva/qed/gossip/auditor" "github.com/bbva/qed/gossip/member" "github.com/bbva/qed/log" + "github.com/bbva/qed/metrics" "github.com/bbva/qed/util" ) @@ -57,8 +58,8 @@ func newAgentAuditorCommand(ctx *cmdContext, config gossip.Config, agentPreRun f if err != nil { log.Fatalf("Failed to start the QED monitor: %v", err) } - - agent, err := gossip.NewAgent(&config, []gossip.Processor{auditor}) + metricsServer := metrics.NewServer(config.MetricsAddr) + agent, err := gossip.NewAgent(&config, []gossip.Processor{auditor}, metricsServer) if err != nil { log.Fatalf("Failed to start the QED auditor: %v", err) } diff --git a/cmd/agent_monitor.go b/cmd/agent_monitor.go index a823ef7c6..fda4db0f6 100644 --- a/cmd/agent_monitor.go +++ b/cmd/agent_monitor.go @@ -21,6 +21,7 @@ import ( "github.com/bbva/qed/gossip/member" "github.com/bbva/qed/gossip/monitor" "github.com/bbva/qed/log" + "github.com/bbva/qed/metrics" "github.com/bbva/qed/util" ) @@ -57,8 +58,8 @@ func newAgentMonitorCommand(ctx *cmdContext, config gossip.Config, agentPreRun f if err != nil { log.Fatalf("Failed to start the QED monitor: %v", err) } - - agent, err := gossip.NewAgent(&config, []gossip.Processor{monitor}) + metricsServer := metrics.NewServer(config.MetricsAddr) + agent, err := gossip.NewAgent(&config, []gossip.Processor{monitor}, metricsServer) if err != nil { log.Fatalf("Failed to start the QED monitor: %v", err) } diff --git a/cmd/agent_publisher.go b/cmd/agent_publisher.go index cfe16e4ee..e2c40680f 100644 --- a/cmd/agent_publisher.go +++ b/cmd/agent_publisher.go @@ -21,6 +21,7 @@ import ( "github.com/bbva/qed/gossip/member" "github.com/bbva/qed/gossip/publisher" "github.com/bbva/qed/log" + "github.com/bbva/qed/metrics" "github.com/bbva/qed/util" ) @@ -56,8 +57,8 @@ func newAgentPublisherCommand(ctx *cmdContext, config gossip.Config, agentPreRun if err != nil { log.Fatalf("Failed to start the QED publisher: %v", err) } - - agent, err := gossip.NewAgent(&config, []gossip.Processor{publisher}) + metricsServer := metrics.NewServer(config.MetricsAddr) + agent, err := gossip.NewAgent(&config, []gossip.Processor{publisher}, metricsServer) if err != nil { log.Fatalf("Failed to start the QED publisher: %v", err) } diff --git a/gossip/agent.go b/gossip/agent.go index 2a9dc6f32..13e349ab6 100644 --- a/gossip/agent.go +++ b/gossip/agent.go @@ -24,6 +24,7 @@ import ( "github.com/bbva/qed/gossip/member" "github.com/bbva/qed/hashing" "github.com/bbva/qed/log" + "github.com/bbva/qed/metrics" "github.com/bbva/qed/protocol" "github.com/coocood/freecache" "github.com/hashicorp/memberlist" @@ -38,7 +39,7 @@ type Agent struct { config *Config Self *member.Peer - metricsServer *metricsServer + metricsServer *metrics.Server memberlist *memberlist.Memberlist broadcasts *memberlist.TransmitLimitedQueue @@ -55,11 +56,11 @@ type Agent struct { quit chan bool } -func NewAgent(conf *Config, p []Processor) (agent *Agent, err error) { +func NewAgent(conf *Config, p []Processor, m *metrics.Server) (agent *Agent, err error) { log.Infof("New agent %s\n", conf.NodeName) agent = &Agent{ config: conf, - metricsServer: newMetricsServer(conf.MetricsAddr), + metricsServer: m, Topology: NewTopology(), processors: p, processed: freecache.NewCache(1 << 20), @@ -134,11 +135,11 @@ func (a *Agent) ChTimedSend(batch *protocol.BatchSnapshots, ch chan *protocol.Ba func (a *Agent) start() { for _, p := range a.processors { - p.RegisterMetrics(a.metricsServer.registry) + p.RegisterMetrics(a.metricsServer) } go func() { - a.metricsServer.start() + a.metricsServer.Start() }() for { @@ -261,7 +262,7 @@ func (a *Agent) Shutdown() error { a.stateLock.Lock() defer a.stateLock.Unlock() - a.metricsServer.shutdown() + a.metricsServer.Shutdown() if a.Self.Status == member.Shutdown { return nil diff --git a/gossip/agent_test.go b/gossip/agent_test.go index 8c36100e9..9641cab61 100644 --- a/gossip/agent_test.go +++ b/gossip/agent_test.go @@ -23,6 +23,7 @@ import ( "github.com/stretchr/testify/require" "github.com/bbva/qed/gossip/member" + "github.com/bbva/qed/metrics" ) func TestJoin(t *testing.T) { @@ -30,7 +31,8 @@ func TestJoin(t *testing.T) { conf.NodeName = "testNode" conf.Role = member.Auditor conf.BindAddr = "127.0.0.1:12345" - a, _ := NewAgent(conf, []Processor{FakeProcessor{}}) + metricsServer := metrics.NewServer("127.0.0.2:23464") + a, _ := NewAgent(conf, []Processor{FakeProcessor{}}, metricsServer) testCases := []struct { agentState member.Status @@ -71,7 +73,8 @@ func TestLeave(t *testing.T) { conf.NodeName = "testNode" conf.Role = member.Auditor conf.BindAddr = "127.0.0.1:12346" - a, _ := NewAgent(conf, []Processor{FakeProcessor{}}) + metricsServer := metrics.NewServer("127.0.0.2:13445") + a, _ := NewAgent(conf, []Processor{FakeProcessor{}}, metricsServer) testCases := []struct { agentState member.Status @@ -119,7 +122,8 @@ func TestShutdown(t *testing.T) { conf.NodeName = "testNode" conf.Role = member.Auditor conf.BindAddr = "127.0.0.1:12347" - a, _ := NewAgent(conf, []Processor{FakeProcessor{}}) + metricsServer := metrics.NewServer("127.0.0.2:43512") + a, _ := NewAgent(conf, []Processor{FakeProcessor{}}, metricsServer) testCases := []struct { agentState member.Status diff --git a/gossip/auditor/auditor.go b/gossip/auditor/auditor.go index 9f9af901a..6afe3a1b6 100644 --- a/gossip/auditor/auditor.go +++ b/gossip/auditor/auditor.go @@ -28,6 +28,7 @@ import ( "github.com/bbva/qed/client" "github.com/bbva/qed/hashing" "github.com/bbva/qed/log" + "github.com/bbva/qed/metrics" "github.com/bbva/qed/protocol" "github.com/pkg/errors" @@ -123,7 +124,7 @@ func NewAuditor(conf Config) (*Auditor, error) { return &auditor, nil } -func (a Auditor) RegisterMetrics(r *prometheus.Registry) { +func (a Auditor) RegisterMetrics(srv *metrics.Server) { metrics := []prometheus.Collector{ QedAuditorInstancesCount, QedAuditorBatchesProcessSeconds, @@ -132,7 +133,7 @@ func (a Auditor) RegisterMetrics(r *prometheus.Registry) { } for _, m := range metrics { - r.Register(m) + srv.Register(m) } } diff --git a/gossip/metrics.go b/gossip/metrics.go deleted file mode 100644 index 4a281086a..000000000 --- a/gossip/metrics.go +++ /dev/null @@ -1,63 +0,0 @@ -/* - Copyright 2018 Banco Bilbao Vizcaya Argentaria, S.A. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -package gossip - -import ( - "context" - "net/http" - "time" - - "github.com/bbva/qed/api/metricshttp" - "github.com/bbva/qed/log" - "github.com/prometheus/client_golang/prometheus" -) - -type metricsServer struct { - server *http.Server - registry *prometheus.Registry -} - -func newMetricsServer(addr string) *metricsServer { - r := prometheus.NewRegistry() - return &metricsServer{ - server: &http.Server{ - Addr: addr, - Handler: metricshttp.NewMetricsHTTP(r), - }, - registry: r, - } -} - -func (m metricsServer) start() { - if err := m.server.ListenAndServe(); err != http.ErrServerClosed { - log.Errorf("Can't start metrics HTTP server: %s", err) - } -} - -func (m metricsServer) shutdown() { - ctx, _ := context.WithTimeout(context.Background(), 5*time.Second) - m.server.Shutdown(ctx) -} - -func (m metricsServer) register(metric prometheus.Collector) { - if err := m.registry.Register(metric); err != nil { - log.Infof("metric not registered:", err) - } else { - log.Infof("metric registered.") - } -} - diff --git a/gossip/monitor/monitor.go b/gossip/monitor/monitor.go index 68b2379da..63576ed85 100644 --- a/gossip/monitor/monitor.go +++ b/gossip/monitor/monitor.go @@ -28,6 +28,7 @@ import ( "github.com/bbva/qed/client" "github.com/bbva/qed/hashing" "github.com/bbva/qed/log" + "github.com/bbva/qed/metrics" "github.com/bbva/qed/protocol" "github.com/pkg/errors" @@ -120,7 +121,7 @@ func NewMonitor(conf *Config) (*Monitor, error) { return &monitor, nil } -func (m Monitor) RegisterMetrics(r *prometheus.Registry) { +func (m Monitor) RegisterMetrics(srv *metrics.Server) { metrics := []prometheus.Collector{ QedMonitorInstancesCount, QedMonitorBatchesReceivedTotal, @@ -129,7 +130,7 @@ func (m Monitor) RegisterMetrics(r *prometheus.Registry) { } for _, m := range metrics { - r.Register(m) + srv.Register(m) } } diff --git a/gossip/processor.go b/gossip/processor.go index 3c3c8a79f..2be11922f 100644 --- a/gossip/processor.go +++ b/gossip/processor.go @@ -22,25 +22,25 @@ import ( "net/http" "github.com/bbva/qed/log" + "github.com/bbva/qed/metrics" "github.com/bbva/qed/protocol" - "github.com/prometheus/client_golang/prometheus" ) type Processor interface { Process(*protocol.BatchSnapshots) - RegisterMetrics(*prometheus.Registry) + RegisterMetrics(*metrics.Server) } type FakeProcessor struct { } -func (d FakeProcessor) Process(b *protocol.BatchSnapshots) {} -func (d FakeProcessor) RegisterMetrics(r *prometheus.Registry) {} +func (d FakeProcessor) Process(b *protocol.BatchSnapshots) {} +func (d FakeProcessor) RegisterMetrics(m *metrics.Server) {} type DummyProcessor struct { } -func (d DummyProcessor) RegisterMetrics(r *prometheus.Registry) {} +func (d DummyProcessor) RegisterMetrics(m *metrics.Server) {} func (d DummyProcessor) Process(b *protocol.BatchSnapshots) { for i := 0; i < len(b.Snapshots); i++ { diff --git a/gossip/publisher/publisher.go b/gossip/publisher/publisher.go index 458b1b080..d273293f8 100644 --- a/gossip/publisher/publisher.go +++ b/gossip/publisher/publisher.go @@ -24,6 +24,7 @@ import ( "time" "github.com/bbva/qed/log" + "github.com/bbva/qed/metrics" "github.com/bbva/qed/protocol" "github.com/coocood/freecache" @@ -105,7 +106,7 @@ func NewPublisher(conf Config) (*Publisher, error) { return &publisher, nil } -func (p Publisher) RegisterMetrics(r *prometheus.Registry) { +func (p Publisher) RegisterMetrics(srv *metrics.Server) { metrics := []prometheus.Collector{ QedPublisherInstancesCount, QedPublisherBatchesReceivedTotal, @@ -113,7 +114,7 @@ func (p Publisher) RegisterMetrics(r *prometheus.Registry) { } for _, m := range metrics { - r.Register(m) + srv.Register(m) } } diff --git a/metrics/definitions.go b/metrics/definitions.go new file mode 100644 index 000000000..0d3b9eae2 --- /dev/null +++ b/metrics/definitions.go @@ -0,0 +1,198 @@ +/* + Copyright 2018-2019 Banco Bilbao Vizcaya Argentaria, S.A. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package metrics + +import ( + "expvar" + "fmt" + "sync" + + "github.com/prometheus/client_golang/prometheus" +) + +var ( + + // Balloon has a Map of all the stats relative to Balloon + Balloon *expvar.Map + + // Prometheus + + // SERVER + + QedInstancesCount = prometheus.NewGauge( + prometheus.GaugeOpts{ + Name: "qed_instances_count", + Help: "Number of QED servers currently running", + }, + ) + + // API + + QedAPIHealthcheckRequestsTotal = prometheus.NewCounter( + prometheus.CounterOpts{ + Name: "qed_api_healthcheck_requests_total", + Help: "The total number of healthcheck api requests", + }, + ) + + // BALLOON + + QedBalloonAddDurationSeconds = prometheus.NewSummary( + prometheus.SummaryOpts{ + Name: "qed_balloon_add_duration_seconds", + Help: "Duration of the add operation.", + }, + ) + QedBalloonMembershipDurationSeconds = prometheus.NewSummary( + prometheus.SummaryOpts{ + Name: "qed_balloon_membership_duration_seconds", + Help: "Duration of the membership queries.", + }, + ) + QedBalloonDigestMembershipDurationSeconds = prometheus.NewSummary( + prometheus.SummaryOpts{ + Name: "qed_balloon_digest_membership_duration_seconds", + Help: "Duration of the membership by digest queries.", + }, + ) + QedBalloonIncrementalDurationSeconds = prometheus.NewSummary( + prometheus.SummaryOpts{ + Name: "qed_balloon_incremental_duration_seconds", + Help: "Duration of the incremental queries.", + }, + ) + QedBalloonAddTotal = prometheus.NewCounter( + prometheus.CounterOpts{ + Name: "qed_balloon_add_total", + Help: "Number of add operations", + }, + ) + QedBalloonMembershipTotal = prometheus.NewCounter( + prometheus.CounterOpts{ + Name: "qed_balloon_membership_total", + Help: "Number of membership queries.", + }, + ) + QedBalloonDigestMembershipTotal = prometheus.NewGauge( + prometheus.GaugeOpts{ + Name: "qed_balloon_digest_membership_total", + Help: "Number of membership by digest queries.", + }, + ) + QedBalloonIncrementalTotal = prometheus.NewCounter( + prometheus.CounterOpts{ + Name: "qed_balloon_incremental_total", + Help: "Number of incremental queries.", + }, + ) + + // HYPER TREE + + QedHyperAddTotal = prometheus.NewCounter( + prometheus.CounterOpts{ + Name: "qed_hyper_add_total", + Help: "Number of the events added to the hyper tree.", + }, + ) + QedHyperMembershipTotal = prometheus.NewCounter( + prometheus.CounterOpts{ + Name: "qed_hyper_membership_total", + Help: "Number of membership queries", + }, + ) + + // HISTORY TREE + + QedHistoryAddTotal = prometheus.NewCounter( + prometheus.CounterOpts{ + Name: "qed_history_add_total", + Help: "Number of the events added to the history tree.", + }, + ) + QedHistoryMembershipTotal = prometheus.NewCounter( + prometheus.CounterOpts{ + Name: "qed_history_membership_total", + Help: "Number of membership queries", + }, + ) + QedHistoryConsistencyTotal = prometheus.NewCounter( + prometheus.CounterOpts{ + Name: "qed_history_consistency_total", + Help: "Number of consistency queries", + }, + ) + + // SENDER + + QedSenderInstancesCount = prometheus.NewGauge( + prometheus.GaugeOpts{ + Name: "qed_sender_instances_count", + Help: "Number of sender agents running", + }, + ) + QedSenderBatchesSentTotal = prometheus.NewCounter( + prometheus.CounterOpts{ + Name: "qed_sender_batches_sent_total", + Help: "Number of batches sent by the sender.", + }, + ) + + // PROMETHEUS + + metricsList = []prometheus.Collector{ + QedInstancesCount, + QedAPIHealthcheckRequestsTotal, + + QedBalloonAddDurationSeconds, + QedBalloonMembershipDurationSeconds, + QedBalloonDigestMembershipDurationSeconds, + QedBalloonIncrementalDurationSeconds, + + QedBalloonAddTotal, + QedBalloonMembershipTotal, + QedBalloonDigestMembershipTotal, + QedBalloonIncrementalTotal, + + QedSenderInstancesCount, + QedSenderBatchesSentTotal, + } + + registerMetrics sync.Once +) + +// Register all metrics. +func Register(r *prometheus.Registry) { + // Register the metrics. + registerMetrics.Do( + func() { + for _, metric := range metricsList { + r.MustRegister(metric) + } + }, + ) +} + +// Implement expVar.Var interface +type Uint64ToVar uint64 + +func (v Uint64ToVar) String() string { + return fmt.Sprintf("%d", v) +} + +func init() { + Balloon = expvar.NewMap("Qed_balloon_stats") +} diff --git a/metrics/metrics.go b/metrics/metrics.go index 0d3b9eae2..95315ca28 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -1,5 +1,5 @@ /* - Copyright 2018-2019 Banco Bilbao Vizcaya Argentaria, S.A. + Copyright 2018 Banco Bilbao Vizcaya Argentaria, S.A. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -17,182 +17,47 @@ package metrics import ( - "expvar" - "fmt" - "sync" + "context" + "net/http" + "time" + "github.com/bbva/qed/api/metricshttp" + "github.com/bbva/qed/log" "github.com/prometheus/client_golang/prometheus" ) -var ( - - // Balloon has a Map of all the stats relative to Balloon - Balloon *expvar.Map - - // Prometheus - - // SERVER - - QedInstancesCount = prometheus.NewGauge( - prometheus.GaugeOpts{ - Name: "qed_instances_count", - Help: "Number of QED servers currently running", - }, - ) - - // API - - QedAPIHealthcheckRequestsTotal = prometheus.NewCounter( - prometheus.CounterOpts{ - Name: "qed_api_healthcheck_requests_total", - Help: "The total number of healthcheck api requests", - }, - ) - - // BALLOON - - QedBalloonAddDurationSeconds = prometheus.NewSummary( - prometheus.SummaryOpts{ - Name: "qed_balloon_add_duration_seconds", - Help: "Duration of the add operation.", - }, - ) - QedBalloonMembershipDurationSeconds = prometheus.NewSummary( - prometheus.SummaryOpts{ - Name: "qed_balloon_membership_duration_seconds", - Help: "Duration of the membership queries.", - }, - ) - QedBalloonDigestMembershipDurationSeconds = prometheus.NewSummary( - prometheus.SummaryOpts{ - Name: "qed_balloon_digest_membership_duration_seconds", - Help: "Duration of the membership by digest queries.", - }, - ) - QedBalloonIncrementalDurationSeconds = prometheus.NewSummary( - prometheus.SummaryOpts{ - Name: "qed_balloon_incremental_duration_seconds", - Help: "Duration of the incremental queries.", - }, - ) - QedBalloonAddTotal = prometheus.NewCounter( - prometheus.CounterOpts{ - Name: "qed_balloon_add_total", - Help: "Number of add operations", - }, - ) - QedBalloonMembershipTotal = prometheus.NewCounter( - prometheus.CounterOpts{ - Name: "qed_balloon_membership_total", - Help: "Number of membership queries.", - }, - ) - QedBalloonDigestMembershipTotal = prometheus.NewGauge( - prometheus.GaugeOpts{ - Name: "qed_balloon_digest_membership_total", - Help: "Number of membership by digest queries.", - }, - ) - QedBalloonIncrementalTotal = prometheus.NewCounter( - prometheus.CounterOpts{ - Name: "qed_balloon_incremental_total", - Help: "Number of incremental queries.", - }, - ) - - // HYPER TREE - - QedHyperAddTotal = prometheus.NewCounter( - prometheus.CounterOpts{ - Name: "qed_hyper_add_total", - Help: "Number of the events added to the hyper tree.", - }, - ) - QedHyperMembershipTotal = prometheus.NewCounter( - prometheus.CounterOpts{ - Name: "qed_hyper_membership_total", - Help: "Number of membership queries", - }, - ) - - // HISTORY TREE - - QedHistoryAddTotal = prometheus.NewCounter( - prometheus.CounterOpts{ - Name: "qed_history_add_total", - Help: "Number of the events added to the history tree.", - }, - ) - QedHistoryMembershipTotal = prometheus.NewCounter( - prometheus.CounterOpts{ - Name: "qed_history_membership_total", - Help: "Number of membership queries", - }, - ) - QedHistoryConsistencyTotal = prometheus.NewCounter( - prometheus.CounterOpts{ - Name: "qed_history_consistency_total", - Help: "Number of consistency queries", - }, - ) - - // SENDER +type Server struct { + server *http.Server + registry *prometheus.Registry +} - QedSenderInstancesCount = prometheus.NewGauge( - prometheus.GaugeOpts{ - Name: "qed_sender_instances_count", - Help: "Number of sender agents running", - }, - ) - QedSenderBatchesSentTotal = prometheus.NewCounter( - prometheus.CounterOpts{ - Name: "qed_sender_batches_sent_total", - Help: "Number of batches sent by the sender.", +func NewServer(addr string) *Server { + r := prometheus.NewRegistry() + return &Server{ + server: &http.Server{ + Addr: addr, + Handler: metricshttp.NewMetricsHTTP(r), }, - ) - - // PROMETHEUS - - metricsList = []prometheus.Collector{ - QedInstancesCount, - QedAPIHealthcheckRequestsTotal, - - QedBalloonAddDurationSeconds, - QedBalloonMembershipDurationSeconds, - QedBalloonDigestMembershipDurationSeconds, - QedBalloonIncrementalDurationSeconds, - - QedBalloonAddTotal, - QedBalloonMembershipTotal, - QedBalloonDigestMembershipTotal, - QedBalloonIncrementalTotal, - - QedSenderInstancesCount, - QedSenderBatchesSentTotal, + registry: r, } - - registerMetrics sync.Once -) - -// Register all metrics. -func Register(r *prometheus.Registry) { - // Register the metrics. - registerMetrics.Do( - func() { - for _, metric := range metricsList { - r.MustRegister(metric) - } - }, - ) } -// Implement expVar.Var interface -type Uint64ToVar uint64 +func (m Server) Start() { + if err := m.server.ListenAndServe(); err != http.ErrServerClosed { + log.Errorf("Can't start metrics HTTP server: %s", err) + } +} -func (v Uint64ToVar) String() string { - return fmt.Sprintf("%d", v) +func (m Server) Shutdown() { + ctx, _ := context.WithTimeout(context.Background(), 5*time.Second) + m.server.Shutdown(ctx) } -func init() { - Balloon = expvar.NewMap("Qed_balloon_stats") +func (m Server) Register(metric prometheus.Collector) { + if err := m.registry.Register(metric); err != nil { + log.Infof("metric not registered:", err) + } else { + log.Infof("metric registered.") + } } + diff --git a/server/server.go b/server/server.go index e864fb68f..a87e61331 100644 --- a/server/server.go +++ b/server/server.go @@ -33,7 +33,6 @@ import ( "github.com/prometheus/client_golang/prometheus" "github.com/bbva/qed/api/apihttp" - "github.com/bbva/qed/api/metricshttp" "github.com/bbva/qed/api/mgmthttp" "github.com/bbva/qed/api/tampering" "github.com/bbva/qed/gossip" @@ -58,7 +57,7 @@ type Server struct { mgmtServer *http.Server raftBalloon *raftwal.RaftBalloon tamperingServer *http.Server - metricsServer *http.Server + metricsServer *metrics.Server prometheusRegistry *prometheus.Registry signer sign.Signer sender *sender.Sender @@ -123,12 +122,16 @@ func NewServer(conf *Config) (*Server, error) { return nil, err } + // metrics server + server.metricsServer = metrics.NewServer(conf.MetricsAddr) + // Create gossip agent config := gossip.DefaultConfig() config.BindAddr = conf.GossipAddr config.Role = member.Server config.NodeName = conf.NodeID - server.agent, err = gossip.NewAgent(config, nil) + + server.agent, err = gossip.NewAgent(config, nil, server.metricsServer) if err != nil { return nil, err } @@ -173,13 +176,6 @@ func NewServer(conf *Config) (*Server, error) { server.tamperingServer = newHTTPServer(fmt.Sprintf("localhost:1880%d", id), tamperMux) } - r := prometheus.NewRegistry() - metrics.Register(r) - server.prometheusRegistry = r - metricsMux := metricshttp.NewMetricsHTTP(r) - - server.metricsServer = newHTTPServer(conf.MetricsAddr, metricsMux) - return server, nil } @@ -218,9 +214,7 @@ func (s *Server) Start() error { go func() { log.Debugf(" * Starting metrics HTTP server in addr: %s", s.conf.MetricsAddr) - if err := s.metricsServer.ListenAndServe(); err != http.ErrServerClosed { - log.Errorf("Can't start metrics HTTP server: %s", err) - } + s.metricsServer.Start() }() if s.tamperingServer != nil { @@ -290,10 +284,8 @@ func (s *Server) Stop() error { log.Infof("\nShutting down QED server %s", s.conf.NodeID) log.Debugf("Metrics enabled: stopping server...") - if err := s.metricsServer.Shutdown(context.Background()); err != nil { // TODO include timeout instead nil - log.Error(err) - return err - } + s.metricsServer.Shutdown() + log.Debugf("Done.\n") if s.tamperingServer != nil { diff --git a/tests/e2e/setup.go b/tests/e2e/setup.go index 516a0fea7..1b04c23c4 100644 --- a/tests/e2e/setup.go +++ b/tests/e2e/setup.go @@ -34,6 +34,7 @@ import ( "github.com/bbva/qed/gossip/member" "github.com/bbva/qed/gossip/monitor" "github.com/bbva/qed/gossip/publisher" + "github.com/bbva/qed/metrics" "github.com/bbva/qed/server" "github.com/bbva/qed/testutils/scope" "github.com/pkg/errors" @@ -123,8 +124,8 @@ func newAgent(id int, name string, role member.Type, p gossip.Processor, t *test agentConf.EnableCompression = true agentConf.AlertsUrls = []string{AlertsURL} agentConf.Role = role - - agent, err := gossip.NewAgent(agentConf, []gossip.Processor{p}) + metricsServer := metrics.NewServer(agentConf.MetricsAddr) + agent, err := gossip.NewAgent(agentConf, []gossip.Processor{p}, metricsServer) if err != nil { t.Fatalf("Failed to start AGENT %s: %v", name, err) }