-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
310 lines (271 loc) · 7.58 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package main
import (
"context"
"errors"
"fmt"
"net"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/gorilla/mux"
"github.com/urfave/cli/v2"
"golang.org/x/exp/slog"
"golang.org/x/sync/errgroup"
)
func main() {
if err := Run(os.Args); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
}
var options struct {
addr string
verbose bool
veryverbose bool
diagnosticsAddr string
dbHost string
dbPort int
dbName string
dbUser string
dbPassword string
dbSSLMode string
batchSize int
metricReportInterval int
}
var envPrefix = "TRACECATCHER_"
var app = &cli.App{
Name: "tracecatcher",
HelpName: "tracecatcher",
Usage: "Listens to gossipsub traces emitted from Lotus and stores them in postgresql.",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "addr",
Aliases: []string{"a"},
Usage: "Listen for traces on `ADDRESS:PORT`",
Value: ":5151",
EnvVars: []string{envPrefix + "ADDR"},
Destination: &options.addr,
},
&cli.StringFlag{
Name: "diag-addr",
Aliases: []string{"da"},
Usage: "Run diagnostics server for metrics on `ADDRESS:PORT`",
Value: "",
EnvVars: []string{envPrefix + "DIAG_ADDR"},
Destination: &options.diagnosticsAddr,
},
&cli.BoolFlag{
Name: "verbose",
Aliases: []string{"v"},
Usage: "Set logging level more verbose to include info level logs",
Value: false,
EnvVars: []string{envPrefix + "VERBOSE"},
Destination: &options.verbose,
},
&cli.BoolFlag{
Name: "veryverbose",
Aliases: []string{"vv"},
Usage: "Set logging level very verbose to include debug level logs",
Value: false,
EnvVars: []string{envPrefix + "VERY_VERBOSE"},
Destination: &options.veryverbose,
},
&cli.StringFlag{
Name: "db-host",
Usage: "The hostname/address of the database server",
EnvVars: []string{envPrefix + "DB_HOST"},
Destination: &options.dbHost,
},
&cli.IntFlag{
Name: "db-port",
Usage: "The port number of the database server",
EnvVars: []string{envPrefix + "DB_PORT"},
Value: 5432,
Destination: &options.dbPort,
},
&cli.StringFlag{
Name: "db-name",
Usage: "The name of the database to use",
EnvVars: []string{envPrefix + "DB_NAME"},
Destination: &options.dbName,
},
&cli.StringFlag{
Name: "db-password",
Usage: "The password to use when connecting the the database",
EnvVars: []string{envPrefix + "DB_PASSWORD"},
Destination: &options.dbPassword,
},
&cli.StringFlag{
Name: "db-user",
Usage: "The user to use when connecting the the database",
EnvVars: []string{envPrefix + "DB_USER"},
Destination: &options.dbUser,
},
&cli.StringFlag{
Name: "db-sslmode",
Usage: "The sslmode to use when connecting the the database",
EnvVars: []string{envPrefix + "DB_SSL_MODE"},
Value: "prefer",
Destination: &options.dbSSLMode,
},
&cli.IntFlag{
Name: "batch-size",
Aliases: []string{"b"},
Usage: "The size of query batches to use when inserting into the database",
EnvVars: []string{envPrefix + "BATCH_SIZE"},
Value: 100,
Destination: &options.batchSize,
},
&cli.IntFlag{
Name: "metric-report-interval",
Usage: "The interval (in seconds) on which metrics should be updated",
EnvVars: []string{envPrefix + "METRIC_REPORT_INTERVAL"},
Value: 30,
Destination: &options.metricReportInterval,
},
},
Action: run,
HideHelpCommand: true,
}
func Run(args []string) error {
return app.Run(os.Args)
}
func run(cc *cli.Context) error {
logLevel := new(slog.LevelVar)
logLevel.Set(slog.LevelWarn)
slog.SetDefault(slog.New(slog.HandlerOptions{Level: logLevel}.NewTextHandler(os.Stdout)))
if options.verbose {
logLevel.Set(slog.LevelInfo)
}
if options.veryverbose {
logLevel.Set(slog.LevelDebug)
}
ctx, cancel := context.WithCancel(cc.Context)
defer cancel()
pool, err := connect(ctx, options.dbHost, options.dbPort, options.dbName, options.dbSSLMode, options.dbUser, options.dbPassword)
if err != nil {
slog.Error("pgconn failed to connect", err)
return err
}
rg := new(RunGroup)
// Init metric reporting if required
if options.diagnosticsAddr != "" {
if err := InitMetricReporting(time.Duration(options.metricReportInterval) * time.Second); err != nil {
return fmt.Errorf("failed to initialize metric reporting: %w", err)
}
dr := &DiagRunner{
addr: options.diagnosticsAddr,
}
rg.Add(dr)
}
bat, err := NewBatcher(pool, options.batchSize)
if err != nil {
return fmt.Errorf("failed to create batcher: %w", err)
}
svr, err := NewServer(bat)
if err != nil {
return fmt.Errorf("failed to create web server: %w", err)
}
p := &WebRunner{
server: svr,
}
rg.Add(p)
return rg.RunAndWait(ctx)
}
// Runnable allows a component to be started.
type Runnable interface {
// Run starts running the component and blocks until the context is canceled, Shutdown is // called or a fatal error is encountered.
Run(context.Context) error
}
type RunGroup struct {
runnables []Runnable
}
func (rg *RunGroup) Add(r Runnable) {
rg.runnables = append(rg.runnables, r)
}
func (rg *RunGroup) RunAndWait(ctx context.Context) error {
if len(rg.runnables) == 0 {
return nil
}
ctx, cancel := context.WithCancel(ctx)
g, ctx := errgroup.WithContext(ctx)
for i := range rg.runnables {
r := rg.runnables[i]
g.Go(func() error { return r.Run(ctx) })
}
// Ensure components stop if we receive a terminating operating system signal.
g.Go(func() error {
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, syscall.SIGTERM, syscall.SIGINT)
select {
case <-interrupt:
cancel()
case <-ctx.Done():
}
return nil
})
// Wait for all servers to run to completion.
if err := g.Wait(); err != nil {
if !errors.Is(err, context.Canceled) {
return err
}
}
return nil
}
type WebRunner struct {
server *Server
}
func (r *WebRunner) Run(ctx context.Context) error {
mx := mux.NewRouter()
r.server.ConfigureRoutes(mx)
srv := &http.Server{
Handler: mx,
BaseContext: func(net.Listener) context.Context { return ctx },
}
go func() {
<-ctx.Done()
if err := srv.Shutdown(context.Background()); err != nil {
slog.Error("failed to shut down RPC server", err)
}
}()
slog.Info("starting server", "addr", options.addr)
listener, err := net.Listen("tcp", options.addr)
if err != nil {
return fmt.Errorf("failed to listen on %q: %w", options.addr, err)
}
if err := srv.Serve(listener); err != nil {
if !errors.Is(err, http.ErrServerClosed) {
return fmt.Errorf("serve failed: %w", err)
}
}
return nil
}
type DiagRunner struct {
addr string
}
func (dr *DiagRunner) Run(ctx context.Context) error {
diagListener, err := net.Listen("tcp", dr.addr)
if err != nil {
return fmt.Errorf("failed to listen on %q: %w", dr.addr, err)
}
pe, err := RegisterPrometheusExporter("tracecatcher")
if err != nil {
return fmt.Errorf("failed to register prometheus exporter: %w", err)
}
mx := mux.NewRouter()
mx.Handle("/metrics", pe)
srv := &http.Server{
Handler: mx,
BaseContext: func(net.Listener) context.Context { return ctx },
}
go func() {
<-ctx.Done()
if err := srv.Shutdown(context.Background()); err != nil {
slog.Error("failed to shut down diagnostics server", err)
}
}()
slog.Info("starting diagnostics server", "addr", dr.addr)
return srv.Serve(diagListener)
}