/
main.go
211 lines (192 loc) · 4.91 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
package main
import (
"context"
"flag"
"fmt"
"net/http"
"net/url"
"os"
"os/signal"
"strings"
"text/tabwriter"
"time"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/oklog/run"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var version = "dev"
func main() {
fs := flag.NewFlagSet("fastly-exporter", flag.ExitOnError)
var (
token = fs.String("token", "", "Fastly API token (required; also via FASTLY_API_TOKEN)")
serviceIDs = stringslice{}
addr = fs.String("endpoint", "http://127.0.0.1:8080/metrics", "Prometheus /metrics endpoint")
namespace = fs.String("namespace", "fastly", "Prometheus namespace")
subsystem = fs.String("subsystem", "rt", "Prometheus subsystem")
debug = fs.Bool("debug", false, "Log debug information")
)
fs.Var(&serviceIDs, "service", "Specific Fastly service ID (optional, repeatable)")
fs.Usage = usageFor(fs, "fastly-exporter [flags]")
fs.Parse(os.Args[1:])
var logger log.Logger
{
logger = log.NewLogfmtLogger(os.Stderr)
loglevel := level.AllowInfo()
if *debug {
loglevel = level.AllowDebug()
}
logger = level.NewFilter(logger, loglevel)
}
if *token == "" {
if *token = os.Getenv("FASTLY_API_TOKEN"); *token == "" {
level.Error(logger).Log("err", "-token or FASTLY_API_TOKEN is required")
os.Exit(1)
}
}
var metrics prometheusMetrics
{
metrics.register(*namespace, *subsystem)
}
var promURL *url.URL
{
var err error
promURL, err = url.Parse(*addr)
if err != nil {
level.Error(logger).Log("err", err)
os.Exit(1)
}
level.Info(logger).Log("prometheus_addr", promURL.Host, "path", promURL.Path, "namespace", *namespace, "subsystem", *subsystem)
}
var cache *nameCache
{
cache = newNameCache()
}
var manager *monitorManager
{
var (
postprocess = func() {} // only used for tests
rtClient = &http.Client{Timeout: time.Minute} // rt.fastly.com blocks awhile by design
)
manager = newMonitorManager(rtClient, *token, cache, metrics, postprocess, log.With(logger, "component", "monitors"))
}
var apiClient *http.Client
{
apiClient = &http.Client{
Timeout: 10 * time.Second, // api.fastly.com should be fast
}
}
var queryer *serviceQueryer
{
queryer = newServiceQueryer(*token, serviceIDs, cache, manager)
if err := queryer.refresh(apiClient); err != nil { // first refresh must succeed
level.Error(logger).Log("during", "initial service refresh", "err", err)
os.Exit(1)
}
}
var g run.Group
{
// Every minute, query Fastly for new services and their names.
// Update our name cache and managed monitors accordingly.
var (
ctx, cancel = context.WithCancel(context.Background())
ticker = time.NewTicker(time.Minute)
)
g.Add(func() error {
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
if err := queryer.refresh(apiClient); err != nil {
level.Warn(logger).Log("during", "service refresh", "err", err)
}
}
}
}, func(error) {
ticker.Stop()
cancel()
})
}
{
// A pseudo-actor that exists only to tear down all managed monitors.
ctx, cancel := context.WithCancel(context.Background())
g.Add(func() error {
<-ctx.Done()
manager.stopAll()
return ctx.Err()
}, func(error) {
cancel()
})
}
{
// Serve Prometheus metrics over HTTP.
mux := http.NewServeMux()
mux.Handle(promURL.Path, promhttp.Handler())
server := http.Server{
Addr: promURL.Host,
Handler: mux,
}
g.Add(func() error {
return server.ListenAndServe()
}, func(error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
server.Shutdown(ctx)
})
}
{
// Catch ctrl-C.
var (
ctx, cancel = context.WithCancel(context.Background())
c = make(chan os.Signal, 1)
)
signal.Notify(c, os.Interrupt)
g.Add(func() error {
select {
case <-ctx.Done():
return ctx.Err()
case sig := <-c:
return fmt.Errorf("received signal %s", sig)
}
}, func(error) {
cancel()
})
}
level.Info(logger).Log("exit", g.Run())
}
type stringslice []string
func (ss *stringslice) Set(s string) error {
(*ss) = append(*ss, s)
return nil
}
func (ss *stringslice) String() string {
if len(*ss) <= 0 {
return "..."
}
return strings.Join(*ss, ", ")
}
type httpClient interface {
Do(*http.Request) (*http.Response, error)
}
func usageFor(fs *flag.FlagSet, short string) func() {
return func() {
fmt.Fprintf(os.Stderr, "USAGE\n")
fmt.Fprintf(os.Stderr, " %s\n", short)
fmt.Fprintf(os.Stderr, "\n")
fmt.Fprintf(os.Stderr, "FLAGS\n")
w := tabwriter.NewWriter(os.Stderr, 0, 2, 2, ' ', 0)
fs.VisitAll(func(f *flag.Flag) {
def := f.DefValue
if def == "" {
def = "..."
}
fmt.Fprintf(w, "\t-%s %s\t%s\n", f.Name, def, f.Usage)
})
w.Flush()
fmt.Fprintf(os.Stderr, "\n")
fmt.Fprintf(os.Stderr, "VERSION\n")
fmt.Fprintf(os.Stderr, " %s\n", version)
fmt.Fprintf(os.Stderr, "\n")
}
}