refactor(logging): log with the caller's context so records carry trace ids

This commit is contained in:
sam committed 2026-10-25 23:30:01 +11:00
1 parent 0611e00330
commit 656feab2a4
118 files changed
+512 -511

No files matched your search

+10 -10
View File
@@ -163,7 +163,7 @@ func (r *Receiver) ServeHTTP(w http.ResponseWriter, req *http.Request) {
body, err := io.ReadAll(io.LimitReader(req.Body, maxBodyBytes))
if err != nil {
r.log.Error("read body", "err", err)
r.log.ErrorContext(req.Context(), "read body", "err", err)
http.Error(w, "read error", http.StatusBadRequest)
return
}
@@ -172,7 +172,7 @@ func (r *Receiver) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// logged, never silently accepted.
sig := req.Header.Get("X-Hub-Signature-256")
if !webhookguard.VerifyHMAC(body, sig, r.secret) {
r.log.Warn("invalid signature", "remote", req.RemoteAddr)
r.log.WarnContext(req.Context(), "invalid signature", "remote", req.RemoteAddr)
http.Error(w, "invalid signature", http.StatusUnauthorized)
return
}
@@ -183,12 +183,12 @@ func (r *Receiver) ServeHTTP(w http.ResponseWriter, req *http.Request) {
}
event, err := r.parser.ParseWebhook(req.Context(), headers, body)
if errors.Is(err, forge.ErrBadWebhook) {
r.log.Warn("parse webhook", "err", err)
r.log.WarnContext(req.Context(), "parse webhook", "err", err)
http.Error(w, "bad payload", http.StatusBadRequest)
return
}
if err != nil {
r.log.Error("parse webhook", "err", err)
r.log.ErrorContext(req.Context(), "parse webhook", "err", err)
http.Error(w, "parse unavailable", http.StatusServiceUnavailable)
return
}
@@ -220,7 +220,7 @@ func (r *Receiver) serveIssueEvent(w http.ResponseWriter, req *http.Request, iss
// anything else is acknowledged without publishing.
if reason, withdrawn := r.withdrawal(issueEvent); withdrawn && r.withdraw != nil {
if err := r.withdraw(req.Context(), issueEvent.Owner, issueEvent.Repo, issueEvent.Number, reason); err != nil {
r.log.Error("withdraw task", "repo", issueEvent.Owner+"/"+issueEvent.Repo, "issue", issueEvent.Number, "err", err)
r.log.ErrorContext(req.Context(), "withdraw task", "repo", issueEvent.Owner+"/"+issueEvent.Repo, "issue", issueEvent.Number, "err", err)
http.Error(w, "withdraw failed", http.StatusInternalServerError)
return
}
@@ -230,12 +230,12 @@ func (r *Receiver) serveIssueEvent(w http.ResponseWriter, req *http.Request, iss
}
if err := r.publish(req.Context(), task); err != nil {
r.log.Error("publish task", "task", task.Ref(), "err", err)
r.log.ErrorContext(req.Context(), "publish task", "task", task.Ref(), "err", err)
http.Error(w, "publish failed", http.StatusInternalServerError)
return
}
r.recordPublish()
r.log.Info("published", "task", task.Ref())
r.log.InfoContext(req.Context(), "published", "task", task.Ref())
w.WriteHeader(http.StatusAccepted)
}
@@ -249,7 +249,7 @@ func (r *Receiver) serveReviewEvent(w http.ResponseWriter, req *http.Request, e
return
}
if !r.allowReactionDelivery(req.RemoteAddr) {
r.log.Warn("reaction rate limit exceeded", "remote", req.RemoteAddr)
r.log.WarnContext(req.Context(), "reaction rate limit exceeded", "remote", req.RemoteAddr)
http.Error(w, "rate limited", http.StatusTooManyRequests)
return
}
@@ -280,7 +280,7 @@ func (r *Receiver) serveReviewCommentEvent(w http.ResponseWriter, req *http.Requ
return
}
if !r.allowReactionDelivery(req.RemoteAddr) {
r.log.Warn("reaction rate limit exceeded", "remote", req.RemoteAddr)
r.log.WarnContext(req.Context(), "reaction rate limit exceeded", "remote", req.RemoteAddr)
http.Error(w, "rate limited", http.StatusTooManyRequests)
return
}
@@ -305,7 +305,7 @@ func (r *Receiver) serveReviewCommentEvent(w http.ResponseWriter, req *http.Requ
func (r *Receiver) deliverReaction(w http.ResponseWriter, req *http.Request, reaction workintake.ReviewCommentEnvelope) {
if err := r.reactionPublish(req.Context(), reaction); err != nil {
r.log.Error("publish reaction", "key", reaction.IdempotencyKey(), "err", err)
r.log.ErrorContext(req.Context(), "publish reaction", "key", reaction.IdempotencyKey(), "err", err)
http.Error(w, "publish failed", http.StatusInternalServerError)
return
}