-
Notifications
You must be signed in to change notification settings - Fork 13
/
requestid.go
54 lines (43 loc) · 1.39 KB
/
requestid.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
49
50
51
52
53
54
/* Copyright (c) 2018 Salesforce
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
package hcontext
import (
"context"
"fmt"
"net/http"
"github.com/google/uuid"
)
type idkey int
var ridKey idkey
var headersToSearch = []string{
"Request-Id", "X-Request-Id",
"Request-ID", "X-Request-ID",
}
// FromRequest fetches the given request's request ID from the Headers.
// If one is found, it appends a new request ID and sets the comma separated value as the header.
// If one is not found, it sets a new request ID as the header.
func FromRequest(r *http.Request) (id string, ok bool) {
newRID := uuid.New().String()
for _, try := range headersToSearch {
if id = r.Header.Get(try); id != "" {
newRID = fmt.Sprintf("%s,%s", newRID, id)
r.Header.Set("X-Request-Id", newRID)
return newRID, true
}
}
r.Header.Set("X-Request-Id", newRID)
return newRID, false
}
// WithRequestID adds the given request ID to a context for processing later
// down the chain.
func WithRequestID(ctx context.Context, id string) context.Context {
return context.WithValue(ctx, ridKey, id)
}
// RequestIDFromContext fetches a request ID from the given context if it exists.
func RequestIDFromContext(ctx context.Context) (id string, ok bool) {
id, ok = ctx.Value(ridKey).(string)
return
}