Skip to content

Commit

Permalink
Separate server metrics from default definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
aalda committed Apr 1, 2019
1 parent 09efd47 commit ee53275
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 12 deletions.
8 changes: 0 additions & 8 deletions metrics/definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ import (
)

var (
QedInstancesCount = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "qed_instances_count",
Help: "Number of QED servers currently running",
},
)

// API

Expand Down Expand Up @@ -127,7 +121,6 @@ var (
// PROMETHEUS

DefaultMetrics = []prometheus.Collector{
QedInstancesCount,
QedAPIHealthcheckRequestsTotal,

QedBalloonAddDurationSeconds,
Expand All @@ -141,4 +134,3 @@ var (
QedBalloonIncrementalTotal,
}
)

4 changes: 4 additions & 0 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ type Registry interface {
MustRegister(...prometheus.Collector)
}

type Registerer interface {
RegisterMetrics(Registry)
}

// A metrics server holds the http API and the prometheus registry
// which provides access to the registered metrics.
type Server struct {
Expand Down
47 changes: 47 additions & 0 deletions server/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
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 server

import "github.com/prometheus/client_golang/prometheus"

// namespace is the leading part of all published metrics.
const namespace = "qed"

// subsystem associated with metrics for server
const subsystem = "server"

type serverMetrics struct {
Instances prometheus.Gauge
}

func newServerMetrics() *serverMetrics {
return &serverMetrics{
Instances: prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "instances",
Help: "Number of QED servers currently running",
},
),
}
}

// collectors satisfies the prom.PrometheusCollector interface.
func (m *serverMetrics) collectors() []prometheus.Collector {
return []prometheus.Collector{
m.Instances,
}
}
15 changes: 12 additions & 3 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type Server struct {
httpServer *http.Server
mgmtServer *http.Server
raftBalloon *raftwal.RaftBalloon
metrics *serverMetrics
metricsServer *metrics.Server
prometheusRegistry *prometheus.Registry
signer sign.Signer
Expand Down Expand Up @@ -119,8 +120,9 @@ func NewServer(conf *Config) (*Server, error) {
}

// create metrics server and register default qed metrics
server.metrics = newServerMetrics()
server.metricsServer = metrics.NewServer(conf.MetricsAddr)
server.metricsServer.MustRegister(metrics.DefaultMetrics...)
server.RegisterMetrics(server.metricsServer)
store.RegisterMetrics(server.metricsServer)
// server.metricsServer.Register(raft.PrometheusCollectors())
// server.metricsServer.Register(balloon.PrometheusCollectors())
Expand Down Expand Up @@ -195,7 +197,7 @@ func join(joinAddr, raftAddr, nodeID string, metadata map[string]string) error {

// Start will start the server in a non-blockable fashion.
func (s *Server) Start() error {
metrics.QedInstancesCount.Inc()
s.metrics.Instances.Inc()
log.Infof("Starting QED server %s\n", s.conf.NodeID)

metadata := map[string]string{}
Expand Down Expand Up @@ -264,7 +266,7 @@ func (s *Server) Start() error {

// Stop will close all the channels from the mux servers.
func (s *Server) Stop() error {
metrics.QedInstancesCount.Dec()
s.metrics.Instances.Dec()
log.Infof("\nShutting down QED server %s", s.conf.NodeID)

log.Debugf("Metrics enabled: stopping server...")
Expand Down Expand Up @@ -303,6 +305,13 @@ func (s *Server) Stop() error {
return nil
}

func (s *Server) RegisterMetrics(registry metrics.Registry) {
if registry != nil {
registry.MustRegister(s.metrics.collectors()...)
registry.MustRegister(metrics.DefaultMetrics...) // TODO: remove this!!!
}
}

func newTLSServer(addr string, mux *http.ServeMux) *http.Server {

cfg := &tls.Config{
Expand Down
2 changes: 1 addition & 1 deletion storage/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ type ManagedStore interface {
Backup(w io.Writer, until uint64) error
Snapshot() (uint64, error)
Load(r io.Reader) error
RegisterMetrics(metrics.Registry)
metrics.Registerer
}

type Mutation struct {
Expand Down

0 comments on commit ee53275

Please sign in to comment.