-
Notifications
You must be signed in to change notification settings - Fork 1
/
metrics_server.go
60 lines (50 loc) · 1.47 KB
/
metrics_server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package server
import (
"context"
"github.com/ozonva/ova-journey-api/internal/config"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rs/zerolog/log"
"net/http"
"sync"
)
// MetricsServer - represents simple http server wrapper for prometheus metrics
type MetricsServer struct {
prometheusConfiguration *config.PrometheusConfiguration
httpServer *http.Server
wg *sync.WaitGroup
}
// NewMetricsServer - creates new MetricsServer with configuration parameters
func NewMetricsServer(prometheusConfiguration *config.PrometheusConfiguration) *MetricsServer {
return &MetricsServer{
prometheusConfiguration: prometheusConfiguration,
}
}
// Start - start MetricsServer
func (s *MetricsServer) Start() {
mux := http.NewServeMux()
mux.Handle(s.prometheusConfiguration.Path, promhttp.Handler())
s.httpServer = &http.Server{
Addr: s.prometheusConfiguration.GetEndpointAddress(),
Handler: mux,
}
s.wg = &sync.WaitGroup{}
go func() {
log.Debug().Msg("Metrics server: starting")
s.wg.Add(1)
err := s.httpServer.ListenAndServe()
if err == http.ErrServerClosed {
s.wg.Done()
return
}
if err != nil {
log.Err(err).Msg("Metrics server: failed to serve")
}
}()
}
// Stop - graceful stop MetricsServer with waiting of http server is stopped
func (s *MetricsServer) Stop() {
if err := s.httpServer.Shutdown(context.Background()); err != nil {
log.Err(err).Msg("Metrics server: shutdown failed")
}
s.wg.Wait()
}