-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
76 lines (67 loc) · 1.66 KB
/
main.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
package main
import (
"context"
"errors"
"log/slog"
"net/http"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func main() {
rMetrics := mux.NewRouter()
rMetrics.Handle("/metrics", promhttp.Handler())
srvMetrics := &http.Server{
Addr: ":9145",
Handler: http.TimeoutHandler(rMetrics, 10*time.Second, "Server Timeout"),
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
var waitGroup sync.WaitGroup
waitGroup.Add(2)
// Run our server in a goroutine so that it doesn't block.
go func() {
defer waitGroup.Done()
if err := srvMetrics.ListenAndServe(); err != nil {
if errors.Is(err, http.ErrServerClosed) {
slog.Info("Metrics server closed")
} else {
errorf("Failed to start metrics server: %v", err)
}
}
}()
// Collect S3 Metrics in a goroutine
s3wCollectCtx, s3wCollectCancel := context.WithCancel(context.Background())
go func() {
defer waitGroup.Done()
s3w, err := initS3Exporter()
if err != nil {
errorf("Failed to get exporters from json file: %v", err)
return
}
tck := time.NewTicker(time.Duration(s3w.Settings.CheckInterval) * time.Second)
defer tck.Stop()
s3w.Collect() //nolint:contextcheck
for {
select {
case <-tck.C:
s3w.Collect() //nolint:contextcheck
case <-s3wCollectCtx.Done():
return
}
}
}()
// Graceful shutdown, inspired by https://github.com/gorilla/mux#graceful-shutdown
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGTERM)
<-c
s3wCollectCancel()
_ = srvMetrics.Shutdown(context.Background())
slog.Info("Shutting down")
waitGroup.Wait()
os.Exit(0)
}