forked from thanos-io/thanos
-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.go
226 lines (196 loc) · 6.55 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.
package main
import (
"context"
"fmt"
"io"
"os"
"os/signal"
"path/filepath"
"regexp"
"runtime"
"runtime/debug"
"syscall"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/oklog/run"
"github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
versioncollector "github.com/prometheus/client_golang/prometheus/collectors/version"
"github.com/prometheus/common/version"
"go.uber.org/automaxprocs/maxprocs"
"gopkg.in/alecthomas/kingpin.v2"
"github.com/thanos-io/thanos/pkg/extkingpin"
"github.com/thanos-io/thanos/pkg/logging"
"github.com/thanos-io/thanos/pkg/tracing/client"
)
func main() {
// We use mmaped resources in most of the components so hardcode PanicOnFault to true. This allows us to recover (if we can e.g if queries
// are temporarily accessing unmapped memory).
debug.SetPanicOnFault(true)
if os.Getenv("DEBUG") != "" {
runtime.SetMutexProfileFraction(10)
runtime.SetBlockProfileRate(10)
}
app := extkingpin.NewApp(kingpin.New(filepath.Base(os.Args[0]), "A block storage based long-term storage for Prometheus.").Version(version.Print("thanos")))
debugName := app.Flag("debug.name", "Name to add as prefix to log lines.").Hidden().String()
logLevel := app.Flag("log.level", "Log filtering level.").
Default("info").Enum("error", "warn", "info", "debug")
logFormat := app.Flag("log.format", "Log format to use. Possible options: logfmt or json.").
Default(logging.LogFormatLogfmt).Enum(logging.LogFormatLogfmt, logging.LogFormatJSON)
tracingConfig := extkingpin.RegisterCommonTracingFlags(app)
goMemLimitConf := goMemLimitConfig{}
goMemLimitConf.registerFlag(app)
registerSidecar(app)
registerStore(app)
registerQuery(app)
registerRule(app)
registerCompact(app)
registerTools(app)
registerReceive(app)
registerQueryFrontend(app)
cmd, setup := app.Parse()
logger := logging.NewLogger(*logLevel, *logFormat, *debugName)
if err := configureGoAutoMemLimit(goMemLimitConf); err != nil {
level.Error(logger).Log("msg", "failed to configure Go runtime memory limits", "err", err)
os.Exit(1)
}
// Running in container with limits but with empty/wrong value of GOMAXPROCS env var could lead to throttling by cpu
// maxprocs will automate adjustment by using cgroups info about cpu limit if it set as value for runtime.GOMAXPROCS.
undo, err := maxprocs.Set(maxprocs.Logger(func(template string, args ...interface{}) {
level.Debug(logger).Log("msg", fmt.Sprintf(template, args))
}))
defer undo()
if err != nil {
level.Warn(logger).Log("warn", errors.Wrapf(err, "failed to set GOMAXPROCS: %v", err))
}
metrics := prometheus.NewRegistry()
metrics.MustRegister(
versioncollector.NewCollector("thanos"),
collectors.NewGoCollector(
collectors.WithGoCollectorRuntimeMetrics(collectors.GoRuntimeMetricsRule{Matcher: regexp.MustCompile("/.*")}),
),
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
)
// Some packages still use default Register. Replace to have those metrics.
prometheus.DefaultRegisterer = metrics
var g run.Group
var tracer opentracing.Tracer
// Setup optional tracing.
{
var (
ctx = context.Background()
closer io.Closer
confContentYaml []byte
)
confContentYaml, err = tracingConfig.Content()
if err != nil {
level.Error(logger).Log("msg", "getting tracing config failed", "err", err)
os.Exit(1)
}
if len(confContentYaml) == 0 {
tracer = client.NoopTracer()
} else {
tracer, closer, err = client.NewTracer(ctx, logger, metrics, confContentYaml)
if err != nil {
fmt.Fprintln(os.Stderr, errors.Wrapf(err, "tracing failed"))
os.Exit(1)
}
}
// This is bad, but Prometheus does not support any other tracer injections than just global one.
// TODO(bplotka): Work with basictracer to handle gracefully tracker mismatches, and also with Prometheus to allow
// tracer injection.
opentracing.SetGlobalTracer(tracer)
ctx, cancel := context.WithCancel(ctx)
g.Add(func() error {
<-ctx.Done()
return ctx.Err()
}, func(error) {
if closer != nil {
if err := closer.Close(); err != nil {
level.Warn(logger).Log("msg", "closing tracer failed", "err", err)
}
}
cancel()
})
}
// Create a signal channel to dispatch reload events to sub-commands.
reloadCh := make(chan struct{}, 1)
if err := setup(&g, logger, metrics, tracer, reloadCh, *logLevel == "debug"); err != nil {
// Use %+v for github.com/pkg/errors error to print with stack.
level.Error(logger).Log("err", fmt.Sprintf("%+v", errors.Wrapf(err, "preparing %s command failed", cmd)))
os.Exit(1)
}
// Listen for termination signals.
{
cancel := make(chan struct{})
g.Add(func() error {
return interrupt(logger, cancel)
}, func(error) {
close(cancel)
})
}
// Listen for reload signals.
{
cancel := make(chan struct{})
g.Add(func() error {
return reload(logger, cancel, reloadCh)
}, func(error) {
close(cancel)
})
}
if err := g.Run(); err != nil {
// Use %+v for github.com/pkg/errors error to print with stack.
level.Error(logger).Log("err", fmt.Sprintf("%+v", errors.Wrapf(err, "%s command failed", cmd)))
os.Exit(1)
}
level.Info(logger).Log("msg", "exiting")
}
func interrupt(logger log.Logger, cancel <-chan struct{}) error {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
select {
case s := <-c:
level.Info(logger).Log("msg", "caught signal. Exiting.", "signal", s)
return nil
case <-cancel:
return errors.New("canceled")
}
}
func reload(logger log.Logger, cancel <-chan struct{}, r chan<- struct{}) error {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGHUP)
for {
select {
case s := <-c:
level.Info(logger).Log("msg", "caught signal. Reloading.", "signal", s)
select {
case r <- struct{}{}:
level.Info(logger).Log("msg", "reload dispatched.")
default:
}
case <-cancel:
return errors.New("canceled")
}
}
}
func getFlagsMap(flags []*kingpin.FlagModel) map[string]string {
flagsMap := map[string]string{}
// Exclude kingpin default flags to expose only Thanos ones.
boilerplateFlags := kingpin.New("", "").Version("")
for _, f := range flags {
if boilerplateFlags.GetFlag(f.Name) != nil {
continue
}
// Mask inline objstore flag which can have credentials.
if f.Name == "objstore.config" || f.Name == "objstore.config-file" {
flagsMap[f.Name] = "<REDACTED>"
continue
}
flagsMap[f.Name] = f.Value.String()
}
return flagsMap
}