tracker/pkg/prometheus/server.go

49 lines
1,001 B
Go
Raw Normal View History

2017-04-30 04:28:44 +02:00
// Package prometheus implements a standalone HTTP server for serving a
// Prometheus metrics endpoint.
package prometheus
import (
"context"
"net/http"
2018-10-23 20:06:52 +02:00
"github.com/prometheus/client_golang/prometheus"
2017-06-20 14:58:44 +02:00
"github.com/chihaya/chihaya/pkg/log"
"github.com/chihaya/chihaya/pkg/stop"
2017-04-30 04:28:44 +02:00
)
// Server represents a standalone HTTP server for serving a Prometheus metrics
// endpoint.
type Server struct {
srv *http.Server
}
// Stop shuts down the server.
func (s *Server) Stop() stop.Result {
c := make(stop.Channel)
2017-04-30 04:28:44 +02:00
go func() {
c.Done(s.srv.Shutdown(context.Background()))
2017-04-30 04:28:44 +02:00
}()
return c.Result()
2017-04-30 04:28:44 +02:00
}
// NewServer creates a new instance of a Prometheus server that asynchronously
// serves requests.
func NewServer(addr string) *Server {
s := &Server{
srv: &http.Server{
Addr: addr,
Handler: prometheus.Handler(),
},
}
go func() {
if err := s.srv.ListenAndServe(); err != http.ErrServerClosed {
2017-06-20 14:58:44 +02:00
log.Fatal("failed while serving prometheus", log.Err(err))
2017-04-30 04:28:44 +02:00
}
}()
return s
}