-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
65 lines (50 loc) · 1.93 KB
/
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
61
62
63
64
65
package aleh
import (
"context"
"net/http"
"os"
"github.com/gojuno/aleh/collectors"
"github.com/gojuno/aleh/storages"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type Config struct {
DockerDaemonSocket string `edn:"docker_daemon_socket"`
Endpoint string `edn:"endpoint"`
MetricPrefix string `edn:"metric_prefix"`
Services map[string]map[string]collectors.ContainerInfo `edn:"services"`
}
// Server implements net/http.Handler
// It registers all needed prometheus collectors
// and handles http GET /metrics for prometheus
// and http GET /internal for debug purposes
type Server struct {
mux *http.ServeMux
}
func New(ctx context.Context, c Config) *Server {
s := &Server{mux: http.NewServeMux()}
containerListener := storages.New(ctx, c.DockerDaemonSocket)
// cpu
if v := os.Getenv("CPU_STATS"); v == "true" {
cpuStatCollector := collectors.NewCPUCollector(c.MetricPrefix, containerListener)
prometheus.MustRegister(cpuStatCollector)
}
// mem
memStatCollector := collectors.NewMemCollector(c.MetricPrefix, containerListener)
prometheus.MustRegister(memStatCollector)
// alive
aliveCollector := collectors.NewAliveCollector(c.MetricPrefix, containerListener, c.Services)
prometheus.MustRegister(aliveCollector)
// restarts
restartCollector := collectors.NewRestartCollector(c.MetricPrefix, containerListener)
prometheus.MustRegister(restartCollector)
// docker space
spaceCollector := collectors.NewDockerSpaceCollector(c.MetricPrefix, c.DockerDaemonSocket)
prometheus.MustRegister(spaceCollector)
s.mux.Handle("/metrics", promhttp.Handler())
s.mux.HandleFunc("/internal", containerListener.HttpHandler())
return s
}
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.mux.ServeHTTP(w, r)
}