Skip to content

Commit d8e5d0a

Browse files
authored
chore: Add new client rate limit logging (#3486)
Signed-off-by: Steve Hipwell <steve.hipwell@gmail.com>
1 parent bcd1ee9 commit d8e5d0a

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

internal/ghclient/transport.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import (
77

88
ghct "github.com/bored-engineer/github-conditional-http-transport"
99
ratelimit "github.com/gofri/go-github-ratelimit/v2/github_ratelimit"
10+
ratelimitp "github.com/gofri/go-github-ratelimit/v2/github_ratelimit/github_primary_ratelimit"
11+
ratelimits "github.com/gofri/go-github-ratelimit/v2/github_ratelimit/github_secondary_ratelimit"
1012
"github.com/hashicorp/go-retryablehttp"
13+
"github.com/hashicorp/terraform-plugin-log/tflog"
1114
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging"
1215
"golang.org/x/oauth2"
1316
)
@@ -65,7 +68,17 @@ func newTransport(tokenSource oauth2.TokenSource, opts Options) (http.RoundTripp
6568
}
6669

6770
// Wrap with rate limit transport
68-
tr = ratelimit.New(tr)
71+
tr = ratelimit.New(tr, ratelimitp.WithLimitDetectedCallback(primaryRateLimitCallback), ratelimits.WithLimitDetectedCallback(secondaryRateLimitCallback))
6972

7073
return tr, nil
7174
}
75+
76+
// primaryRateLimitCallback is a callback function that is called when the GitHub API primary rate limit is detected. It logs a warning message with the category of the rate limit and the reset time.
77+
func primaryRateLimitCallback(cb *ratelimitp.CallbackContext) {
78+
tflog.Warn(cb.Request.Context(), "GitHub API primary rate limit detected.", map[string]any{"category": cb.Category, "reset_time": cb.ResetTime})
79+
}
80+
81+
// secondaryRateLimitCallback is a callback function that is called when the GitHub API secondary rate limit is detected. It logs a warning message with the reset time.
82+
func secondaryRateLimitCallback(cb *ratelimits.CallbackContext) {
83+
tflog.Warn(cb.Request.Context(), "GitHub API secondary rate limit detected.", map[string]any{"reset_time": cb.ResetTime})
84+
}

0 commit comments

Comments
 (0)