From 27b6358a48ed0f77d2e585d091173b18ff4667c0 Mon Sep 17 00:00:00 2001 From: Spencer Schrock Date: Tue, 28 Mar 2023 14:40:42 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=8C=B1=20Restore=20API=20quota=20metrics?= =?UTF-8?q?=20for=20the=20weekly=20cron=20job.=20(#2799)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Spencer Schrock --- clients/githubrepo/roundtripper/rate_limit.go | 6 ++++++ clients/githubrepo/roundtripper/transport.go | 11 ----------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/clients/githubrepo/roundtripper/rate_limit.go b/clients/githubrepo/roundtripper/rate_limit.go index 9730616d1f1..950e8f8aa8b 100644 --- a/clients/githubrepo/roundtripper/rate_limit.go +++ b/clients/githubrepo/roundtripper/rate_limit.go @@ -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" @@ -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")) diff --git a/clients/githubrepo/roundtripper/transport.go b/clients/githubrepo/roundtripper/transport.go index fa606979459..d8c577ed168 100644 --- a/clients/githubrepo/roundtripper/transport.go +++ b/clients/githubrepo/roundtripper/transport.go @@ -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" @@ -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 }