-
Notifications
You must be signed in to change notification settings - Fork 5
/
http.go
88 lines (73 loc) · 2.44 KB
/
http.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package trace
import (
"context"
"fmt"
"net/http"
"strconv"
"github.com/rs/zerolog/log"
)
const (
headerTraceID = "X-TRACEID"
headerProbabilitySample = "X-PROBABILITYSAMPLE"
)
// GetFromHTTPRequest returns a Trace using the trace
// ID and the probability sample get from the header of @r
func GetFromHTTPRequest(r *http.Request) Trace {
idStr := r.Header.Get(headerTraceID)
id := decode([]byte(idStr))
probabilitySampleStr := r.Header.Get(headerProbabilitySample)
probabilitySample, err := strconv.ParseFloat(probabilitySampleStr, 64)
var probabilitySamplePtr *float64
if err == nil {
probabilitySamplePtr = &probabilitySample
}
return Trace{
ID: id,
ProbabilitySample: probabilitySamplePtr,
}
}
// SetInHTTPRequest sets the header of @request using the
// trace in the @ctx. If @trace is empty or @request is nil, nothing will happen
func SetInHTTPRequest(ctx context.Context, request *http.Request) {
if request == nil {
log.Warn().
Str("method", "trace.SetInHTTPRequest").
Msg("[FoundationKit] Request is nil. Nothing will happen")
return
}
trace := GetFromContext(ctx)
request.Header.Set(headerTraceID, trace.ID.String())
request.Header.Set(headerProbabilitySample, fmt.Sprintf("%f", *trace.ProbabilitySample))
}
// GetFromHTTPResponse returns a Trace using the trace
// ID and the probability sample get from the header of @r
func GetFromHTTPResponse(r *http.Response) Trace {
idStr := r.Header.Get(headerTraceID)
id := decode([]byte(idStr))
probabilitySampleStr := r.Header.Get(headerProbabilitySample)
probabilitySample, err := strconv.ParseFloat(probabilitySampleStr, 64)
var probabilitySamplePtr *float64
if err == nil {
probabilitySamplePtr = &probabilitySample
}
return Trace{
ID: id,
ProbabilitySample: probabilitySamplePtr,
}
}
// SetInHTTPResponse sets the header of @response using @trace.
// If @trace is empty or @response is nil, nothing will happen
func SetInHTTPResponse(trace Trace, response http.ResponseWriter) {
if response == nil {
log.Warn().
Str("method", "trace.SetInHTTPResponse").
Msg("[FoundationKit] Response is nil. Nothing will happen")
return
}
if trace.isEmpty() {
log.Warn().Msg("[FoundationKit] Trace has some empty field. Creating a new one...")
}
trace = ensureTraceNotEmpty(trace)
response.Header().Set(headerTraceID, trace.ID.String())
response.Header().Set(headerProbabilitySample, fmt.Sprintf("%f", *trace.ProbabilitySample))
}