Skip to content

Commit

Permalink
Export Prometheus metrics as well as Crowley's
Browse files Browse the repository at this point in the history
  • Loading branch information
2 people authored and iknite committed Feb 19, 2019
1 parent c74224e commit 275b69d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
22 changes: 22 additions & 0 deletions api/apihttp/apihttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ import (
"github.com/bbva/qed/log"
"github.com/bbva/qed/protocol"
"github.com/bbva/qed/raftwal"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/rcrowley/go-metrics"
)

var (
// Prometheus:
opsProcessed = promauto.NewCounter(prometheus.CounterOpts{
Name: "qed_healthcheck_ops_total",
Help: "The total number of processed events",
})

// Crowley:
cOpsProcessed = metrics.NewCounter()
)

// HealthCheckResponse contains the response from HealthCheckHandler.
Expand All @@ -43,6 +58,12 @@ type HealthCheckResponse struct {
// If everything is allright, the HTTP status is 200 and the body contains:
// {"version": "0", "status":"ok"}
func HealthCheckHandler(w http.ResponseWriter, r *http.Request) {
// Prometheus
opsProcessed.Inc()

// Crowley
cOpsProcessed.Inc(1)

result := HealthCheckResponse{
Version: 0,
Status: "ok",
Expand Down Expand Up @@ -301,6 +322,7 @@ func AuthHandlerMiddleware(handler http.HandlerFunc) http.HandlerFunc {
// /events -> Add
// /proofs/membership -> Membership
func NewApiHttp(balloon raftwal.RaftBalloonApi) *http.ServeMux {
metrics.Register("healthcheck_ops_total", cOpsProcessed)

api := http.NewServeMux()
api.HandleFunc("/health-check", AuthHandlerMiddleware(HealthCheckHandler))
Expand Down
13 changes: 13 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"net/http"
_ "net/http/pprof" // this will enable the default profiling capabilities
"os"
"time"

"github.com/bbva/qed/api/apihttp"
"github.com/bbva/qed/api/mgmthttp"
Expand All @@ -43,7 +44,10 @@ import (
"github.com/bbva/qed/sign"
"github.com/bbva/qed/storage/badger"
"github.com/bbva/qed/util"
metricsprom "github.com/deathowl/go-metrics-prometheus"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rcrowley/go-metrics"
)

// Server encapsulates the data and login to start/stop a QED server
Expand Down Expand Up @@ -199,6 +203,15 @@ func (s *Server) Start() error {
}

if s.conf.EnableMetrics {
pClient := metricsprom.NewPrometheusProvider(
metrics.DefaultRegistry,
"qed",
"crowley",
prometheus.DefaultRegisterer,
1*time.Second,
)
go pClient.UpdatePrometheusMetrics()

go func() {
log.Debugf(" * Starting metrics HTTP server in addr: localhost:9990")
if err := http.ListenAndServe("localhost:9990", nil); err != http.ErrServerClosed {
Expand Down

0 comments on commit 275b69d

Please sign in to comment.