go-waku/waku/metrics/http.go

59 lines
1.3 KiB
Go
Raw Normal View History

package metrics
import (
"context"
"fmt"
"net/http"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.opencensus.io/plugin/ochttp"
"go.uber.org/zap"
)
// Server runs and controls a HTTP pprof interface.
type Server struct {
server *http.Server
log *zap.Logger
}
2021-10-09 18:18:53 +00:00
// NewMetricsServer creates a prometheus server on a particular interface and port
func NewMetricsServer(address string, port int, log *zap.Logger) *Server {
p := Server{
log: log.Named("metrics"),
}
p.log.Info("starting server", zap.String("address", address), zap.Int("port", port))
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.Handler())
// Healthcheck
mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "OK")
})
h := &ochttp.Handler{Handler: mux}
p.server = &http.Server{
Addr: fmt.Sprintf("%s:%d", address, port),
Handler: h,
}
2021-10-16 21:50:49 +00:00
return &p
}
2021-10-09 18:18:53 +00:00
// Start executes the HTTP server in the background.
func (p *Server) Start() {
p.log.Info("server started ", zap.Error(p.server.ListenAndServe()))
}
2021-10-09 18:18:53 +00:00
// Stop shuts down the prometheus server
2021-10-18 12:43:17 +00:00
func (p *Server) Stop(ctx context.Context) error {
err := p.server.Shutdown(ctx)
if err != nil {
p.log.Error("stopping server", zap.Error(err))
2021-10-18 12:43:17 +00:00
return err
}
2021-10-18 12:43:17 +00:00
return nil
}