Skip to content

Commit afdd35b

Browse files
committed
Logging Middleware: Make Rate limit logging options configurable
1 parent d07099e commit afdd35b

File tree

1 file changed

+21
-23
lines changed

1 file changed

+21
-23
lines changed

githubapp/middleware_logging.go

+21-23
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,7 @@ func ClientLogging(lvl zerolog.Level, opts ...ClientLoggingOption) ClientMiddlew
9292
Int64("size", -1)
9393
}
9494

95-
if options.LogRateLimitInformation {
96-
addRateLimitInformationToLog(evt, res)
97-
}
98-
95+
addRateLimitInformationToLog(options.LogRateLimitInformation, evt, res)
9996
evt.Msg("github_request")
10097
return res, err
10198
})
@@ -110,7 +107,16 @@ type clientLoggingOptions struct {
110107
ResponseBodyPatterns []*regexp.Regexp
111108

112109
// Output control
113-
LogRateLimitInformation bool
110+
LogRateLimitInformation *RateLimitLoggingOption
111+
}
112+
113+
// RateLimitLoggingOption controls which rate limit information is logged.
114+
type RateLimitLoggingOption struct {
115+
Limit bool
116+
Remaining bool
117+
Used bool
118+
Reset bool
119+
Resource bool
114120
}
115121

116122
// LogRequestBody enables request body logging for requests to paths matching
@@ -133,19 +139,11 @@ func LogResponseBody(patterns ...string) ClientLoggingOption {
133139
}
134140
}
135141

136-
// EnableRateLimitInformation enables logging of rate limit information like
137-
// the number of requests remaining in the current rate limit window.
138-
func EnableRateLimitInformation() ClientLoggingOption {
139-
return func(opts *clientLoggingOptions) {
140-
opts.LogRateLimitInformation = true
141-
}
142-
}
143-
144-
// DisableRateLimitInformation disables logging of rate limit information like
145-
// the number of requests remaining in the current rate limit window.
146-
func DisableRateLimitInformation() ClientLoggingOption {
142+
// SetRateLimitInformation defines which rate limit information like
143+
// the number of requests remaining in the current rate limit window is getting logged.
144+
func SetRateLimitInformation(options *RateLimitLoggingOption) ClientLoggingOption {
147145
return func(opts *clientLoggingOptions) {
148-
opts.LogRateLimitInformation = false
146+
opts.LogRateLimitInformation = options
149147
}
150148
}
151149

@@ -207,25 +205,25 @@ func closeBody(b io.ReadCloser) {
207205
_ = b.Close() // per http.Transport impl, ignoring close errors is fine
208206
}
209207

210-
func addRateLimitInformationToLog(evt *zerolog.Event, res *http.Response) {
211-
if limitHeader := res.Header.Get(HTTPHeaderRateLimit); limitHeader != "" {
208+
func addRateLimitInformationToLog(loggingOptions *RateLimitLoggingOption, evt *zerolog.Event, res *http.Response) {
209+
if limitHeader := res.Header.Get(HTTPHeaderRateLimit); loggingOptions.Limit && limitHeader != "" {
212210
limit, _ := strconv.Atoi(limitHeader)
213211
evt.Int("ratelimit-limit", limit)
214212
}
215-
if remainingHeader := res.Header.Get(HTTPHeaderRateRemaining); remainingHeader != "" {
213+
if remainingHeader := res.Header.Get(HTTPHeaderRateRemaining); loggingOptions.Remaining && remainingHeader != "" {
216214
remaining, _ := strconv.Atoi(remainingHeader)
217215
evt.Int("ratelimit-remaining", remaining)
218216
}
219-
if usedHeader := res.Header.Get(HTTPHeaderRateUsed); usedHeader != "" {
217+
if usedHeader := res.Header.Get(HTTPHeaderRateUsed); loggingOptions.Used && usedHeader != "" {
220218
used, _ := strconv.Atoi(usedHeader)
221219
evt.Int("ratelimit-used", used)
222220
}
223-
if resetHeader := res.Header.Get(HTTPHeaderRateReset); resetHeader != "" {
221+
if resetHeader := res.Header.Get(HTTPHeaderRateReset); loggingOptions.Reset && resetHeader != "" {
224222
if v, _ := strconv.ParseInt(resetHeader, 10, 64); v != 0 {
225223
evt.Time("ratelimit-reset", time.Unix(v, 0))
226224
}
227225
}
228-
if resourceHeader := res.Header.Get(HTTPHeaderRateResource); resourceHeader != "" {
226+
if resourceHeader := res.Header.Get(HTTPHeaderRateResource); loggingOptions.Resource && resourceHeader != "" {
229227
evt.Str("ratelimit-resource", resourceHeader)
230228
}
231229
}

0 commit comments

Comments
 (0)