Skip to content

Commit

Permalink
feat: add multiple target specification with the -t flag
Browse files Browse the repository at this point in the history
  • Loading branch information
smartinov committed Dec 5, 2018
1 parent 3875229 commit f20ad89
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ pagespeed_exporter -api-key {KEY} -targets https://google.com,https://prometheus
| Flag | Variable | Description | Default | Required |
|-----------|--------------------|---------------------------------------------|---------|----------|
| -api-key | PAGESPEED_API_KEY | sets the google API key used for pagespeed | | True |
| -targets | PAGESPEED_TARGETS | comma separated list of targets to measure | | True |
| -targets | PAGESPEED_TARGETS | comma separated list of targets to measure | | False |
| -t | NONE | multi-value target array (check docker comp)| | False |
| -listener | PAGESPEED_LISTENER | sets the listener address for the exporters | :9271 | False |

Note: google api key is required only if scraping more than 2 targets/second
### Docker

```sh
docker run -p "9271:9271" --rm foomo/pagespeed_exporter -api-key {KEY} -targets https://google.com,https://prometheus.io
docker run -p "9271:9271" --rm foomo/pagespeed_exporter -api-key {KEY} -t https://google.com,https://prometheus.io
```
or
```sh
Expand Down
3 changes: 2 additions & 1 deletion example/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ services:
image: foomo/pagespeed_exporter:latest
restart: unless-stopped
command:
- "-targets=https://www.google.com"
- "-t=https://www.google.com"
- "-t=https://www.google.com/webhp"
# - "-api-key=${MY_API_KEY}"

prometheus:
Expand Down
19 changes: 16 additions & 3 deletions pagespeed_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,28 @@ import (
var (
googleApiKey string
listenerAddress string
targets []string
targets arrayFlags
)

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", Version, listenerAddress)
log.Infof("starting pagespeed exporter version %s on address %s for %d targets", Version, listenerAddress, len(targets))

psc, errCollector := collector.NewCollector(targets, googleApiKey)
if errCollector != nil {
Expand All @@ -40,10 +51,12 @@ 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")
targetsFlag := flag.String("targets", getenv("PAGESPEED_TARGETS", ""), "comma separated list of targets to measure")
flag.Var(&targets, "t", "Targets on a per-line basis")

flag.Parse()

targets = strings.Split(*targetsFlag, ",")
additionalTargets := strings.Split(*targetsFlag, ",")
targets = append(targets, additionalTargets...)

if len(targets) == 0 || targets[0] == "" {
log.Fatal("at least one target must be specified for metrics")
Expand Down

0 comments on commit f20ad89

Please sign in to comment.