This repository has been archived by the owner on Oct 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
241 lines (204 loc) · 6.92 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Copyright 2018 The conprof Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"fmt"
"net/http"
"os"
"path/filepath"
"runtime"
"syscall"
"time"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/oklog/run"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/version"
"github.com/thanos-io/thanos/pkg/component"
"github.com/thanos-io/thanos/pkg/extkingpin"
"github.com/thanos-io/thanos/pkg/prober"
httpserver "github.com/thanos-io/thanos/pkg/server/http"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp"
"go.opentelemetry.io/otel/exporters/otlp/otlpgrpc"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/semconv"
"gopkg.in/alecthomas/kingpin.v2"
"github.com/conprof/conprof/config"
)
const (
logFormatLogfmt = "logfmt"
logFormatJSON = "json"
)
type httpMux interface {
Handle(pattern string, handler http.Handler)
}
type setupFunc func(component.Component, *run.Group, httpMux, prober.Probe, log.Logger, *prometheus.Registry, bool) (prober.Probe, error)
type configReloaders struct {
funcs []func(*config.Config) error
}
func (r *configReloaders) Register(reloader func(*config.Config) error) {
r.funcs = append(r.funcs, reloader)
}
func main() {
if os.Getenv("DEBUG") != "" {
runtime.SetMutexProfileFraction(10)
runtime.SetBlockProfileRate(10)
}
app := kingpin.New(filepath.Base(os.Args[0]), "Continuous profiling - to have a profile when it matters.")
app.Version(version.Print("conprof"))
app.HelpFlag.Short('h')
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.").
Default(logFormatLogfmt).Enum(logFormatLogfmt, logFormatJSON)
otlpAddress := app.Flag("otlp-address", "OpenTelemetry collector address to send traces to.").
Default("").String()
corsOrigin := app.Flag("cors.access-control-allow-origin", "Cross-origin resource sharing allowed origins.").
Default("").String()
corsMethods := app.Flag("cors.access-control-allow-methods", "Cross-origin resource sharing allowed methods.").
Default("").String()
httpBindAddr, httpGracePeriod, _ := extkingpin.RegisterHTTPFlags(app)
cmds := map[string]setupFunc{}
reloadCh := make(chan struct{}, 1)
reloaders := &configReloaders{}
registerSampler(cmds, app, "sampler", reloadCh, reloaders)
registerStorage(cmds, app, "storage", reloadCh)
registerWeb(cmds, app, "web", reloadCh, reloaders)
registerApi(cmds, app, "api")
registerSymbol(cmds, app, "symbol")
registerAll(cmds, app, "all", reloadCh, reloaders)
cmd, err := app.Parse(os.Args[1:])
if err != nil {
fmt.Fprintln(os.Stderr, errors.Wrapf(err, "Error parsing commandline arguments"))
app.Usage(os.Args[1:])
os.Exit(2)
}
var logger log.Logger
{
var lvl level.Option
switch *logLevel {
case "error":
lvl = level.AllowError()
case "warn":
lvl = level.AllowWarn()
case "info":
lvl = level.AllowInfo()
case "debug":
lvl = level.AllowDebug()
default:
panic("unexpected log level")
}
logger = log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
if *logFormat == logFormatJSON {
logger = log.NewJSONLogger(log.NewSyncWriter(os.Stderr))
}
logger = level.NewFilter(logger, lvl)
if *debugName != "" {
logger = log.With(logger, "name", *debugName)
}
logger = log.With(logger, "ts", log.DefaultTimestampUTC, "caller", log.DefaultCaller)
}
reg := prometheus.NewRegistry()
reg.MustRegister(
version.NewCollector("conprof"),
prometheus.NewGoCollector(),
prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}),
)
prometheus.DefaultRegisterer = reg
var g run.Group
mux := http.NewServeMux()
httpProbe := prober.NewHTTP()
comp := componentString(cmd)
if *otlpAddress != "" {
initTracer(logger, cmd, *otlpAddress)
}
statusProber, err := cmds[cmd](comp, &g, mux, httpProbe, logger, reg, *logLevel == "debug")
if err != nil {
fmt.Fprintln(os.Stderr, errors.Wrapf(err, "%s command failed", cmd))
os.Exit(1)
}
{
srv := httpserver.New(logger, reg, comp, httpProbe,
httpserver.WithListen(*httpBindAddr),
httpserver.WithGracePeriod(time.Duration(*httpGracePeriod)),
)
srv.Handle("/", cors(*corsOrigin, *corsMethods, mux))
g.Add(func() error {
statusProber.Healthy()
return srv.ListenAndServe()
}, func(err error) {
statusProber.NotReady(err)
defer statusProber.NotHealthy(err)
srv.Shutdown(err)
})
}
// Listen for termination signals.
g.Add(run.SignalHandler(context.Background(), syscall.SIGINT, syscall.SIGTERM))
// Synchronize all reloaders for the first time
reloadCh <- struct{}{}
if err := g.Run(); err != nil {
level.Error(logger).Log("msg", "running command failed", "err", err)
os.Exit(1)
}
level.Info(logger).Log("msg", "exiting")
}
func cors(corsOrigin, corsMethods string, h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if corsOrigin != "" {
w.Header().Set("Access-Control-Allow-Origin", corsOrigin)
}
if corsMethods != "" {
w.Header().Set("Access-Control-Allow-Methods", corsMethods)
}
h.ServeHTTP(w, r)
})
}
func initTracer(logger log.Logger, serviceName string, otlpAddress string) func() {
ctx := context.Background()
driver := otlpgrpc.NewDriver(
otlpgrpc.WithInsecure(),
otlpgrpc.WithEndpoint(otlpAddress),
)
exporter, err := otlp.NewExporter(ctx, driver)
handleErr(logger, err, "failed to create exporter")
res, err := resource.New(ctx,
resource.WithAttributes(
semconv.ServiceNameKey.String(serviceName),
),
)
handleErr(logger, err, "failed to create resource")
bsp := sdktrace.NewBatchSpanProcessor(exporter)
tracerProvider := sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithResource(res),
sdktrace.WithSpanProcessor(bsp),
)
// set global propagator to tracecontext (the default is no-op).
otel.SetTextMapPropagator(propagation.TraceContext{})
otel.SetTracerProvider(tracerProvider)
return func() {
handleErr(logger, exporter.Shutdown(context.Background()), "failed to stop exporter")
}
}
func handleErr(logger log.Logger, err error, message string) {
if err != nil {
level.Error(logger).Log("msg", message, "err", err)
os.Exit(1)
}
}