-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
141 lines (118 loc) · 3.32 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"context"
"fmt"
stdlog "log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/metalmatze/signal/healthcheck"
"github.com/metalmatze/signal/internalserver"
rulesspec "github.com/observatorium/api/rules"
"github.com/oklog/run"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/thanos-io/thanos/pkg/objstore/client"
"github.com/observatorium/rules-objstore/pkg/config"
"github.com/observatorium/rules-objstore/pkg/server"
)
func runRulesObjstore() error {
cfg, err := config.ParseFlags()
if err != nil {
return fmt.Errorf("parsing config: %w", err)
}
logger := log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
if cfg.LogFormat == "json" {
logger = log.NewJSONLogger(log.NewSyncWriter(os.Stderr))
}
logger = level.NewFilter(logger, cfg.LogLevel)
if cfg.Name != "" {
logger = log.With(logger, "name", cfg.Name)
}
logger = log.With(logger, "ts", log.DefaultTimestampUTC, "caller", log.DefaultCaller)
defer level.Info(logger).Log("msg", "exiting")
reg := prometheus.NewRegistry()
reg.MustRegister(
collectors.NewGoCollector(),
//nolint:exhaustivestruct
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
)
healthchecks := healthcheck.NewMetricsHandler(healthcheck.NewHandler(), reg)
if cfg.Server.HealthcheckURL != "" {
// Checks if server is up.
healthchecks.AddLivenessCheck("http",
healthcheck.HTTPCheck(
cfg.Server.HealthcheckURL,
http.MethodGet,
http.StatusNotFound,
time.Second,
),
)
}
bkt, err := client.NewBucket(logger, cfg.BucketConfig, reg, cfg.Name)
if err != nil {
return fmt.Errorf("creating object store bucket client: %w", err)
}
level.Info(logger).Log("msg", "starting rules-objstore")
var g run.Group
{
// Signal channels must be buffered.
sig := make(chan os.Signal, 1)
g.Add(func() error {
signal.Notify(sig, os.Interrupt, syscall.SIGTERM)
<-sig
level.Info(logger).Log("msg", "caught interrupt")
return nil
}, func(_ error) {
close(sig)
})
}
{
//nolint:exhaustivestruct
s := http.Server{
Addr: cfg.Server.Listen,
Handler: rulesspec.Handler(
server.NewServer(bkt, log.With(logger, "component", "server"), reg),
),
}
g.Add(func() error {
level.Info(logger).Log("msg", "starting the HTTP server", "address", cfg.Server.Listen)
return s.ListenAndServe() //nolint:wrapcheck
}, func(_ error) {
level.Info(logger).Log("msg", "shutting down the HTTP server")
_ = s.Shutdown(context.Background())
})
}
{
h := internalserver.NewHandler(
internalserver.WithName("Internal - rules-objstore API"),
internalserver.WithHealthchecks(healthchecks),
internalserver.WithPrometheusRegistry(reg),
internalserver.WithPProf(),
)
//nolint:exhaustivestruct
s := http.Server{
Addr: cfg.Server.ListenInternal,
Handler: h,
}
g.Add(func() error {
level.Info(logger).Log("msg", "starting internal HTTP server", "address", s.Addr)
return s.ListenAndServe() //nolint:wrapcheck
}, func(_ error) {
_ = s.Shutdown(context.Background())
})
}
if err := g.Run(); err != nil {
return fmt.Errorf("starting run group: %w", err)
}
return nil
}
func main() {
if err := runRulesObjstore(); err != nil {
stdlog.Fatal(err)
}
}