-
-
Notifications
You must be signed in to change notification settings - Fork 564
/
Copy pathsegment.go
161 lines (141 loc) Β· 3.62 KB
/
segment.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package xray
import (
"bufio"
"fmt"
"net"
"net/http"
"strings"
"goa.design/goa/middleware/xray"
)
type (
// HTTPSegment represents an AWS X-Ray segment document for HTTP services.
// It wraps the AWS X-Ray segment with the http response writer.
HTTPSegment struct {
*xray.Segment
http.ResponseWriter
}
)
// RecordRequest traces a request.
//
// It sets Http.Request & Namespace (ex: "remote")
func (s *HTTPSegment) RecordRequest(req *http.Request, namespace string) {
s.Lock()
defer s.Unlock()
if s.HTTP == nil {
s.HTTP = &xray.HTTP{}
}
s.Namespace = namespace
s.HTTP.Request = requestData(req)
}
// RecordResponse traces a response.
//
// It sets Throttle, Fault, Error and HTTP.Response
func (s *HTTPSegment) RecordResponse(resp *http.Response) {
s.Lock()
defer s.Unlock()
if s.HTTP == nil {
s.HTTP = &xray.HTTP{}
}
s.recordStatusCode(resp.StatusCode)
s.HTTP.Response = responseData(resp)
}
// WriteHeader records the HTTP response code and calls the corresponding
// ResponseWriter method.
func (s *HTTPSegment) WriteHeader(code int) {
s.Lock()
defer s.Unlock()
if s.HTTP == nil {
s.HTTP = &xray.HTTP{}
}
if s.HTTP.Response == nil {
s.HTTP.Response = &xray.Response{}
}
s.HTTP.Response.Status = code
s.recordStatusCode(code)
s.ResponseWriter.WriteHeader(code)
}
// Write records the HTTP response content length and error (if any)
// and calls the corresponding ResponseWriter method.
func (s *HTTPSegment) Write(p []byte) (int, error) {
s.Lock()
n, err := s.ResponseWriter.Write(p)
s.Unlock()
if err != nil {
s.RecordError(err)
}
s.Lock()
defer s.Unlock()
if s.HTTP == nil {
s.HTTP = &xray.HTTP{}
}
if s.HTTP.Response == nil {
s.HTTP.Response = &xray.Response{}
}
s.HTTP.Response.ContentLength = int64(n)
return n, err
}
// Hijack supports the http.Hijacker interface.
func (s *HTTPSegment) Hijack() (net.Conn, *bufio.ReadWriter, error) {
if hijacker, ok := s.ResponseWriter.(http.Hijacker); ok {
return hijacker.Hijack()
}
return nil, nil, fmt.Errorf("xray: inner ResponseWriter cannot be hijacked: %T", s.ResponseWriter)
}
// recordStatusCode sets Throttle, Fault, Error
//
// It is expected that the mutex has already been locked when calling this method.
func (s *HTTPSegment) recordStatusCode(statusCode int) {
switch {
case statusCode == http.StatusTooManyRequests:
s.Throttle = true
case statusCode >= 400 && statusCode < 500:
s.Fault = true
case statusCode >= 500:
s.Error = true
}
}
// requestData creates a Request from a http.Request.
func requestData(req *http.Request) *xray.Request {
var (
scheme = "http"
host = req.Host
)
if len(req.URL.Scheme) > 0 {
scheme = req.URL.Scheme
}
if len(req.URL.Host) > 0 {
host = req.URL.Host
}
return &xray.Request{
Method: req.Method,
URL: fmt.Sprintf("%s://%s%s", scheme, host, req.URL.Path),
ClientIP: getIP(req),
UserAgent: req.UserAgent(),
ContentLength: req.ContentLength,
}
}
// responseData creates a Response from a http.Response.
func responseData(resp *http.Response) *xray.Response {
return &xray.Response{
Status: resp.StatusCode,
ContentLength: resp.ContentLength,
}
}
// getIP implements a heuristic that returns an origin IP address for a request.
func getIP(req *http.Request) string {
for _, h := range []string{"X-Forwarded-For", "X-Real-Ip"} {
for _, ip := range strings.Split(req.Header.Get(h), ",") {
if len(ip) == 0 {
continue
}
realIP := net.ParseIP(strings.Replace(ip, " ", "", -1))
return realIP.String()
}
}
// not found in header
host, _, err := net.SplitHostPort(req.RemoteAddr)
if err != nil {
return req.RemoteAddr
}
return host
}