-
Notifications
You must be signed in to change notification settings - Fork 3
/
cmd.go
110 lines (97 loc) · 2.59 KB
/
cmd.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
package main
import (
"context"
"errors"
"fmt"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"syscall"
_ "github.com/lib/pq"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"github.com/volatiletech/sqlboiler/v4/boil"
)
var app *cli.App
func main() {
app = &cli.App{
Name: "tiros",
Version: "0.1.0",
Authors: []*cli.Author{
{
Name: "Dennis Trautwein",
Email: "[email protected]",
},
},
Usage: "measures the latency of making requests to the local gateway",
Before: Before,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "debug",
Usage: "Set this flag to enable debug logging",
EnvVars: []string{"TIROS_DEBUG"},
Value: false,
},
&cli.IntFlag{
Name: "log-level",
Usage: "Set this flag to a value from 0 (least verbose) to 6 (most verbose). Overrides the --debug flag",
EnvVars: []string{"TIROS_LOG_LEVEL"},
Value: 4,
},
&cli.StringFlag{
Name: "telemetry-host",
Usage: "To which network address should the telemetry (prometheus, pprof) server bind",
EnvVars: []string{"TIROS_TELEMETRY_HOST"},
Value: "localhost",
},
&cli.IntFlag{
Name: "telemetry-port",
Usage: "On which port should the telemetry (prometheus, pprof) server listen",
EnvVars: []string{"TIROS_TELEMETRY_PORT"},
Value: 6666,
},
},
Commands: []*cli.Command{
RunCommand,
},
}
sigs := make(chan os.Signal, 1)
ctx, cancel := context.WithCancel(context.Background())
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
go func() {
sig := <-sigs
log.Printf("Received %s signal - Stopping...\n", sig.String())
signal.Stop(sigs)
cancel()
}()
if err := app.RunContext(ctx, os.Args); err != nil && !errors.Is(err, context.Canceled) {
log.Errorf("error: %v\n", err)
os.Exit(1)
}
log.Infoln("Tiros stopped.")
}
func Before(c *cli.Context) error {
if c.Bool("debug") {
log.SetLevel(log.DebugLevel)
}
if c.IsSet("log-level") {
ll := c.Int("log-level")
log.SetLevel(log.Level(ll))
if ll == int(log.TraceLevel) {
boil.DebugMode = true
}
}
// Start prometheus metrics endpoint
go metricsListenAndServe(c.String("telemetry-host"), c.Int("telemetry-port"))
return nil
}
func metricsListenAndServe(host string, port int) {
addr := fmt.Sprintf("%s:%d", host, port)
log.WithField("addr", addr).Debugln("Starting telemetry endpoint")
http.Handle("/metrics", promhttp.Handler())
if err := http.ListenAndServe(addr, nil); err != nil {
log.WithError(err).Warnln("Error serving prometheus")
}
}