-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
93 lines (77 loc) · 2.17 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"context"
"encoding/json"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
log "github.com/sirupsen/logrus"
)
func main() {
conf := loadConfig()
awsSession := session.Must(session.NewSession(&aws.Config{
Region: aws.String(conf.AWSRegionID),
}))
gs := mustNewGlueStore(
conf.Tenants,
conf.SSMPrefix,
conf.AWSAccountID,
conf.AWSRegionID,
awsSession,
)
for _, m := range conf.Metrics {
recorder := newMetricRecorder(gs, m)
go recorder.startRecording()
}
ep := newServer(conf.ListenAddress, getRoutes())
go func() { panicOnErr(ep.ListenAndServe(), "endpoint failed") }()
log.WithField("core.addr", conf.ListenAddress).Info("started endpoint")
ShutdownOnSignal(ep)
}
// ShutdownOnSignal sets up OS signal listeners and gracefully shuts down servers passed in
func ShutdownOnSignal(servers ...*http.Server) {
ch := make(chan os.Signal)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
sig := <-ch
log.Infof("Server got signal %s to shutdown, doing so...", sig)
for _, server := range servers {
if err := server.Shutdown(context.Background()); err != nil {
log.WithError(err).Error("unable to gracefully shutdown server")
}
}
}
func getRoutes() *http.ServeMux {
mux := http.NewServeMux()
mux.Handle("/healthcheck", createHealthCheckEndpoint())
mux.Handle("/metrics", createMetricsEndpoint())
return mux
}
func createHealthCheckEndpoint() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
json, err := json.Marshal(map[string]string{"status": "healthy"})
var request []byte
_, err = r.Body.Read(request)
if err != nil {
log.WithError(err).Error("Failed to read body")
w.WriteHeader(http.StatusOK)
w.Write([]byte("ok"))
}
if err != nil {
log.WithField("core.error", err).
WithField("core.request", string(request)).
Error("Unable to marshal healthcheck")
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(json))
})
}
func panicOnErr(err error, msg string, args ...interface{}) {
if err != nil {
log.WithError(err).Panicf(msg, args...)
}
}