Skip to content

Commit 4d45710

Browse files
authored
ensure log messages have requestID where possible (sigstore#907)
Signed-off-by: Bob Callaway <bcallaway@google.com>
1 parent 45becc9 commit 4d45710

File tree

5 files changed

+23
-21
lines changed

5 files changed

+23
-21
lines changed

pkg/api/entries.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func logEntryFromLeaf(ctx context.Context, signer signature.Signer, tc TrillianC
113113
if attKey != "" {
114114
att, fetchErr = storageClient.FetchAttestation(ctx, attKey)
115115
if fetchErr != nil {
116-
log.Logger.Errorf("error fetching attestation by key, trying by UUID: %s %w", attKey, fetchErr)
116+
log.ContextLogger(ctx).Errorf("error fetching attestation by key, trying by UUID: %s %w", attKey, fetchErr)
117117
}
118118
}
119119
// if looking up by key failed or we weren't able to generate a key, try looking up by uuid
@@ -125,7 +125,7 @@ func logEntryFromLeaf(ctx context.Context, signer signature.Signer, tc TrillianC
125125
}
126126
att, fetchErr = storageClient.FetchAttestation(ctx, entryIDstruct.UUID)
127127
if fetchErr != nil {
128-
log.Logger.Errorf("error fetching attestation by uuid: %s %v", entryIDstruct.UUID, fetchErr)
128+
log.ContextLogger(ctx).Errorf("error fetching attestation by uuid: %s %v", entryIDstruct.UUID, fetchErr)
129129
}
130130
}
131131
if fetchErr == nil {
@@ -149,7 +149,7 @@ func GetLogEntryByIndexHandler(params entries.GetLogEntryByIndexParams) middlewa
149149
ctx := params.HTTPRequest.Context()
150150
tid, resolvedIndex := api.logRanges.ResolveVirtualIndex(int(params.LogIndex))
151151
tc := NewTrillianClientFromTreeID(ctx, tid)
152-
log.RequestIDLogger(params.HTTPRequest).Debugf("Retrieving resolved index %v from TreeID %v", resolvedIndex, tid)
152+
log.ContextLogger(ctx).Debugf("Retrieving resolved index %v from TreeID %v", resolvedIndex, tid)
153153

154154
resp := tc.getLeafAndProofByIndex(resolvedIndex)
155155
switch resp.status {
@@ -238,12 +238,12 @@ func createLogEntry(params entries.CreateLogEntryParams) (models.LogEntry, middl
238238
go func() {
239239
keys, err := entry.IndexKeys()
240240
if err != nil {
241-
log.RequestIDLogger(params.HTTPRequest).Error(err)
241+
log.ContextLogger(ctx).Error(err)
242242
return
243243
}
244244
for _, key := range keys {
245245
if err := addToIndex(context.Background(), key, entryID); err != nil {
246-
log.RequestIDLogger(params.HTTPRequest).Error(err)
246+
log.ContextLogger(ctx).Error(err)
247247
}
248248
}
249249
}()
@@ -254,14 +254,14 @@ func createLogEntry(params entries.CreateLogEntryParams) (models.LogEntry, middl
254254
go func() {
255255
attKey, attVal := entry.AttestationKeyValue()
256256
if attVal == nil {
257-
log.RequestIDLogger(params.HTTPRequest).Infof("no attestation for %s", uuid)
257+
log.ContextLogger(ctx).Infof("no attestation for %s", uuid)
258258
return
259259
}
260260
if err := storeAttestation(context.Background(), attKey, attVal); err != nil {
261261
// entryIDstruct.UUID
262-
log.RequestIDLogger(params.HTTPRequest).Errorf("error storing attestation: %s", err)
262+
log.ContextLogger(ctx).Errorf("error storing attestation: %s", err)
263263
} else {
264-
log.RequestIDLogger(params.HTTPRequest).Infof("stored attestation for uuid %s with filename %s", entryIDstruct.UUID, attKey)
264+
log.ContextLogger(ctx).Infof("stored attestation for uuid %s with filename %s", entryIDstruct.UUID, attKey)
265265
}
266266
}()
267267
}
@@ -490,7 +490,7 @@ func retrieveUUIDFromTree(ctx context.Context, uuid string, tid int64) (models.L
490490
}
491491

492492
tc := NewTrillianClientFromTreeID(ctx, tid)
493-
log.Logger.Debugf("Attempting to retrieve UUID %v from TreeID %v", uuid, tid)
493+
log.ContextLogger(ctx).Debugf("Attempting to retrieve UUID %v from TreeID %v", uuid, tid)
494494

495495
resp := tc.getLeafAndProofByHash(hashValue)
496496
switch resp.status {
@@ -510,6 +510,7 @@ func retrieveUUIDFromTree(ctx context.Context, uuid string, tid int64) (models.L
510510
case codes.NotFound:
511511
return models.LogEntry{}, ErrNotFound
512512
default:
513+
log.ContextLogger(ctx).Errorf("Unexpected response code while attempting to retrieve UUID %v from TreeID %v: %v", uuid, tid, resp.status)
513514
return models.LogEntry{}, errors.New("unexpected error")
514515
}
515516
}

pkg/api/error.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,11 @@ func handleRekorAPIError(params interface{}, code int, err error, message string
6767
handler := re.FindStringSubmatch(typeStr)[1]
6868

6969
logMsg := func(r *http.Request) {
70-
log.RequestIDLogger(r).Errorw("exiting with error", append([]interface{}{"handler", handler, "statusCode", code, "clientMessage", message, "error", err}, fields...)...)
70+
ctx := r.Context()
71+
log.ContextLogger(ctx).Errorw("error processing request", append([]interface{}{"handler", handler, "statusCode", code, "clientMessage", message, "error", err}, fields...)...)
7172
paramsFields := map[string]interface{}{}
7273
if err := mapstructure.Decode(params, &paramsFields); err == nil {
73-
log.RequestIDLogger(r).Debug(paramsFields)
74+
log.ContextLogger(ctx).Debug(paramsFields)
7475
}
7576
}
7677

pkg/generated/restapi/configure_rekor_server.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func setupGlobalMiddleware(handler http.Handler) http.Handler {
159159
ctx := r.Context()
160160
r = r.WithContext(log.WithRequestID(ctx, middleware.GetReqID(ctx)))
161161
defer func() {
162-
_ = log.RequestIDLogger(r).Sync()
162+
_ = log.ContextLogger(ctx).Sync()
163163
}()
164164

165165
returnHandler.ServeHTTP(w, r)
@@ -196,14 +196,15 @@ func cacheForever(handler http.Handler) http.Handler {
196196
}
197197

198198
func logAndServeError(w http.ResponseWriter, r *http.Request, err error) {
199+
ctx := r.Context()
199200
if apiErr, ok := err.(errors.Error); ok && apiErr.Code() == http.StatusNotFound {
200-
log.RequestIDLogger(r).Warn(err)
201+
log.ContextLogger(ctx).Warn(err)
201202
} else {
202-
log.RequestIDLogger(r).Error(err)
203+
log.ContextLogger(ctx).Error(err)
203204
}
204205
requestFields := map[string]interface{}{}
205206
if err := mapstructure.Decode(r, &requestFields); err == nil {
206-
log.RequestIDLogger(r).Debug(requestFields)
207+
log.ContextLogger(ctx).Debug(requestFields)
207208
}
208209
errors.ServeError(w, r, err)
209210
}

pkg/log/log.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package log
1818
import (
1919
"context"
2020
"log"
21-
"net/http"
2221

2322
"github.com/go-chi/chi/middleware"
2423
"go.uber.org/zap"
@@ -69,10 +68,10 @@ func WithRequestID(ctx context.Context, id string) context.Context {
6968
return context.WithValue(ctx, middleware.RequestIDKey, id)
7069
}
7170

72-
func RequestIDLogger(r *http.Request) *zap.SugaredLogger {
71+
func ContextLogger(ctx context.Context) *zap.SugaredLogger {
7372
proposedLogger := Logger
74-
if r != nil {
75-
if ctxRequestID, ok := r.Context().Value(middleware.RequestIDKey).(string); ok {
73+
if ctx != nil {
74+
if ctxRequestID, ok := ctx.Value(middleware.RequestIDKey).(string); ok {
7675
proposedLogger = proposedLogger.With(zap.String("requestID", ctxRequestID))
7776
}
7877
}

pkg/storage/storage.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ type Blob struct {
5555
}
5656

5757
func (b *Blob) StoreAttestation(ctx context.Context, key string, attestation []byte) error {
58-
log.Logger.Infof("storing attestation at %s", key)
58+
log.ContextLogger(ctx).Infof("storing attestation at %s", key)
5959
w, err := b.bucket.NewWriter(ctx, key, nil)
6060
if err != nil {
6161
return err
@@ -67,7 +67,7 @@ func (b *Blob) StoreAttestation(ctx context.Context, key string, attestation []b
6767
}
6868

6969
func (b *Blob) FetchAttestation(ctx context.Context, key string) ([]byte, error) {
70-
log.Logger.Infof("fetching attestation %s", key)
70+
log.ContextLogger(ctx).Infof("fetching attestation %s", key)
7171
exists, err := b.bucket.Exists(ctx, key)
7272
if err != nil {
7373
return nil, err

0 commit comments

Comments
 (0)