Skip to content

Commit

Permalink
httptransport: add a request_id to logs
Browse files Browse the repository at this point in the history
This re-uses any trace ID that OpenTelemetry supports, or generates a
random one.

Closes: #1547
Signed-off-by: Hank Donnay <hdonnay@redhat.com>
  • Loading branch information
hdonnay committed Dec 8, 2022
1 parent 7de63a9 commit a9228d4
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
33 changes: 33 additions & 0 deletions httptransport/common.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package httptransport

import (
crand "crypto/rand"
"encoding/binary"
"errors"
"fmt"
"math/rand"
"mime"
"net/http"
"path"
"sort"
"strconv"
"strings"
"sync"

"github.com/quay/zlog"
"go.opentelemetry.io/otel/trace"

"github.com/quay/claircore"
)
Expand Down Expand Up @@ -106,3 +113,29 @@ func (a *accept) Match(mt string) bool {
t, s := mt[:i], mt[i+1:]
return a.Type == t && (a.Subtype == s || a.Subtype == "*")
}

var idPool = sync.Pool{
New: func() interface{} {
b := make([]byte, 8)
if _, err := crand.Read(b); err != nil {
panic(err) // ???
}
s := binary.LittleEndian.Uint64(b)
src := rand.NewSource(int64(s))
return rand.New(src)
},
}

func withRequestID(r *http.Request) *http.Request {
const key = `request_id`
ctx := r.Context()
sctx := trace.SpanContextFromContext(ctx)
if sctx.HasTraceID() {
ctx = zlog.ContextWithValues(ctx, key, sctx.TraceID().String())
} else {
rng := idPool.Get().(*rand.Rand)
ctx = zlog.ContextWithValues(ctx, key, fmt.Sprintf("%016x", rng.Uint64()))
idPool.Put(rng)
}
return r.WithContext(ctx)
}
2 changes: 1 addition & 1 deletion httptransport/indexer_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (h *IndexerV1) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Dur("duration", time.Since(start)).
Msg("handled HTTP request")
}()
h.inner.ServeHTTP(wr, r)
h.inner.ServeHTTP(wr, withRequestID(r))
}

func (h *IndexerV1) indexReport(w http.ResponseWriter, r *http.Request) {
Expand Down
2 changes: 1 addition & 1 deletion httptransport/matcher_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (h *MatcherV1) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Dur("duration", time.Since(start)).
Msg("handled HTTP request")
}()
h.inner.ServeHTTP(wr, r)
h.inner.ServeHTTP(wr, withRequestID(r))
}

func (h *MatcherV1) vulnerabilityReport(w http.ResponseWriter, r *http.Request) {
Expand Down
2 changes: 1 addition & 1 deletion httptransport/notification_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (h *NotificationV1) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Dur("duration", time.Since(start)).
Msg("handled HTTP request")
}()
h.inner.ServeHTTP(wr, r)
h.inner.ServeHTTP(wr, withRequestID(r))
}

func (h *NotificationV1) serveHTTP(w http.ResponseWriter, r *http.Request) {
Expand Down

0 comments on commit a9228d4

Please sign in to comment.