2018-11-15 12:54:45 +00:00
|
|
|
package prometheus
|
|
|
|
|
|
|
|
import (
|
2018-12-30 21:20:43 +00:00
|
|
|
"bytes"
|
2018-11-15 12:54:45 +00:00
|
|
|
"context"
|
2018-12-30 21:20:43 +00:00
|
|
|
"fmt"
|
2018-11-15 12:54:45 +00:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2018-12-30 21:20:43 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared"
|
2018-11-15 12:54:45 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
var log = logrus.WithField("prefix", "prometheus")
|
|
|
|
|
|
|
|
// Service provides Prometheus metrics via the /metrics route. This route will
|
|
|
|
// show all the metrics registered with the Prometheus DefaultRegisterer.
|
|
|
|
type Service struct {
|
2018-12-30 21:20:43 +00:00
|
|
|
server *http.Server
|
|
|
|
svcRegistry *shared.ServiceRegistry
|
2018-11-15 12:54:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewPrometheusService sets up a new instance for a given address host:port.
|
|
|
|
// An empty host will match with any IP so an address like ":2121" is perfectly acceptable.
|
2018-12-30 21:20:43 +00:00
|
|
|
func NewPrometheusService(addr string, svcRegistry *shared.ServiceRegistry) *Service {
|
|
|
|
s := &Service{svcRegistry: svcRegistry}
|
|
|
|
|
2018-11-15 12:54:45 +00:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.Handle("/metrics", promhttp.Handler())
|
2018-12-30 21:20:43 +00:00
|
|
|
mux.HandleFunc("/healthz", s.healthzHandler)
|
|
|
|
|
|
|
|
s.server = &http.Server{Addr: addr, Handler: mux}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) healthzHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Call all services in the registry.
|
|
|
|
// if any are not OK, write 500
|
|
|
|
// print the statuses of all services.
|
2018-11-15 12:54:45 +00:00
|
|
|
|
2018-12-30 21:20:43 +00:00
|
|
|
statuses := s.svcRegistry.Statuses()
|
|
|
|
hasError := false
|
|
|
|
var buf bytes.Buffer
|
|
|
|
for k, v := range statuses {
|
|
|
|
var status string
|
|
|
|
if v == nil {
|
|
|
|
status = "OK"
|
|
|
|
} else {
|
|
|
|
hasError = true
|
|
|
|
status = "ERROR " + v.Error()
|
|
|
|
}
|
2019-01-05 03:58:19 +00:00
|
|
|
if _, err := buf.WriteString(fmt.Sprintf("%s: %s\n", k, status)); err != nil {
|
|
|
|
hasError = true
|
|
|
|
status = "ERROR " + err.Error()
|
|
|
|
}
|
2018-11-15 12:54:45 +00:00
|
|
|
}
|
2018-12-30 21:20:43 +00:00
|
|
|
|
|
|
|
// Write status header
|
|
|
|
if hasError {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
} else {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write http body
|
|
|
|
w.Write(buf.Bytes())
|
2018-11-15 12:54:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start the prometheus service.
|
|
|
|
func (s *Service) Start() {
|
|
|
|
log.WithField("endpoint", s.server.Addr).Info("Starting service")
|
|
|
|
go func() {
|
|
|
|
err := s.server.ListenAndServe()
|
|
|
|
if err != nil && err != http.ErrServerClosed {
|
|
|
|
log.Errorf("Could not listen to host:port :%s: %v", s.server.Addr, err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop the service gracefully.
|
|
|
|
func (s *Service) Stop() error {
|
|
|
|
log.Info("Stopping service")
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
return s.server.Shutdown(ctx)
|
|
|
|
}
|
2018-12-30 21:20:43 +00:00
|
|
|
|
|
|
|
// Status always returns nil.
|
|
|
|
// TODO(1207): Add service health checks.
|
|
|
|
func (s *Service) Status() error {
|
|
|
|
return nil
|
|
|
|
}
|