forked from foomo/pagespeed_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pagespeed_exporter.go
executable file
·100 lines (81 loc) · 2.86 KB
/
pagespeed_exporter.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
package main
import (
"flag"
"net/http"
"strings"
"github.com/foomo/pagespeed_exporter/collector"
"github.com/foomo/pagespeed_exporter/handler"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
"os"
)
var (
googleApiKey string
listenerAddress string
targets arrayFlags
parallel bool
pushGatewayUrl string
pushGatewayJob string
)
var (
Version string
)
type arrayFlags []string
func (i *arrayFlags) String() string {
return "my string representation"
}
func (i *arrayFlags) Set(value string) error {
*i = append(*i, value)
return nil
}
func main() {
parseFlags()
log.Infof("starting pagespeed exporter version %s on address %s for %d targets", Version, listenerAddress, len(targets))
collectorFactory := collector.NewFactory()
// Register prometheus target collectors only if there is more than one target
if len(targets) > 0 {
requests := collector.CalculateScrapeRequests(targets...)
psc, errCollector := collectorFactory.Create(collector.Config{
ScrapeRequests: requests,
GoogleAPIKey: googleApiKey,
Parallel: parallel,
})
if errCollector != nil {
log.WithError(errCollector).Fatal("could not instantiate collector")
}
prometheus.MustRegister(psc)
}
mux := http.NewServeMux()
mux.Handle("/", handler.NewIndexHandler())
mux.Handle("/metrics", promhttp.Handler())
mux.Handle("/probe", handler.NewProbeHandler(googleApiKey, parallel, collectorFactory, pushGatewayUrl, pushGatewayJob))
server := http.Server{
Addr: listenerAddress,
Handler: mux,
}
log.Fatal(server.ListenAndServe())
}
func parseFlags() {
flag.StringVar(&googleApiKey, "api-key", getenv("PAGESPEED_API_KEY", ""), "sets the google API key used for pagespeed")
flag.StringVar(&listenerAddress, "listener", getenv("PAGESPEED_LISTENER", ":9271"), "sets the listener address for the exporters")
flag.BoolVar(¶llel, "parallel", getenv("PAGESPEED_PARALLEL", "false") == "true", "forces parallel execution for pagespeed")
flag.StringVar(&pushGatewayUrl, "pushGatewayUrl", getenv("PUSHGATEWAY_URL", ""), "sets the push gateway to send the metrics. leave empty to ignore it")
flag.StringVar(&pushGatewayJob, "pushGatewayJob", getenv("PUSHGATEWAY_JOB", "pagespeed_exporter"), "sets push gateway job name")
targetsFlag := flag.String("targets", getenv("PAGESPEED_TARGETS", ""), "comma separated list of targets to measure")
flag.Var(&targets, "t", "multiple argument parameters")
flag.Parse()
if *targetsFlag != "" {
additionalTargets := strings.Split(*targetsFlag, ",")
targets = append(targets, additionalTargets...)
}
if len(targets) == 0 || targets[0] == "" {
log.Info("no targets specified, listening from collector")
}
}
func getenv(key string, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}