Skip to content

Commit

Permalink
🌱 Restore API quota metrics for the weekly cron job. (ossf#2799)
Browse files Browse the repository at this point in the history
Signed-off-by: Spencer Schrock <sschrock@google.com>
Signed-off-by: Avishay <avishay.balter@gmail.com>
  • Loading branch information
spencerschrock authored and balteravishay committed Apr 14, 2023
1 parent 269c638 commit f17e8e6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
6 changes: 6 additions & 0 deletions clients/githubrepo/roundtripper/rate_limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"time"

"go.opencensus.io/stats"
"go.opencensus.io/tag"

githubstats "github.com/ossf/scorecard/v4/clients/githubrepo/stats"
sce "github.com/ossf/scorecard/v4/errors"
Expand Down Expand Up @@ -63,6 +64,11 @@ func (gh *rateLimitTransport) RoundTrip(r *http.Request) (*http.Response, error)
if err != nil {
return resp, nil
}
ctx, err := tag.New(r.Context(), tag.Upsert(githubstats.ResourceType, resp.Header.Get("X-RateLimit-Resource")))
if err != nil {
return nil, fmt.Errorf("error updating context: %w", err)
}
stats.Record(ctx, githubstats.RemainingTokens.M(int64(remaining)))

if remaining <= 0 {
reset, err := strconv.Atoi(resp.Header.Get("X-RateLimit-Reset"))
Expand Down
11 changes: 0 additions & 11 deletions clients/githubrepo/roundtripper/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ package roundtripper
import (
"fmt"
"net/http"
"strconv"

"go.opencensus.io/stats"
"go.opencensus.io/tag"

"github.com/ossf/scorecard/v4/clients/githubrepo/roundtripper/tokens"
Expand Down Expand Up @@ -56,14 +54,5 @@ func (gt *githubTransport) RoundTrip(r *http.Request) (*http.Response, error) {
return nil, fmt.Errorf("error in HTTP: %w", err)
}

ctx, err = tag.New(r.Context(), tag.Upsert(githubstats.ResourceType, resp.Header.Get("X-RateLimit-Resource")))
if err != nil {
return nil, fmt.Errorf("error updating context: %w", err)
}
remaining, err := strconv.Atoi(resp.Header.Get("X-RateLimit-Remaining"))
if err == nil {
stats.Record(ctx, githubstats.RemainingTokens.M(int64(remaining)))
}

return resp, nil
}

0 comments on commit f17e8e6

Please sign in to comment.