mirror of
https://github.com/actions/actions-runner-controller.git
synced 2025-12-10 11:41:27 +00:00
The log level -3 is the minimum log level that is supported today, smaller than debug(-1) and -2(used to log some HRA related logs). This commit adds a logging HTTP transport to log HTTP requests and responses to that log level. It implements http.RoundTripper so that it can log each HTTP request with useful metadata like `from_cache` and `ratelimit_remaining`. The former is set to `true` only when the logged request's response was served from ARC's in-memory cache. The latter is set to X-RateLimit-Remaining response header value if and only if the response was served by GitHub, not by ARC's cache.
52 lines
1.1 KiB
Go
52 lines
1.1 KiB
Go
// Package logging provides various logging helpers for ARC
|
|
package logging
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/go-logr/logr"
|
|
"github.com/gregjones/httpcache"
|
|
)
|
|
|
|
const (
|
|
// https://docs.github.com/en/rest/overview/resources-in-the-rest-api#rate-limiting
|
|
headerRateLimitRemaining = "X-RateLimit-Remaining"
|
|
)
|
|
|
|
// Transport wraps a transport with metrics monitoring
|
|
type Transport struct {
|
|
Transport http.RoundTripper
|
|
|
|
Log *logr.Logger
|
|
}
|
|
|
|
func (t Transport) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
resp, err := t.Transport.RoundTrip(req)
|
|
if resp != nil {
|
|
t.log(req, resp)
|
|
}
|
|
return resp, err
|
|
}
|
|
|
|
func (t Transport) log(req *http.Request, resp *http.Response) {
|
|
if t.Log == nil {
|
|
return
|
|
}
|
|
|
|
var args []interface{}
|
|
|
|
marked := resp.Header.Get(httpcache.XFromCache) == "1"
|
|
|
|
args = append(args, "from_cache", marked, "method", req.Method, "url", req.URL.String())
|
|
|
|
if !marked {
|
|
// Do not log outdated rate limit remaining value
|
|
|
|
remaining := resp.Header.Get(headerRateLimitRemaining)
|
|
|
|
args = append(args, "ratelimit_remaining", remaining)
|
|
}
|
|
|
|
t.Log.V(3).Info("Seen HTTP response", args...)
|
|
}
|