/
server.go
46 lines (38 loc) · 950 Bytes
/
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
package metric
import (
"errors"
"net/http"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/zap"
)
// Config of metric servers.
type Config struct {
Address string `koanf:"address"`
Enabled bool `koanf:"enabled"`
}
// Server contains information about metrics server.
type Server struct {
srv *http.ServeMux
address string
}
// NewServer creates a new monitoring server.
func NewServer(cfg Config) Server {
var srv *http.ServeMux
if cfg.Enabled {
srv = http.NewServeMux()
srv.Handle("/metrics", promhttp.Handler())
}
return Server{
address: cfg.Address,
srv: srv,
}
}
// Start creates and run a metric server for prometheus in new go routine.
func (s Server) Start(logger *zap.Logger) {
go func() {
// nolint: gosec
if err := http.ListenAndServe(s.address, s.srv); !errors.Is(err, http.ErrServerClosed) {
logger.Error("metric server initiation failed", zap.Error(err))
}
}()
}