Skip to content

Commit

Permalink
Fix API Key authentication
Browse files Browse the repository at this point in the history
google-api-go-client deprecated setting the api key directly in
the transport and switched to using generic client options. Since
we are passing in our own HTTP client, we need to instantiate a
Google transport with the API key option in order for it to
authenticate correctly.
  • Loading branch information
penmanglewood authored and smartinov committed Jun 2, 2022
1 parent 68e2ac0 commit 1467563
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 54 deletions.
7 changes: 6 additions & 1 deletion collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,14 @@ func newCollector(config Config) (coll prometheus.Collector, err error) {
options = append(options, option.WithCredentialsFile(config.CredentialsFile))
}

svc, err := newPagespeedScrapeService(config.ScrapeTimeout, options...)
if err != nil {
return nil, err
}

return collector{
requests: config.ScrapeRequests,
scrapeService: newPagespeedScrapeService(config.ScrapeTimeout, options...),
scrapeService: svc,
parallel: config.Parallel,
}, nil
}
Expand Down
12 changes: 9 additions & 3 deletions collector/scrape.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"golang.org/x/oauth2"
"google.golang.org/api/option"
"google.golang.org/api/pagespeedonline/v5"
googlehttp "google.golang.org/api/transport/http"
)

var _ scrapeService = &pagespeedScrapeService{}
Expand All @@ -22,9 +23,14 @@ 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, options ...option.ClientOption) scrapeService {
func newPagespeedScrapeService(clientTimeout time.Duration, options ...option.ClientOption) (scrapeService, error) {
transport, err := googlehttp.NewTransport(context.Background(), http.DefaultTransport, options...)
if err != nil {
return nil, err
}

client := &http.Client{
Transport: http.DefaultTransport,
Transport: transport,
}

if clientTimeout != 0 {
Expand All @@ -34,7 +40,7 @@ func newPagespeedScrapeService(clientTimeout time.Duration, options ...option.Cl
return &pagespeedScrapeService{
scrapeClient: client,
options: options,
}
}, nil
}

type pagespeedScrapeService struct {
Expand Down
7 changes: 2 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@ go 1.12

require (
github.com/gammazero/workerpool v0.0.0-20190521015540-3b91a70bc0a1
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/pkg/errors v0.8.1
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.7.0
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5
google.golang.org/api v0.81.0
golang.org/x/oauth2 v0.0.0-20220524215830-622c5d57e401
google.golang.org/api v0.82.0
)
Loading

0 comments on commit 1467563

Please sign in to comment.