include request attempt number in httpclient logs

This commit is contained in:
kim 2024-04-30 11:20:28 +01:00
parent bfc21e4850
commit 128615ea09

View file

@ -19,6 +19,7 @@ package httpclient
import (
"net/http"
"strconv"
"time"
"github.com/superseriousbusiness/gotosocial/internal/log"
@ -50,10 +51,15 @@ type Request struct {
func WrapRequest(r *http.Request) Request {
var rr Request
rr.Request = r
rr.Entry = log.WithContext(r.Context()).
WithField("method", r.Method).
WithField("url", r.URL.String()).
WithField("contentType", r.Header.Get("Content-Type"))
entry := log.WithContext(r.Context())
entry = entry.WithField("method", r.Method)
entry = entry.WithField("url", r.URL.String())
if r.Body != nil {
// Only add content-type header if a request body exists.
entry = entry.WithField("contentType", r.Header.Get("Content-Type"))
}
entry = entry.WithField("attempt", &attemptValue{&rr})
rr.Entry = entry
return rr
}
@ -67,3 +73,15 @@ func (r *Request) BackOff() time.Duration {
}
return r.backoff
}
// attemptValue wraps a Request{}.attempt value
// ptr so that each log attempt will invoke the
// .String() method, fetching current value.
type attemptValue struct{ ptr *Request }
func (v attemptValue) String() string {
if v.ptr == nil {
return "<nil>"
}
return strconv.FormatUint(uint64(v.ptr.attempts), 10)
}