/
app.go
155 lines (140 loc) · 3.91 KB
/
app.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
package main
import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"runtime/debug"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/urfave/cli/v2"
)
func MainApp() *cli.App {
var (
dnsServer string
logLevel string
interval time.Duration
timeout time.Duration
shutdownGracePeriod time.Duration
)
return &cli.App{
Name: "dns-resolve-exporter",
Usage: "Prometheus exporter for dns resolution requests",
UsageText: "dns-resolve-exporter [global options] [probe target...]",
HideHelpCommand: true,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "server",
Usage: "dns server address",
Required: true,
Aliases: []string{"s"},
Destination: &dnsServer,
EnvVars: []string{"DNS_RESOLVE_EXPORTER_DNSServer"},
},
&cli.DurationFlag{
Name: "interval",
Usage: "probe interval seconds",
Aliases: []string{"i"},
Value: 5 * time.Second,
Destination: &interval,
EnvVars: []string{"DNS_RESOLVE_EXPORTER_PROBE_INTERVAL"},
},
&cli.DurationFlag{
Name: "timeout",
Usage: "probe timeout seconds",
Value: 5 * time.Second,
Destination: &timeout,
EnvVars: []string{"DNS_RESOLVE_EXPORTER_PROBE_TIMEOUT"},
},
&cli.DurationFlag{
Name: "shutdown-grace-period",
Usage: "the time that server will wait to gracefully shut down",
Value: 1 * time.Second,
Destination: &shutdownGracePeriod,
EnvVars: []string{"DNS_RESOLVE_EXPORTER_SHUTDOWN_GRACE_PERIOD"},
},
&cli.StringFlag{
Name: "log-level",
Usage: "log level",
Value: "info",
Destination: &logLevel,
EnvVars: []string{"DNS_RESOLVE_EXPORTER_LOG_LEVEL"},
},
},
Before: func(cliCtx *cli.Context) error {
if timeout > interval {
return fmt.Errorf("interval must be larger than timeout")
}
probeTargets := cliCtx.Args().Slice()
if len(probeTargets) == 0 {
return fmt.Errorf("must set probe targets")
}
if _, err := SetLogLevel(logLevel); err != nil {
return err
}
return nil
},
Action: func(cliCtx *cli.Context) error {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
defer cancel()
showBuildInfo()
registry := prometheus.NewRegistry()
prober := NewProber(dnsServer, timeout, interval, registry)
probeTargets := cliCtx.Args().Slice()
for _, target := range probeTargets {
go func(target string) {
prober.Start(ctx, target)
}(target)
}
mux := http.NewServeMux()
mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(http.StatusText(http.StatusOK)))
})
mux.HandleFunc("/readyz", func(w http.ResponseWriter, r *http.Request) {
select {
case <-ctx.Done():
w.WriteHeader(http.StatusServiceUnavailable)
return
default:
}
w.WriteHeader(http.StatusOK)
})
mux.Handle("/metrics", promhttp.HandlerFor(registry, promhttp.HandlerOpts{}))
server := http.Server{
Addr: "0.0.0.0:8080",
Handler: mux,
}
go func() {
<-ctx.Done()
Logger().Info("shutdown signal received")
time.Sleep(shutdownGracePeriod)
if err := server.Shutdown(ctx); err != nil {
Logger().Errorw("http server shutdown failed", "error", err)
}
}()
if err := server.ListenAndServe(); err != http.ErrServerClosed {
return err
}
return nil
},
}
}
func showBuildInfo() {
info, _ := debug.ReadBuildInfo()
var gitCommit string
var dirty bool
for _, s := range info.Settings {
if s.Key == "vcs.revision" {
gitCommit = s.Value
}
if s.Key == "vcs.modified" {
dirty = s.Value == "true"
}
}
if dirty {
gitCommit = gitCommit + "-dirty"
}
Logger().Infow("build info", "goVersion", info.GoVersion, "gitCommit", gitCommit)
}