From 7c7b795e69ed64755be53311fa2258556a0b7aae Mon Sep 17 00:00:00 2001 From: Excavator Bot <33266368+svc-excavator-bot@users.noreply.github.com> Date: Tue, 4 Feb 2025 12:15:52 -0800 Subject: [PATCH] Excavator: Manage go module dependencies (#580) --- go.mod | 2 +- go.sum | 4 +- .../go-githubapp/githubapp/middleware.go | 5 +- .../githubapp/middleware_logging.go | 61 +++++++++++++++++++ vendor/modules.txt | 2 +- 5 files changed, 68 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index 303529631..6961c3db3 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/google/go-github/v68 v68.0.0 github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 github.com/palantir/go-baseapp v0.5.2 - github.com/palantir/go-githubapp v0.32.0 + github.com/palantir/go-githubapp v0.32.1 github.com/pkg/errors v0.9.1 github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 github.com/rs/zerolog v1.33.0 diff --git a/go.sum b/go.sum index cbffea868..c78341abd 100644 --- a/go.sum +++ b/go.sum @@ -55,8 +55,8 @@ github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zk github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= github.com/palantir/go-baseapp v0.5.2 h1:b1ukx7AXo2/E4NkUvTFlW+185uwCcifzd2XzLrG4oS8= github.com/palantir/go-baseapp v0.5.2/go.mod h1:uijQMPfmgV69oiMu2jkskum/4HiYuEP/gzrnphD+/Co= -github.com/palantir/go-githubapp v0.32.0 h1:lrd3hKo09+rM8j5g2q7np9rvUIwKNKlAxLPXaG9944c= -github.com/palantir/go-githubapp v0.32.0/go.mod h1:VDxgPURYOdMjgAhEwrFm3VcyipcOM0OFr4YyTd98YsA= +github.com/palantir/go-githubapp v0.32.1 h1:l+nSk6pjN4+WeJlbQ9iZqmID53kZyW7QBuRC288b8k4= +github.com/palantir/go-githubapp v0.32.1/go.mod h1:/4mdcMoxJG4/2w260cnnfCZ8fopffom4xt7l7ZQSUrU= github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc= github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= diff --git a/vendor/github.com/palantir/go-githubapp/githubapp/middleware.go b/vendor/github.com/palantir/go-githubapp/githubapp/middleware.go index ec11fbc14..1382bc5d1 100644 --- a/vendor/github.com/palantir/go-githubapp/githubapp/middleware.go +++ b/vendor/github.com/palantir/go-githubapp/githubapp/middleware.go @@ -75,8 +75,9 @@ func ClientMetrics(registry metrics.Registry) ClientMiddleware { remainingMetric := fmt.Sprintf("%s[installation:%d]", MetricsKeyRateLimitRemaining, installationID) // Headers from https://developer.github.com/v3/#rate-limiting - updateRegistryForHeader(res.Header, "X-RateLimit-Limit", metrics.GetOrRegisterGauge(limitMetric, registry)) - updateRegistryForHeader(res.Header, "X-RateLimit-Remaining", metrics.GetOrRegisterGauge(remainingMetric, registry)) + updateRegistryForHeader(res.Header, httpHeaderRateLimit, metrics.GetOrRegisterGauge(limitMetric, registry)) + updateRegistryForHeader(res.Header, httpHeaderRateRemaining, metrics.GetOrRegisterGauge(remainingMetric, registry)) + // TODO Think about to add X-Ratelimit-Used, X-Ratelimit-Reset and X-Ratelimit-Resource as well } return res, err diff --git a/vendor/github.com/palantir/go-githubapp/githubapp/middleware_logging.go b/vendor/github.com/palantir/go-githubapp/githubapp/middleware_logging.go index 8e4dfec46..f33882a19 100644 --- a/vendor/github.com/palantir/go-githubapp/githubapp/middleware_logging.go +++ b/vendor/github.com/palantir/go-githubapp/githubapp/middleware_logging.go @@ -19,12 +19,21 @@ import ( "io" "net/http" "regexp" + "strconv" "time" "github.com/gregjones/httpcache" "github.com/rs/zerolog" ) +const ( + httpHeaderRateLimit = "X-Ratelimit-Limit" + httpHeaderRateRemaining = "X-Ratelimit-Remaining" + httpHeaderRateUsed = "X-Ratelimit-Used" + httpHeaderRateReset = "X-Ratelimit-Reset" + httpHeaderRateResource = "X-Ratelimit-Resource" +) + // ClientLogging creates client middleware that logs request and response // information at the given level. If the request fails without creating a // response, it is logged with a status code of -1. The middleware uses a @@ -83,6 +92,7 @@ func ClientLogging(lvl zerolog.Level, opts ...ClientLoggingOption) ClientMiddlew Int64("size", -1) } + addRateLimitInformationToLog(options.LogRateLimitInformation, evt, res) evt.Msg("github_request") return res, err }) @@ -95,6 +105,18 @@ type ClientLoggingOption func(*clientLoggingOptions) type clientLoggingOptions struct { RequestBodyPatterns []*regexp.Regexp ResponseBodyPatterns []*regexp.Regexp + + // Output control + LogRateLimitInformation *RateLimitLoggingOption +} + +// RateLimitLoggingOption controls which rate limit information is logged. +type RateLimitLoggingOption struct { + Limit bool + Remaining bool + Used bool + Reset bool + Resource bool } // LogRequestBody enables request body logging for requests to paths matching @@ -117,6 +139,14 @@ func LogResponseBody(patterns ...string) ClientLoggingOption { } } +// LogRateLimitInformation defines which rate limit information like +// the number of requests remaining in the current rate limit window is getting logged. +func LogRateLimitInformation(options *RateLimitLoggingOption) ClientLoggingOption { + return func(opts *clientLoggingOptions) { + opts.LogRateLimitInformation = options + } +} + func mirrorRequestBody(r *http.Request) (*http.Request, []byte, error) { switch { case r.Body == nil || r.Body == http.NoBody: @@ -174,3 +204,34 @@ func requestMatches(r *http.Request, pats []*regexp.Regexp) bool { func closeBody(b io.ReadCloser) { _ = b.Close() // per http.Transport impl, ignoring close errors is fine } + +func addRateLimitInformationToLog(loggingOptions *RateLimitLoggingOption, evt *zerolog.Event, res *http.Response) { + // Exit early if no rate limit information is requested + if loggingOptions == nil { + return + } + + rateLimitDict := zerolog.Dict() + if limitHeader := res.Header.Get(httpHeaderRateLimit); loggingOptions.Limit && limitHeader != "" { + limit, _ := strconv.Atoi(limitHeader) + rateLimitDict.Int("limit", limit) + } + if remainingHeader := res.Header.Get(httpHeaderRateRemaining); loggingOptions.Remaining && remainingHeader != "" { + remaining, _ := strconv.Atoi(remainingHeader) + rateLimitDict.Int("remaining", remaining) + } + if usedHeader := res.Header.Get(httpHeaderRateUsed); loggingOptions.Used && usedHeader != "" { + used, _ := strconv.Atoi(usedHeader) + rateLimitDict.Int("used", used) + } + if resetHeader := res.Header.Get(httpHeaderRateReset); loggingOptions.Reset && resetHeader != "" { + if v, _ := strconv.ParseInt(resetHeader, 10, 64); v != 0 { + rateLimitDict.Time("reset", time.Unix(v, 0)) + } + } + if resourceHeader := res.Header.Get(httpHeaderRateResource); loggingOptions.Resource && resourceHeader != "" { + rateLimitDict.Str("resource", resourceHeader) + } + + evt.Dict("ratelimit", rateLimitDict) +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 39f135449..0ee72d835 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -67,7 +67,7 @@ github.com/palantir/go-baseapp/appmetrics/emitter/prometheus github.com/palantir/go-baseapp/baseapp github.com/palantir/go-baseapp/baseapp/datadog github.com/palantir/go-baseapp/pkg/errfmt -# github.com/palantir/go-githubapp v0.32.0 +# github.com/palantir/go-githubapp v0.32.1 ## explicit; go 1.23.0 github.com/palantir/go-githubapp/appconfig github.com/palantir/go-githubapp/githubapp