-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
trace.go
48 lines (42 loc) · 1.15 KB
/
trace.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package rest
import (
"context"
"crypto/rand"
"crypto/sha1" //nolint:gosec //not used for cryptography
"encoding/hex"
"fmt"
"net/http"
"time"
)
type contextKey string
const traceHeader = "X-Request-ID"
// Trace looks for header X-Request-ID and makes it as random id if not found, then populates it to the result's header
// and to request context
func Trace(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
traceID := r.Header.Get(traceHeader)
if traceID == "" {
traceID = randToken()
}
w.Header().Set(traceHeader, traceID)
ctx := context.WithValue(r.Context(), contextKey("requestID"), traceID)
r = r.WithContext(ctx)
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
// GetTraceID returns request id from the context
func GetTraceID(r *http.Request) string {
if id, ok := r.Context().Value(contextKey("requestID")).(string); ok {
return id
}
return ""
}
func randToken() string {
b := make([]byte, 32)
if _, err := rand.Read(b); err != nil {
return fmt.Sprintf("%x", time.Now().Nanosecond())
}
sum := sha1.Sum(b) //nolint:gosec //not used for cryptography
return hex.EncodeToString(sum[:])
}