Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for credentials file #42

Merged
merged 1 commit into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ MAINTAINER Stefan Martinov <[email protected]>
COPY --from=builder /pagespeed_exporter /bin/pagespeed_exporter

RUN apk update \
&& apk --no-cache add ca-certificates
&& apk --no-cache add ca-certificates

EXPOSE 9271

Expand Down
12 changes: 11 additions & 1 deletion collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
"google.golang.org/api/option"
"google.golang.org/api/pagespeedonline/v5"
)

Expand Down Expand Up @@ -55,9 +56,18 @@ var timeAuditMetrics = map[string]bool{
}

func newCollector(config Config) (coll prometheus.Collector, err error) {
var options []option.ClientOption
if config.GoogleAPIKey != "" {
options = append(options, option.WithAPIKey(config.GoogleAPIKey))
}

if config.CredentialsFile != "" {
options = append(options, option.WithCredentialsFile(config.CredentialsFile))
}

return collector{
requests: config.ScrapeRequests,
scrapeService: newPagespeedScrapeService(config.ScrapeTimeout, config.GoogleAPIKey),
scrapeService: newPagespeedScrapeService(config.ScrapeTimeout, options...),
parallel: config.Parallel,
}, nil
}
Expand Down
12 changes: 7 additions & 5 deletions collector/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package collector

import (
"encoding/json"
"google.golang.org/api/pagespeedonline/v5"
"net/url"
"time"

"google.golang.org/api/pagespeedonline/v5"
)

const (
Expand Down Expand Up @@ -50,10 +51,11 @@ func (sr ScrapeRequest) IsValid() bool {
}

type Config struct {
ScrapeRequests []ScrapeRequest
GoogleAPIKey string
Parallel bool
ScrapeTimeout time.Duration
ScrapeRequests []ScrapeRequest
GoogleAPIKey string
CredentialsFile string
Parallel bool
ScrapeTimeout time.Duration
}

func CalculateScrapeRequests(targets ...string) []ScrapeRequest {
Expand Down
26 changes: 15 additions & 11 deletions collector/scrape.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ package collector

import (
"context"
"net/http"
"runtime"
"time"

"github.com/gammazero/workerpool"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"golang.org/x/oauth2"
"google.golang.org/api/googleapi/transport"
"google.golang.org/api/option"
"google.golang.org/api/pagespeedonline/v5"
"net/http"
"runtime"
"time"
)

var _ scrapeService = &pagespeedScrapeService{}
Expand All @@ -22,8 +22,7 @@ type scrapeService interface {

// newPagespeedScrapeService creates a new HTTP client service for pagespeed.
// If the client timeout is set to 0 there will be no timeout
func newPagespeedScrapeService(clientTimeout time.Duration, googleApiKey string) scrapeService {

func newPagespeedScrapeService(clientTimeout time.Duration, options ...option.ClientOption) scrapeService {
client := &http.Client{
Transport: http.DefaultTransport,
}
Expand All @@ -32,17 +31,15 @@ func newPagespeedScrapeService(clientTimeout time.Duration, googleApiKey string)
client.Timeout = clientTimeout
}

if googleApiKey != "" {
client.Transport = &transport.APIKey{Key: googleApiKey}
}

return &pagespeedScrapeService{
scrapeClient: client,
options: options,
}
}

type pagespeedScrapeService struct {
scrapeClient *http.Client
options []option.ClientOption
}

func (pss *pagespeedScrapeService) Scrape(parallel bool, requests []ScrapeRequest) (scrapes []*ScrapeResult, err error) {
Expand Down Expand Up @@ -86,7 +83,14 @@ func (pss *pagespeedScrapeService) Scrape(parallel bool, requests []ScrapeReques
}

func (pss pagespeedScrapeService) scrape(request ScrapeRequest) (scrape *ScrapeResult, err error) {
service, err := pagespeedonline.NewService(context.Background(), option.WithHTTPClient(pss.scrapeClient))
opts := []option.ClientOption{
option.WithHTTPClient(pss.scrapeClient),
}
opts = append(opts, pss.options...)
service, err := pagespeedonline.NewService(
context.Background(),
opts...,
)
if err != nil {
return nil, errors.Wrap(err, "could not initialize pagespeed service")
}
Expand Down
2 changes: 1 addition & 1 deletion collector/scrape_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func Test_PagespeedScrapeService(t *testing.T) {
t.Skip("skipping testing in short mode")
}

service := newPagespeedScrapeService(30*time.Second, "")
service := newPagespeedScrapeService(30 * time.Second)
scrapes, err := service.Scrape(false, CalculateScrapeRequests("http://example.com/"))
if err != nil {
t.Fatal("scrape should not throw an error")
Expand Down
2 changes: 2 additions & 0 deletions example/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PAGESPEED_CREDENTIALS_FILE="/path/to/credentials.json"
PAGESPEED_API_KEY="foo"
2 changes: 0 additions & 2 deletions example/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ services:
restart: unless-stopped
ports:
- "9271:9271"
#command:
# - "-api-key=${MY_API_KEY}"

prometheus:
restart: unless-stopped
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/prometheus/client_golang v1.5.1
github.com/prometheus/tsdb v0.7.1 // indirect
github.com/sirupsen/logrus v1.4.2
github.com/stretchr/testify v1.4.0
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
google.golang.org/api v0.5.0
github.com/stretchr/testify v1.7.0
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5
google.golang.org/api v0.81.0
)
Loading