Skip to content

Commit

Permalink
feat: extract collector metrics and add collector tests
Browse files Browse the repository at this point in the history
  • Loading branch information
smartinov committed Dec 5, 2018
1 parent d5fbebe commit 186a2c7
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 11 deletions.
42 changes: 31 additions & 11 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
"google.golang.org/api/pagespeedonline/v5"
"net/url"
"strconv"
"strings"
"time"
Expand All @@ -31,33 +32,52 @@ func (c collector) Describe(ch chan<- *prometheus.Desc) {
func (c collector) Collect(ch chan<- prometheus.Metric) {
start := time.Now()
result, errScrape := c.scrapeService.Scrape(c.targets)

if errScrape != nil {
logrus.WithError(errScrape).Warn("Could not scrape targets")
ch <- prometheus.NewInvalidMetric(prometheus.NewDesc("pagespeed_error", "Error scraping target", nil, nil), errScrape)
ch <- prometheus.NewInvalidMetric(prometheus.NewDesc(fqname("error"), "Error scraping target", nil, nil), errScrape)
return
}

ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc("pagespeed_scrape_duration_seconds", "Total Pagespeed time scrape took for all targets.", nil, nil),
prometheus.NewDesc(fqname("scrape_duration_seconds"), "Total Pagespeed time scrape took for all targets.", nil, nil),
prometheus.GaugeValue,
float64(time.Since(start).Seconds()))

for _, scrape := range result {
collect(scrape, ch)
errCollect := collect(scrape, ch)
if errCollect != nil {
logrus.WithError(errCollect).WithFields(logrus.Fields{
"target": scrape.Target,
"strategy": scrape.Strategy,
}).Error("could not collect scrape target due to errors")
}
}
}

func collect(scrape *Scrape, ch chan<- prometheus.Metric) {
constLables := prometheus.Labels{
"target": scrape.Target,
"strategy": string(scrape.Strategy),
func collect(scrape *Scrape, ch chan<- prometheus.Metric) error {
constLabels, errLabels := getConstLabels(scrape)
if errLabels != nil {
return errLabels
}

r := scrape.Result
collectLoadingExperience("loading_experience", r.LoadingExperience, constLables, ch)
collectLoadingExperience("origin_loading_experience", r.OriginLoadingExperience, constLables, ch)
collectLighthouseResults("lighthouse", r.LighthouseResult, constLables, ch)
collectLoadingExperience("loading_experience", r.LoadingExperience, constLabels, ch)
collectLoadingExperience("origin_loading_experience", r.OriginLoadingExperience, constLabels, ch)
collectLighthouseResults("lighthouse", r.LighthouseResult, constLabels, ch)
return nil
}

func getConstLabels(scrape *Scrape) (prometheus.Labels, error) {
target, errParse := url.Parse(scrape.Target)
if errParse != nil {
return nil, errParse
}

return prometheus.Labels{
"host": target.Host,
"path": target.Path,
"strategy": string(scrape.Strategy),
}, nil
}

func collectLoadingExperience(prefix string, lexp *pagespeedonline.PagespeedApiLoadingExperienceV5, constLables prometheus.Labels, ch chan<- prometheus.Metric) {
Expand Down
44 changes: 44 additions & 0 deletions collector/collector_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package collector

import (
"reflect"
"testing"

"github.com/prometheus/client_golang/prometheus"
)

func Test_getConstLabels(t *testing.T) {
type args struct {
scrape *Scrape
}
getArgs := func(target string, strategy Strategy) args {
return args{&Scrape{Target: target, Strategy: strategy}}
}
tests := []struct {
name string
args args
wantLabels prometheus.Labels
wantErr bool
}{
{"valid mobile", getArgs("https://host/path", StrategyMobile),
prometheus.Labels{"host": "host", "path": "/path", "strategy": string(StrategyMobile)}, false},

{"valid desktop", getArgs("https://host/path", StrategyDesktop),
prometheus.Labels{"host": "host", "path": "/path", "strategy": string(StrategyDesktop)}, false},

{"invalid url", getArgs("http://[fe80::1%en0]:8080/", StrategyMobile),
nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotLabels, err := getConstLabels(tt.args.scrape)
if (err != nil) != tt.wantErr {
t.Errorf("getConstLabels() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotLabels, tt.wantLabels) {
t.Errorf("getConstLabels() = %v, want %v", gotLabels, tt.wantLabels)
}
})
}
}

0 comments on commit 186a2c7

Please sign in to comment.