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"
|
2019-04-06 00:19:27 +00:00
|
|
|
"runtime/debug"
|
|
|
|
"runtime/pprof"
|
2018-11-15 12:54:45 +00:00
|
|
|
"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
|
2019-01-24 01:26:25 +00:00
|
|
|
failStatus error
|
2018-11-15 12:54:45 +00:00
|
|
|
}
|
|
|
|
|
2019-09-03 16:07:40 +00:00
|
|
|
// Handler represents a path and handler func to serve on the same port as /metrics, /healthz, /goroutinez, etc.
|
|
|
|
type Handler struct {
|
|
|
|
Path string
|
|
|
|
Handler func(http.ResponseWriter, *http.Request)
|
|
|
|
}
|
|
|
|
|
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.
|
2019-09-03 16:07:40 +00:00
|
|
|
func NewPrometheusService(addr string, svcRegistry *shared.ServiceRegistry, additionalHandlers ...Handler) *Service {
|
2018-12-30 21:20:43 +00:00
|
|
|
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)
|
2019-04-06 00:19:27 +00:00
|
|
|
mux.HandleFunc("/goroutinez", s.goroutinezHandler)
|
2018-12-30 21:20:43 +00:00
|
|
|
|
2019-09-03 16:07:40 +00:00
|
|
|
// Register additional handlers.
|
|
|
|
for _, h := range additionalHandlers {
|
|
|
|
mux.HandleFunc(h.Path, h.Handler)
|
|
|
|
}
|
|
|
|
|
2018-12-30 21:20:43 +00:00
|
|
|
s.server = &http.Server{Addr: addr, Handler: mux}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2019-01-06 03:12:59 +00:00
|
|
|
func (s *Service) healthzHandler(w http.ResponseWriter, _ *http.Request) {
|
2018-12-30 21:20:43 +00:00
|
|
|
// 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-06 03:12:59 +00:00
|
|
|
|
2019-01-05 03:58:19 +00:00
|
|
|
if _, err := buf.WriteString(fmt.Sprintf("%s: %s\n", k, status)); err != nil {
|
|
|
|
hasError = true
|
|
|
|
}
|
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
|
2019-01-06 03:12:59 +00:00
|
|
|
if _, err := w.Write(buf.Bytes()); err != nil {
|
|
|
|
log.Errorf("Could not write healthz body %v", err)
|
|
|
|
}
|
2018-11-15 12:54:45 +00:00
|
|
|
}
|
|
|
|
|
2019-04-09 01:57:09 +00:00
|
|
|
func (s *Service) goroutinezHandler(w http.ResponseWriter, _ *http.Request) {
|
2019-04-06 00:19:27 +00:00
|
|
|
stack := debug.Stack()
|
2019-04-09 01:57:09 +00:00
|
|
|
// #nosec G104
|
2019-04-06 00:19:27 +00:00
|
|
|
w.Write(stack)
|
2019-04-09 01:57:09 +00:00
|
|
|
// #nosec G104
|
2019-04-06 00:19:27 +00:00
|
|
|
pprof.Lookup("goroutine").WriteTo(w, 2)
|
|
|
|
}
|
|
|
|
|
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)
|
2019-01-24 01:26:25 +00:00
|
|
|
s.failStatus = err
|
2018-11-15 12:54:45 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2019-01-24 01:26:25 +00:00
|
|
|
// Status checks for any service failure conditions.
|
2018-12-30 21:20:43 +00:00
|
|
|
func (s *Service) Status() error {
|
2019-01-24 01:26:25 +00:00
|
|
|
if s.failStatus != nil {
|
|
|
|
return s.failStatus
|
|
|
|
}
|
2018-12-30 21:20:43 +00:00
|
|
|
return nil
|
|
|
|
}
|