generated from vshn/go-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
91 lines (76 loc) · 2.39 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
package main
import (
"context"
"log"
"os"
"os/signal"
"time"
flag "github.com/spf13/pflag"
"github.com/appuio/network-canary/ping"
"github.com/go-logr/logr"
"github.com/go-logr/zapr"
"go.uber.org/zap"
)
var (
// these variables are populated by Goreleaser when releasing
version = "unknown"
commit = "-dirty-"
date = time.Now().Format("2006-01-02")
)
func main() {
listenAddress := flag.String("metrics-addr", ":2112", "Listen address for metrics server")
dnsTargets := flag.StringSlice("ping-dns", []string{}, "List of DNS names to ping to")
ipTargets := flag.StringSlice("ping-ip", []string{}, "List of IPs to ping to")
source := flag.String("src", "", "The source address")
update := flag.Duration("update-interval", 4*time.Second, "How often the canary should fetch DNS updates")
interval := flag.Duration("ping-interval", time.Second, "How often the canary should send a ping to each target")
timeout := flag.Duration("ping-timeout", 5*time.Second, "Timout until a ping should be considered lost")
verbose := flag.Bool("verbose", false, "If the canary should log debug message")
encoding := flag.String("encoding", "console", "How to format log output one of 'console' or 'json'")
flag.Parse()
if *source == "" {
*source = os.Getenv("POD_IP")
}
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()
l, err := newLogger(*verbose, *encoding)
if err != nil {
log.Fatalf("Failed to initialie logger: \n\n%s\n", err.Error())
}
l.Info("Starting canary..")
ctx = logr.NewContext(ctx, l)
m := ping.NewManager(rttHist, ping.ManagerConfig{
Source: *source,
DNSTargets: *dnsTargets,
IPTargets: *ipTargets,
UpdateInterval: *update,
PingInterval: *interval,
PingTimeout: *timeout,
})
go func() {
err := m.Run(ctx)
if err != nil {
l.Error(err, "Ping manager failed")
stop()
}
}()
if err := serveMetrics(ctx, *listenAddress); err != nil {
l.Error(err, "Serving Metrics service failed")
os.Exit(1)
}
}
func newLogger(debug bool, encoding string) (logr.Logger, error) {
cfg := zap.NewProductionConfig()
if debug {
cfg.Level = zap.NewAtomicLevelAt(zap.DebugLevel)
}
cfg.Encoding = encoding
cfg.EncoderConfig.ConsoleSeparator = " | "
z, err := cfg.Build()
if err != nil {
return logr.Logger{}, err
}
zap.ReplaceGlobals(z)
logger := zapr.NewLogger(z)
return logger.WithValues("version", version), nil
}