Skip to content

Commit f26c908

Browse files
committed
httptransport: add a request_id to logs
This re-uses any trace ID that OpenTelemetry supports, or generates a weakly random one based on some request features. Closes: #1547 Signed-off-by: Hank Donnay <[email protected]>
1 parent 4b37dcd commit f26c908

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

httptransport/common.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
package httptransport
22

33
import (
4+
"encoding/hex"
45
"errors"
56
"fmt"
7+
"hash"
8+
"hash/fnv"
9+
"io"
610
"mime"
711
"net/http"
812
"path"
913
"sort"
1014
"strconv"
1115
"strings"
16+
"sync"
17+
18+
"github.com/quay/zlog"
19+
"go.opentelemetry.io/otel/trace"
1220

1321
"github.com/quay/claircore"
1422
)
@@ -106,3 +114,24 @@ func (a *accept) Match(mt string) bool {
106114
t, s := mt[:i], mt[i+1:]
107115
return a.Type == t && (a.Subtype == s || a.Subtype == "*")
108116
}
117+
118+
var idPool = sync.Pool{
119+
New: func() interface{} { return fnv.New64a() },
120+
}
121+
122+
func withRequestID(r *http.Request) *http.Request {
123+
const key = `request_id`
124+
ctx := r.Context()
125+
sctx := trace.SpanContextFromContext(ctx)
126+
if sctx.HasTraceID() {
127+
ctx = zlog.ContextWithValues(ctx, key, sctx.TraceID().String())
128+
} else {
129+
h := idPool.Get().(hash.Hash64)
130+
h.Reset()
131+
io.WriteString(h, r.RequestURI)
132+
io.WriteString(h, r.RemoteAddr)
133+
ctx = zlog.ContextWithValues(ctx, key, hex.EncodeToString(h.Sum(nil)))
134+
idPool.Put(h)
135+
}
136+
return r.WithContext(ctx)
137+
}

httptransport/indexer_v1.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (h *IndexerV1) ServeHTTP(w http.ResponseWriter, r *http.Request) {
6868
Dur("duration", time.Since(start)).
6969
Msg("handled HTTP request")
7070
}()
71-
h.inner.ServeHTTP(wr, r)
71+
h.inner.ServeHTTP(wr, withRequestID(r))
7272
}
7373

7474
func (h *IndexerV1) indexReport(w http.ResponseWriter, r *http.Request) {

httptransport/matcher_v1.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (h *MatcherV1) ServeHTTP(w http.ResponseWriter, r *http.Request) {
7676
Dur("duration", time.Since(start)).
7777
Msg("handled HTTP request")
7878
}()
79-
h.inner.ServeHTTP(wr, r)
79+
h.inner.ServeHTTP(wr, withRequestID(r))
8080
}
8181

8282
func (h *MatcherV1) vulnerabilityReport(w http.ResponseWriter, r *http.Request) {

httptransport/notification_v1.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (h *NotificationV1) ServeHTTP(w http.ResponseWriter, r *http.Request) {
6868
Dur("duration", time.Since(start)).
6969
Msg("handled HTTP request")
7070
}()
71-
h.inner.ServeHTTP(wr, r)
71+
h.inner.ServeHTTP(wr, withRequestID(r))
7272
}
7373

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

0 commit comments

Comments
 (0)