-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
183 lines (147 loc) · 4.22 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package isuhttp
import (
"bufio"
"fmt"
"io"
"net"
"net/http"
"strconv"
"strings"
"time"
isutools "github.com/mazrean/isucon-go-tools"
isuhttpgen "github.com/mazrean/isucon-go-tools/http/internal/generate"
)
func ListenAndServe(addr string, handler http.Handler) error {
if isutools.Enable {
handler = StdMetricsMiddleware(handler)
}
listener, err := listen(addr)
if err != nil {
return err
}
return http.Serve(listener, handler)
}
func ListenAndServeTLS(addr, certFile, keyFile string, handler http.Handler) error {
if isutools.Enable {
handler = StdMetricsMiddleware(handler)
}
listener, err := listen(addr)
if err != nil {
return err
}
return http.ServeTLS(listener, handler, certFile, keyFile)
}
func ServerListenAndServe(server *http.Server) error {
if isutools.Enable {
server.Handler = StdMetricsMiddleware(server.Handler)
}
listener, err := listen(server.Addr)
if err != nil {
return err
}
return server.Serve(listener)
}
func ServerListenAndServeTLS(server *http.Server, certFile, keyFile string) error {
if isutools.Enable {
server.Handler = StdMetricsMiddleware(server.Handler)
}
listener, err := listen(server.Addr)
if err != nil {
return err
}
return server.ServeTLS(listener, certFile, keyFile)
}
func listen(addr string) (net.Listener, error) {
listener, ok, err := newUnixDomainSockListener()
if err != nil {
return nil, err
}
if !ok {
if addr == "" {
addr = ":http"
}
listener, err = newTCPListener(addr)
if err != nil {
return nil, fmt.Errorf("failed to listen on %s: %w", addr, err)
}
}
return listener, nil
}
type responseWriterWithMetrics struct {
http.ResponseWriter
responseWriterMetrics
}
type responseWriterMetrics struct {
statusCode int
resSize float64
}
func newResponseWriterWithMetrics(w http.ResponseWriter) *responseWriterWithMetrics {
return &responseWriterWithMetrics{
ResponseWriter: w,
responseWriterMetrics: responseWriterMetrics{
statusCode: 200,
resSize: 0,
},
}
}
func (r *responseWriterWithMetrics) WriteHeader(code int) {
r.statusCode = code
r.ResponseWriter.WriteHeader(code)
}
func (r *responseWriterWithMetrics) Write(b []byte) (int, error) {
n, err := r.ResponseWriter.Write(b)
r.resSize += float64(n)
return n, err
}
func (r *responseWriterWithMetrics) CloseNotify() <-chan bool {
return r.ResponseWriter.(http.CloseNotifier).CloseNotify()
}
func (r *responseWriterWithMetrics) Flush() {
r.ResponseWriter.(http.Flusher).Flush()
}
func (r *responseWriterWithMetrics) Hijack() (net.Conn, *bufio.ReadWriter, error) {
return r.ResponseWriter.(http.Hijacker).Hijack()
}
func (r *responseWriterWithMetrics) ReadFrom(src io.Reader) (int64, error) {
return r.ResponseWriter.(io.ReaderFrom).ReadFrom(src)
}
func StdMetricsMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
if !isutools.Enable {
next.ServeHTTP(res, req)
return
}
var metrics *responseWriterMetrics
wrappedRes := isuhttpgen.ResponseWriterWrapper(res, func(w http.ResponseWriter) isuhttpgen.ResponseWriter {
rw := newResponseWriterWithMetrics(w)
metrics = &rw.responseWriterMetrics
return rw
})
path := FilterFunc(req.URL.Path)
host := req.Host
method := req.Method
reqSz := reqSize(req)
// シナリオ解析用メトリクス
flowCookie, err := req.Cookie("isutools_flow")
if err == nil {
flowMethod, flowPath, ok := strings.Cut(flowCookie.Value, ",")
if ok {
flowCounterVec.WithLabelValues(flowMethod, flowPath, method, path).Inc()
}
} else {
flowCookie = new(http.Cookie)
flowCookie.Name = "isutools_flow"
}
flowCookie.Value = fmt.Sprintf("%s,%s", method, path)
flowCookie.Expires = time.Now().Add(1 * time.Hour)
http.SetCookie(res, flowCookie)
start := time.Now()
next.ServeHTTP(wrappedRes, req)
reqDur := float64(time.Since(start)) / float64(time.Second)
statusCode := strconv.Itoa(metrics.statusCode)
reqSizeHistogramVec.WithLabelValues(statusCode, method, path).Observe(reqSz)
reqDurHistogramVec.WithLabelValues(statusCode, method, path).Observe(reqDur)
reqCounterVec.WithLabelValues(statusCode, method, host, path).Inc()
resSizeHistogramVec.WithLabelValues(statusCode, method, path).Observe(metrics.resSize)
})
}