Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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