Skip to content

Commit

Permalink
Fix metrics.go to use non-default Prometheus registry
Browse files Browse the repository at this point in the history
  • Loading branch information
panchoh authored and iknite committed Feb 19, 2019
1 parent a649f2f commit c45832f
Show file tree
Hide file tree
Showing 2 changed files with 203 additions and 151 deletions.
347 changes: 198 additions & 149 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ package metrics
import (
"expvar"
"fmt"
"sync"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)

var (
Expand All @@ -33,39 +34,210 @@ var (

// Prometheus
// API
Api_healthcheck_requests_total prometheus.Counter
API_healthcheck_requests_total = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "API_healthcheck_requests_total",
Help: "The total number of healthcheck api requests",
},
)

// Balloon
Balloon_add_duration_seconds prometheus.Gauge
Balloon_membership_duration_seconds prometheus.Gauge
Balloon_digest_membership_duration_seconds prometheus.Gauge
Balloon_incremental_duration_seconds prometheus.Gauge

Balloon_add_total prometheus.Gauge
Balloon_membership_total prometheus.Gauge
Balloon_digest_membership_total prometheus.Gauge
Balloon_incremental_total prometheus.Gauge
Balloon_add_duration_seconds = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "Balloon_add_duration_seconds",
Help: "Duration of the 'Add' balloon method.",
},
)

Balloon_membership_duration_seconds = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "Balloon_membership_duration_seconds",
Help: "Duration of the 'Membership' balloon method.",
},
)

Balloon_digest_membership_duration_seconds = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "Balloon_digest_membership_duration_seconds",
Help: "Duration of the 'Digest Membership' balloon method",
},
)

Balloon_incremental_duration_seconds = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "Balloon_incremental_duration_seconds",
Help: "Duration of the 'Incremental' balloon method.",
},
)

Balloon_add_total = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "Balloon_add_total",
Help: "Duration of the last call of an example function.",
},
)

Balloon_membership_total = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "Balloon_membership_total",
Help: "Duration of the last call of an example function.",
},
)

Balloon_digest_membership_total = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "Balloon_digest_membership_total",
Help: "Duration of the last call of an example function.",
},
)

Balloon_incremental_total = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "Balloon_incremental_total",
Help: "Duration of the last call of an example function.",
},
)

// Agents
Sender_instances_count prometheus.Gauge
Auditor_instances_count prometheus.Gauge
Monitor_instances_count prometheus.Gauge
Publisher_instances_count prometheus.Gauge

Sender_batches_sent_total prometheus.Gauge
Auditor_batches_received_total prometheus.Gauge
Monitor_batches_received_total prometheus.Gauge
Publisher_batches_received_total prometheus.Gauge

Auditor_batches_process_seconds prometheus.Gauge
Monitor_batches_process_seconds prometheus.Gauge
Publisher_batches_process_seconds prometheus.Gauge
Sender_instances_count = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "Sender_instances_count",
Help: "Duration of the last call of an example function.",
},
)

Auditor_instances_count = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "Auditor_instances_count",
Help: "Duration of the last call of an example function.",
},
)

Monitor_instances_count = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "Monitor_instances_count",
Help: "Duration of the last call of an example function.",
},
)

Publisher_instances_count = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "Publisher_instances_count",
Help: "Duration of the last call of an example function.",
},
)

Sender_batches_sent_total = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "Sender_batches_sent_total",
Help: "Duration of the last call of an example function.",
},
)

Auditor_batches_received_total = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "Auditor_batches_received_total",
Help: "Duration of the last call of an example function.",
},
)

Monitor_batches_received_total = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "Monitor_batches_received_total",
Help: "Duration of the last call of an example function.",
},
)

Publisher_batches_received_total = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "Publisher_batches_received_total",
Help: "Duration of the last call of an example function.",
},
)

Auditor_batches_process_seconds = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "Auditor_batches_process_seconds",
Help: "Duration of the last call of an example function.",
},
)

Monitor_batches_process_seconds = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "Monitor_batches_process_seconds",
Help: "Duration of the last call of an example function.",
},
)

Publisher_batches_process_seconds = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "Publisher_batches_process_seconds",
Help: "Duration of the last call of an example function.",
},
)

// Example
FuncDuration prometheus.Gauge
RequestDuration prometheus.Histogram
FuncDuration = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "example_function_duration_seconds",
Help: "Duration of the last call of an example function.",
},
)

RequestDuration = prometheus.NewHistogram(
prometheus.HistogramOpts{
Name: "example_request_duration_seconds",
Help: "Histogram for the runtime of a simple example function.",
Buckets: prometheus.LinearBuckets(0.01, 0.01, 10),
},
)

metricsList = []prometheus.Collector{
API_healthcheck_requests_total,

Balloon_add_duration_seconds,
Balloon_membership_duration_seconds,
Balloon_digest_membership_duration_seconds,
Balloon_incremental_duration_seconds,

Balloon_add_total,
Balloon_membership_total,
Balloon_digest_membership_total,
Balloon_incremental_total,

Sender_instances_count,
Auditor_instances_count,
Monitor_instances_count,
Publisher_instances_count,

Sender_batches_sent_total,
Auditor_batches_received_total,
Monitor_batches_received_total,
Publisher_batches_received_total,

Auditor_batches_process_seconds,
Monitor_batches_process_seconds,
Publisher_batches_process_seconds,

FuncDuration,
RequestDuration,
}
)

var 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

Expand All @@ -77,127 +249,4 @@ func init() {
Hyper = expvar.NewMap("hyper_stats")
History = expvar.NewMap("history_stats")
Balloon = expvar.NewMap("Balloon_stats")

apiMetrics()
balloonMetrics()
agentMetrics()

FuncDuration = promauto.NewGauge(prometheus.GaugeOpts{
Name: "example_function_duration_seconds",
Help: "Duration of the last call of an example function.",
})

RequestDuration = promauto.NewHistogram(prometheus.HistogramOpts{
Name: "example_request_duration_seconds",
Help: "Histogram for the runtime of a simple example function.",
Buckets: prometheus.LinearBuckets(0.01, 0.01, 10),
})
}

func apiMetrics() {
Api_healthcheck_requests_total = promauto.NewCounter(prometheus.CounterOpts{
Name: "Api_healthcheck_requests_total",
Help: "The total number of healthcheck api requests",
})
}

func balloonMetrics() {

Balloon_add_duration_seconds = promauto.NewGauge(prometheus.GaugeOpts{
Name: "Balloon_add_duration_seconds",
Help: "Duration of the 'Add' balloon method.",
})

Balloon_membership_duration_seconds = promauto.NewGauge(prometheus.GaugeOpts{
Name: "Balloon_membership_duration_seconds",
Help: "Duration of the 'Membership' balloon method.",
})

Balloon_digest_membership_duration_seconds = promauto.NewGauge(prometheus.GaugeOpts{
Name: "Balloon_digest_membership_duration_seconds",
Help: "Duration of the 'Digest Membership' balloon method",
})

Balloon_incremental_duration_seconds = promauto.NewGauge(prometheus.GaugeOpts{
Name: "Balloon_incremental_duration_seconds",
Help: "Duration of the 'Incremental' balloon method.",
})

Balloon_add_total = promauto.NewGauge(prometheus.GaugeOpts{
Name: "Balloon_add_total",
Help: "Duration of the last call of an example function.",
})

Balloon_membership_total = promauto.NewGauge(prometheus.GaugeOpts{
Name: "Balloon_membership_total",
Help: "Duration of the last call of an example function.",
})

Balloon_digest_membership_total = promauto.NewGauge(prometheus.GaugeOpts{
Name: "Balloon_digest_membership_total",
Help: "Duration of the last call of an example function.",
})

Balloon_incremental_total = promauto.NewGauge(prometheus.GaugeOpts{
Name: "Balloon_incremental_total",
Help: "Duration of the last call of an example function.",
})
}

func agentMetrics() {

Sender_instances_count = promauto.NewGauge(prometheus.GaugeOpts{
Name: "Sender_instances_count",
Help: "Duration of the last call of an example function.",
})

Auditor_instances_count = promauto.NewGauge(prometheus.GaugeOpts{
Name: "Auditor_instances_count",
Help: "Duration of the last call of an example function.",
})

Monitor_instances_count = promauto.NewGauge(prometheus.GaugeOpts{
Name: "Monitor_instances_count",
Help: "Duration of the last call of an example function.",
})

Publisher_instances_count = promauto.NewGauge(prometheus.GaugeOpts{
Name: "Publisher_instances_count",
Help: "Duration of the last call of an example function.",
})

Sender_batches_sent_total = promauto.NewGauge(prometheus.GaugeOpts{
Name: "Sender_batches_sent_total",
Help: "Duration of the last call of an example function.",
})

Auditor_batches_received_total = promauto.NewGauge(prometheus.GaugeOpts{
Name: "Auditor_batches_received_total",
Help: "Duration of the last call of an example function.",
})

Monitor_batches_received_total = promauto.NewGauge(prometheus.GaugeOpts{
Name: "Monitor_batches_received_total",
Help: "Duration of the last call of an example function.",
})

Publisher_batches_received_total = promauto.NewGauge(prometheus.GaugeOpts{
Name: "Publisher_batches_received_total",
Help: "Duration of the last call of an example function.",
})

Auditor_batches_process_seconds = promauto.NewGauge(prometheus.GaugeOpts{
Name: "Auditor_batches_process_seconds",
Help: "Duration of the last call of an example function.",
})

Monitor_batches_process_seconds = promauto.NewGauge(prometheus.GaugeOpts{
Name: "Monitor_batches_process_seconds",
Help: "Duration of the last call of an example function.",
})

Publisher_batches_process_seconds = promauto.NewGauge(prometheus.GaugeOpts{
Name: "Publisher_batches_process_seconds",
Help: "Duration of the last call of an example function.",
})
}
7 changes: 5 additions & 2 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"github.com/bbva/qed/gossip/sender"
"github.com/bbva/qed/hashing"
"github.com/bbva/qed/log"
"github.com/bbva/qed/metrics"
"github.com/bbva/qed/protocol"
"github.com/bbva/qed/raftwal"
"github.com/bbva/qed/sign"
Expand Down Expand Up @@ -169,8 +170,10 @@ func NewServer(conf *Config) (*Server, error) {
server.profilingServer = newHTTPServer("localhost:6060", nil)
}
if conf.EnableMetrics {
server.prometheusRegistry = prometheus.NewRegistry()
metricsMux := metricshttp.NewMetricsHTTP(server.prometheusRegistry)
r := prometheus.NewRegistry()
metrics.Register(r)
server.prometheusRegistry = r
metricsMux := metricshttp.NewMetricsHTTP(r)
server.metricsServer = newHTTPServer("localhost:9990", metricsMux)
}

Expand Down

0 comments on commit c45832f

Please sign in to comment.