2021-06-04 16:38:49 +02:00
|
|
|
package metrics
|
2021-05-18 16:12:51 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2021-08-25 22:08:01 +02:00
|
|
|
|
|
|
|
"github.com/mattermost/mattermost-server/v6/shared/mlog"
|
2021-05-18 16:12:51 +02:00
|
|
|
)
|
|
|
|
|
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.
|
2022-07-15 08:04:35 +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{
|
2021-08-25 22:08:01 +02:00
|
|
|
ErrorLog: logger.StdLogger(mlog.LvlError),
|
2021-06-04 16:38:49 +02:00
|
|
|
}),
|
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")
|
|
|
|
}
|