-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
105 lines (93 loc) · 3.03 KB
/
handler.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"fmt"
"io"
"log"
"net/http"
"strings"
"time"
)
func makeHandleAll(remote string, apiClient ApiClient) func(w http.ResponseWriter, r *http.Request) {
client := http.DefaultClient
return func(w http.ResponseWriter, r *http.Request) {
var traceParent, traceState, traceId, spanId, parentId, newTraceParent, newTraceState string
var err error
tStart := time.Now()
log.Printf("%s %s %s", r.Method, r.Host, r.RequestURI)
defer r.Body.Close()
// Initialize new remote request
var req *http.Request
reqURL := "http://" + remote + r.RequestURI
log.Printf("Creating request to remote: %s %s", r.Method, reqURL)
req, err = http.NewRequest(r.Method, reqURL, r.Body)
if err != nil {
log.Printf("Error: could not create remote request: %s", err)
return
}
// Add some delay to mock processing time
time.Sleep(10 * time.Millisecond)
now := time.Now()
timestamp := fmt.Sprintf("%d", now.UnixMilli())
tDuration := now.Sub(tStart)
// Copy headers to relayed request (except W3C trace context)
for key, values := range r.Header {
keyLC := strings.ToLower(key)
if strings.Contains(keyLC, "trace") {
if len(values) != 1 {
log.Printf("Error: %s has %d values", key, len(values))
continue
}
if strings.Contains(keyLC, "parent") {
traceParent = values[0]
log.Printf("Received %s: %s", key, traceParent)
} else if strings.Contains(keyLC, "state") {
traceState = values[0]
log.Printf("Received %s: %s", key, traceState)
}
} else {
if keyLC == "newrelic" || keyLC == "user-agent" || keyLC == "content-length" {
log.Printf("%s: %s", key, strings.Join(values, "; "))
}
for _, v := range values {
req.Header.Set(key, v)
}
}
}
// Make new trace context and set headers
traceId, spanId, parentId, newTraceParent, newTraceState = apiClient.makeNewContext(traceParent, traceState, timestamp)
req.Header.Set("Traceparent", newTraceParent)
req.Header.Set("Tracestate", newTraceState)
// Send traces to NR
go func() {
traces := makeTrace(spanId, traceId, parentId, newTraceParent, newTraceState, r.RequestURI, apiClient.ServiceName,
reqURL, r.Method, 200, tDuration.Milliseconds(), now.UnixMilli())
log.Printf("Sending traceparent: %s", newTraceParent)
log.Printf("Sending tracestate: %s", newTraceState)
apiClient.sendTraces(traces)
}()
// Relay request to remote
var resp *http.Response
var body []byte
resp, err = client.Do(req)
if err != nil {
log.Printf("Error: could not send remote request: %s", err)
return
} else {
body, err = io.ReadAll(resp.Body)
if err != nil {
log.Printf("Error: could not read remote response body %v", err)
} else {
for key, values := range resp.Header {
for _, v := range values {
w.Header().Set(key, v)
}
}
log.Printf("Forwarding %s %s %d bytes", r.Method, reqURL, len(body))
_, err = w.Write(body)
if err != nil {
log.Printf("Error: could not write local with remote response (%d bytes) %v", len(body), err)
}
}
}
}
}