-
Notifications
You must be signed in to change notification settings - Fork 153
/
metrics.go
217 lines (187 loc) · 5.48 KB
/
metrics.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package apihandler
import (
"bufio"
"errors"
"io"
"net"
"net/http"
"strconv"
"strings"
"time"
"github.com/wundergraph/wundergraph/pkg/metrics"
)
const (
metricsNamespace = "wundernode"
)
var (
_ http.ResponseWriter = (*responseWriter)(nil)
_ http.Flusher = (*responseWriter)(nil)
_ http.CloseNotifier = (*responseWriter)(nil)
_ http.Hijacker = (*responseWriter)(nil)
_ io.ReaderFrom = (*responseWriter)(nil)
)
type responseWriter struct {
statusCode int
written int64
w http.ResponseWriter
}
func (w *responseWriter) Header() http.Header {
return w.w.Header()
}
func (w *responseWriter) WriteHeader(statusCode int) {
w.statusCode = statusCode
w.w.WriteHeader(statusCode)
}
func (w *responseWriter) Write(data []byte) (int, error) {
if w.statusCode == 0 {
w.statusCode = http.StatusOK
}
n, err := w.w.Write(data)
w.written += int64(n)
return n, err
}
func (w *responseWriter) Flush() {
if f, ok := w.w.(http.Flusher); ok {
f.Flush()
}
}
func (w *responseWriter) CloseNotify() <-chan bool {
if cn, ok := w.w.(http.CloseNotifier); ok {
return cn.CloseNotify()
}
return nil
}
func (w *responseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
if h, ok := w.w.(http.Hijacker); ok {
return h.Hijack()
}
return nil, nil, errors.New("underlying http.ResponseWriter is not an http.Hijacker")
}
func (w *responseWriter) ReadFrom(r io.Reader) (int64, error) {
if w.statusCode == 0 {
w.statusCode = http.StatusOK
}
var n int64
var err error
if rf, ok := w.w.(io.ReaderFrom); ok {
n, err = rf.ReadFrom(r)
} else {
n, err = io.Copy(w.w, r)
}
w.written += n
return n, err
}
// operationMetrics represents an interface type that provides handlers
// for collecting operation metrics
type operationMetrics interface {
// Handler wraps the given http.Handler returning a new http.Handler which
// counts requests to the given operation
Handler(operationHandler http.Handler) http.Handler
}
type operationMetricsHandler struct {
counter metrics.CounterVec
requestDuration metrics.SummaryVec
requestSize metrics.SummaryVec
responseSize metrics.SummaryVec
}
func (h *operationMetricsHandler) estimateRequestSize(r *http.Request) int {
s := len(r.Method) + len(r.Proto)
for name, values := range r.Header {
s += len(name)
for _, value := range values {
s += len(value)
}
}
s += len(r.Host)
if r.ContentLength != -1 {
s += int(r.ContentLength)
}
return s
}
func (h *operationMetricsHandler) Handler(operationHandler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
ww := &responseWriter{
w: w,
}
operationHandler.ServeHTTP(ww, r)
elapsed := time.Since(start)
// XXX: Must match the number of dynamic labels used at creation
h.counter.Inc(strconv.Itoa(ww.statusCode))
h.requestDuration.Observe(elapsed.Seconds())
h.requestSize.Observe(float64(h.estimateRequestSize(r)))
h.responseSize.Observe(float64(ww.written))
})
}
// newOperationMetrics returns a new operationMetrics which can count requests.
func newOperationMetrics(m metrics.Metrics, operationName string) operationMetrics {
// XXX: Dynamic label counts must match between here and their uses in Handler()
constLabels := metrics.Labels{
"operationName": operationName,
}
counter := m.NewCounterVec(metrics.MetricOpts{
Namespace: metricsNamespace,
Name: "http_requests_total",
Help: "Total number of served HTTP requests",
ConstLabels: constLabels,
}, "statusCode")
requestDuration := m.NewSummaryVec(metrics.MetricOpts{
Namespace: metricsNamespace,
Name: "http_request_duration_seconds",
Help: "Duration of HTTP requests",
ConstLabels: constLabels,
})
requestSize := m.NewSummaryVec(metrics.MetricOpts{
Namespace: metricsNamespace,
Name: "http_response_size_bytes",
Help: "Size of served HTTP responses",
ConstLabels: constLabels,
})
responseSize := m.NewSummaryVec(metrics.MetricOpts{
Namespace: metricsNamespace,
Name: "http_request_size_bytes",
Help: "Size of received HTTP requests",
ConstLabels: constLabels,
})
return &operationMetricsHandler{
counter: counter,
requestDuration: requestDuration,
requestSize: requestSize,
responseSize: responseSize,
}
}
type outgoingRequestCounter struct {
requestsCounter metrics.CounterVec
requestDuration metrics.SummaryVec
}
func newOutgoingRequestCounter(m metrics.Metrics) *outgoingRequestCounter {
const (
subsystem = "api_transport"
)
requestCounter := m.NewCounterVec(metrics.MetricOpts{
Namespace: metricsNamespace,
Subsystem: subsystem,
Name: "outgoing_http_requests_total",
Help: "Outgoing HTTP requests sent to upstream APIs",
}, "host", "method", "statusCode")
requestDuration := m.NewSummaryVec(metrics.MetricOpts{
Namespace: metricsNamespace,
Subsystem: subsystem,
Name: "outgoing_http_request_duration",
Help: "Duration of outgoing HTTP requests sent to upstream APIs",
}, "host", "method", "statusCode")
return &outgoingRequestCounter{
requestsCounter: requestCounter,
requestDuration: requestDuration,
}
}
func (c *outgoingRequestCounter) Inc(r *http.Request, resp *http.Response, duration time.Duration) {
host := r.Host
if host == "" {
host = r.URL.Host
}
method := strings.ToUpper(r.Method)
statusCode := strconv.Itoa(resp.StatusCode)
c.requestsCounter.Inc(host, method, statusCode)
c.requestDuration.Observe(duration.Seconds(), host, method, statusCode)
}