2021-06-04 16:38:49 +02:00
|
|
|
package metrics
|
2021-05-18 16:12:51 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2021-06-04 16:38:49 +02:00
|
|
|
"github.com/mattermost/focalboard/server/services/mlog"
|
2021-05-18 16:12:51 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
|
|
)
|
|
|
|
|
2021-06-21 11:21:42 +02:00
|
|
|
// Service prometheus to run the server.
|
2021-05-18 16:12:51 +02:00
|
|
|
type Service struct {
|
|
|
|
*http.Server
|
|
|
|
}
|
|
|
|
|
2021-06-21 11:21:42 +02:00
|
|
|
// NewMetricsServer factory method to create a new prometheus server.
|
2021-06-04 16:38:49 +02:00
|
|
|
func NewMetricsServer(address string, metricsService *Metrics, logger *mlog.Logger) *Service {
|
2021-05-18 16:12:51 +02:00
|
|
|
return &Service{
|
|
|
|
&http.Server{
|
2021-06-04 16:38:49 +02:00
|
|
|
Addr: address,
|
|
|
|
Handler: promhttp.HandlerFor(metricsService.registry, promhttp.HandlerOpts{
|
|
|
|
ErrorLog: logger.StdLogger(mlog.Error),
|
|
|
|
}),
|
2021-05-18 16:12:51 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-21 11:21:42 +02:00
|
|
|
// Run will start the prometheus server.
|
2021-05-18 16:12:51 +02:00
|
|
|
func (h *Service) Run() error {
|
|
|
|
return errors.Wrap(h.Server.ListenAndServe(), "prometheus ListenAndServe")
|
|
|
|
}
|
|
|
|
|
2021-06-21 11:21:42 +02:00
|
|
|
// Shutdown will shutdown the prometheus server.
|
2021-05-18 16:12:51 +02:00
|
|
|
func (h *Service) Shutdown() error {
|
|
|
|
return errors.Wrap(h.Server.Close(), "prometheus Close")
|
|
|
|
}
|