-
Notifications
You must be signed in to change notification settings - Fork 7
/
exporter.go
90 lines (76 loc) · 2.34 KB
/
exporter.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"net/http"
"strconv"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/version"
"github.com/sirupsen/logrus"
)
const exporterName = "netns_exporter"
func init() { //nolint:gochecknoinits
prometheus.MustRegister(version.NewCollector(exporterName))
}
type APIServer struct {
config *NetnsExporterConfig
server *http.Server
logger logrus.FieldLogger
}
func NewAPIServer(config *NetnsExporterConfig, logger *logrus.Logger) (*APIServer, error) {
apiServer := APIServer{
config: config,
logger: logger.WithField("component", "api-server"),
}
// Try to register new Prometheus exporter.
err := prometheus.Register(NewCollector(config, logger))
if err != nil {
apiServer.logger.Errorf("Registering netns exporter failed: %s", err)
return nil, err
}
// Configure and start HTTP server.
httpMux := http.NewServeMux()
timeout := time.Duration(config.APIServer.RequestTimeout) * time.Second
address := strings.Join([]string{
config.APIServer.ServerAddress,
strconv.Itoa(config.APIServer.ServerPort),
}, ":")
apiServer.server = &http.Server{
Addr: address,
Handler: httpMux,
ReadHeaderTimeout: timeout,
WriteTimeout: timeout,
IdleTimeout: timeout,
}
httpMux.HandleFunc("/", apiServer.indexPage)
httpMux.Handle(config.APIServer.TelemetryPath, apiServer.middlewareLogging(promhttp.Handler()))
return &apiServer, nil
}
// StartAPIServer starts Exporter's HTTP server.
func (s *APIServer) Start() error {
s.logger.Infof("Starting API server on %s", s.server.Addr)
return s.server.ListenAndServe()
}
func (s *APIServer) middlewareLogging(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
s.logger.WithFields(logrus.Fields{
"addr": r.RemoteAddr,
"method": r.Method,
"agent": r.UserAgent(),
}).Debugf("%s", r.URL.Path)
next.ServeHTTP(w, r)
})
}
func (s *APIServer) indexPage(w http.ResponseWriter, _ *http.Request) {
_, err := w.Write([]byte(`<html>
<head><title>Network nemespace Exporter</title></head>
<body>
<h1>Network nemespace Exporter</h1>
<p><a href='` + s.config.APIServer.TelemetryPath + `'>Metrics</a></p>
</body>
</html>`))
if err != nil {
s.logger.Errorf("error handling index page: %s", err)
}
}