-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.go
107 lines (99 loc) · 2.3 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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/oklog/run"
"github.com/cbrgm/promcheck/promcheck/metrics"
)
func (app *promcheckApp) runPromcheckExporter() error {
ctx, cancel := context.WithCancel(context.Background())
var gr run.Group
// http server
{
httpLogger := log.With(app.logger, "component", "exporter")
m := http.NewServeMux()
handleHealth := func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
}
m.HandleFunc("/health", handleHealth)
m.HandleFunc("/healthz", handleHealth)
m.Handle("/metrics", metrics.HandlerFor(app.metrics, metrics.Options{
Prefix: app.optExporterMetricsPrefix,
EnableProfile: app.optExporterEnableProfiling,
EnableRuntimeMetrics: app.optExporterEnableRuntimeMetrics,
PrometheusRegistry: nil,
}))
s := http.Server{
Addr: app.optExporterHTTPAddr,
Handler: m,
}
m.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(`<html>
<head><title>Promcheck Exporter</title></head>
<body>
<h1>promcheckApp Exporter</h1>
<p><a href="` + app.optExporterMetricsPrefix + `">see metrics</a></p>
</body>
</html>`))
})
gr.Add(func() error {
// nolint: errcheck
level.Info(httpLogger).Log(
"msg", "running http server",
"addr", s.Addr,
)
return s.ListenAndServe()
}, func(_ error) {
_ = s.Shutdown(context.TODO())
})
}
// promcheck
{
tick := time.NewTicker(app.optExporterInterval)
defer tick.Stop()
gr.Add(func() error {
for {
select {
case <-ctx.Done():
return nil
case <-tick.C:
// nolint: errcheck
level.Info(app.logger).Log(
"msg", "executing promcheck routine",
)
if err := app.checkRules(); err != nil {
return err
}
}
}
}, func(err error) {
// nolint: errcheck
level.Info(app.logger).Log(
"msg", "error while executing promcheck routine",
"err", err,
)
})
}
{
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, syscall.SIGTERM)
gr.Add(func() error {
<-sig
return nil
}, func(_ error) {
cancel()
close(sig)
})
}
if err := gr.Run(); err != nil {
return fmt.Errorf("error running: %w", err)
}
return nil
}