Skip to content

Commit

Permalink
Fix #5, turn off caching for all queries
Browse files Browse the repository at this point in the history
  • Loading branch information
MacroPower committed Aug 12, 2020
1 parent b2f3801 commit e78e048
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 14 deletions.
9 changes: 6 additions & 3 deletions collectors/alltime/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ var (
// Exporter is the local definition of Exporter
type Exporter exporter.Exporter

// NewExporter creates the Summary exporter
// NewExporter creates the AllTime exporter
func NewExporter(baseURI *url.URL, user string, token string, sslVerify bool, timeout time.Duration, logger log.Logger) *Exporter {
var fetchStat func(url.URL, string) (io.ReadCloser, error)
var fetchStat func(url.URL, string, url.Values) (io.ReadCloser, error)
fetchStat = exporter.FetchHTTP(token, sslVerify, timeout, logger)

return &Exporter{
Expand Down Expand Up @@ -121,7 +121,10 @@ func (e *Exporter) scrape(ch chan<- prometheus.Metric) error {

userURL := exporter.UserPath(e.URI, e.User)

body, fetchErr := e.FetchStat(userURL, endpoint)
params := url.Values{}
params.Add("cache", "false")

body, fetchErr := e.FetchStat(userURL, endpoint, params)
if fetchErr != nil {
return fetchErr
}
Expand Down
9 changes: 6 additions & 3 deletions collectors/goal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ var (
// Exporter is the local definition of Exporter
type Exporter exporter.Exporter

// NewExporter creates the Summary exporter
// NewExporter creates the Goal exporter
func NewExporter(baseURI *url.URL, user string, token string, sslVerify bool, timeout time.Duration, logger log.Logger) *Exporter {
var fetchStat func(url.URL, string) (io.ReadCloser, error)
var fetchStat func(url.URL, string, url.Values) (io.ReadCloser, error)
fetchStat = exporter.FetchHTTP(token, sslVerify, timeout, logger)

return &Exporter{
Expand Down Expand Up @@ -122,7 +122,10 @@ func (e *Exporter) scrape(ch chan<- prometheus.Metric) error {

userURL := exporter.UserPath(e.URI, e.User)

body, fetchErr := e.FetchStat(userURL, endpoint)
params := url.Values{}
params.Add("cache", "false")

body, fetchErr := e.FetchStat(userURL, endpoint, params)
if fetchErr != nil {
return fetchErr
}
Expand Down
7 changes: 5 additions & 2 deletions collectors/leader/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type Exporter exporter.Exporter

// NewExporter creates the Leader exporter
func NewExporter(baseURI *url.URL, user string, token string, sslVerify bool, timeout time.Duration, logger log.Logger) *Exporter {
var fetchStat func(url.URL, string) (io.ReadCloser, error)
var fetchStat func(url.URL, string, url.Values) (io.ReadCloser, error)
fetchStat = exporter.FetchHTTP(token, sslVerify, timeout, logger)

return &Exporter{
Expand Down Expand Up @@ -118,7 +118,10 @@ func (e *Exporter) scrape(ch chan<- prometheus.Metric) error {

e.TotalScrapes.Inc()

body, fetchErr := e.FetchStat(*e.URI, endpoint)
params := url.Values{}
params.Add("cache", "false")

body, fetchErr := e.FetchStat(*e.URI, endpoint, params)
if fetchErr != nil {
return fetchErr
}
Expand Down
6 changes: 3 additions & 3 deletions collectors/summary/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type Exporter exporter.Exporter

// NewExporter creates the Summary exporter
func NewExporter(baseURI *url.URL, user string, token string, sslVerify bool, timeout time.Duration, logger log.Logger) *Exporter {
var fetchStat func(url.URL, string) (io.ReadCloser, error)
var fetchStat func(url.URL, string, url.Values) (io.ReadCloser, error)
fetchStat = exporter.FetchHTTP(token, sslVerify, timeout, logger)

return &Exporter{
Expand Down Expand Up @@ -127,11 +127,11 @@ func (e *Exporter) scrape(ch chan<- prometheus.Metric) error {
params := url.Values{}
params.Add("start", "today")
params.Add("end", "today")
params.Add("cache", "false")

userURL := exporter.UserPath(e.URI, e.User)
userURL.RawQuery = params.Encode()

body, fetchErr := e.FetchStat(userURL, endpoint)
body, fetchErr := e.FetchStat(userURL, endpoint, params)
if fetchErr != nil {
return fetchErr
}
Expand Down
2 changes: 1 addition & 1 deletion lib/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Exporter struct {
URI *url.URL
Endpoint, User string
Mutex sync.RWMutex
FetchStat func(url.URL, string) (io.ReadCloser, error)
FetchStat func(url.URL, string, url.Values) (io.ReadCloser, error)

Up prometheus.Gauge
TotalScrapes, QueryFailures prometheus.Counter
Expand Down
5 changes: 3 additions & 2 deletions lib/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,17 @@ func BoolToBinary(b bool) string {
}

// FetchHTTP is a generic fetch method for Wakatime API endpoints
func FetchHTTP(token string, sslVerify bool, timeout time.Duration, logger log.Logger) func(uri url.URL, subPath string) (io.ReadCloser, error) {
func FetchHTTP(token string, sslVerify bool, timeout time.Duration, logger log.Logger) func(uri url.URL, subPath string, params url.Values) (io.ReadCloser, error) {
tr := &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: !sslVerify}}
client := http.Client{
Timeout: timeout,
Transport: tr,
}
sEnc := b64.StdEncoding.EncodeToString([]byte(token))
return func(uri url.URL, subPath string) (io.ReadCloser, error) {
return func(uri url.URL, subPath string, params url.Values) (io.ReadCloser, error) {

uri.Path = path.Join(uri.Path, subPath)
uri.RawQuery = params.Encode()
url := uri.String()

level.Info(logger).Log("msg", "Scraping Wakatime", "path", subPath, "url", url)
Expand Down

0 comments on commit e78e048

Please sign in to comment.