forked from cloudfoundry/gorouter
/
varz.go
297 lines (232 loc) · 6.47 KB
/
varz.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package varz
import (
"encoding/json"
"fmt"
"net/http"
"sync"
"time"
"github.com/cloudfoundry/gorouter/registry"
"github.com/cloudfoundry/gorouter/route"
"github.com/cloudfoundry/gorouter/stats"
metrics "github.com/rcrowley/go-metrics"
)
type topAppsEntry struct {
ApplicationId string `json:"application_id"`
RequestsPerSecond int64 `json:"rps"`
RequestsPerMinute int64 `json:"rpm"`
}
type varz struct {
All *HttpMetric `json:"all"`
Tags struct {
Component TaggedHttpMetric `json:"component"`
} `json:"tags"`
Urls int `json:"urls"`
Droplets int `json:"droplets"`
BadRequests int `json:"bad_requests"`
BadGateways int `json:"bad_gateways"`
RequestsPerSec float64 `json:"requests_per_sec"`
TopApps []topAppsEntry `json:"top10_app_requests"`
MillisSinceLastRegistryUpdate int64 `json:"ms_since_last_registry_update"`
}
type httpMetric struct {
Requests int64 `json:"requests"`
Rate [3]float64 `json:"rate"`
Responses2xx int64 `json:"responses_2xx"`
Responses3xx int64 `json:"responses_3xx"`
Responses4xx int64 `json:"responses_4xx"`
Responses5xx int64 `json:"responses_5xx"`
ResponsesXxx int64 `json:"responses_xxx"`
Latency map[string]float64 `json:"latency"`
}
type HttpMetric struct {
Requests metrics.Counter
Rate metrics.Meter
Responses2xx metrics.Counter
Responses3xx metrics.Counter
Responses4xx metrics.Counter
Responses5xx metrics.Counter
ResponsesXxx metrics.Counter
Latency metrics.Histogram
}
func NewHttpMetric() *HttpMetric {
x := &HttpMetric{
Requests: metrics.NewCounter(),
Rate: metrics.NewMeter(),
Responses2xx: metrics.NewCounter(),
Responses3xx: metrics.NewCounter(),
Responses4xx: metrics.NewCounter(),
Responses5xx: metrics.NewCounter(),
ResponsesXxx: metrics.NewCounter(),
Latency: metrics.NewHistogram(metrics.NewExpDecaySample(1028, 0.015)),
}
return x
}
func (x *HttpMetric) MarshalJSON() ([]byte, error) {
y := httpMetric{}
y.Requests = x.Requests.Count()
y.Rate[0] = x.Rate.Rate1()
y.Rate[1] = x.Rate.Rate5()
y.Rate[2] = x.Rate.Rate15()
y.Responses2xx = x.Responses2xx.Count()
y.Responses3xx = x.Responses3xx.Count()
y.Responses4xx = x.Responses4xx.Count()
y.Responses5xx = x.Responses5xx.Count()
y.ResponsesXxx = x.ResponsesXxx.Count()
p := []float64{0.50, 0.75, 0.90, 0.95, 0.99}
z := x.Latency.Percentiles(p)
y.Latency = make(map[string]float64)
for i, e := range p {
y.Latency[fmt.Sprintf("%d", int(e*100))] = z[i] / float64(time.Second)
}
// Add fields for backwards compatibility with the collector
y.Latency["value"] = p[0] / float64(time.Millisecond)
y.Latency["samples"] = 1
return json.Marshal(y)
}
func (x *HttpMetric) CaptureRequest() {
x.Requests.Inc(1)
x.Rate.Mark(1)
}
func (x *HttpMetric) CaptureResponse(response *http.Response, duration time.Duration) {
var statusCode int
if response != nil {
statusCode = response.StatusCode / 100
}
switch statusCode {
case 2:
x.Responses2xx.Inc(1)
case 3:
x.Responses3xx.Inc(1)
case 4:
x.Responses4xx.Inc(1)
case 5:
x.Responses5xx.Inc(1)
default:
x.ResponsesXxx.Inc(1)
}
x.Latency.Update(duration.Nanoseconds())
}
type TaggedHttpMetric map[string]*HttpMetric
func NewTaggedHttpMetric() TaggedHttpMetric {
x := make(TaggedHttpMetric)
return x
}
func (x TaggedHttpMetric) httpMetric(t string) *HttpMetric {
y := x[t]
if y == nil {
y = NewHttpMetric()
x[t] = y
}
return y
}
func (x TaggedHttpMetric) CaptureRequest(t string) {
x.httpMetric(t).CaptureRequest()
}
func (x TaggedHttpMetric) CaptureResponse(t string, y *http.Response, z time.Duration) {
x.httpMetric(t).CaptureResponse(y, z)
}
type Varz interface {
json.Marshaler
ActiveApps() *stats.ActiveApps
CaptureBadRequest(req *http.Request)
CaptureBadGateway(req *http.Request)
CaptureRoutingRequest(b *route.Endpoint, req *http.Request)
CaptureRoutingResponse(b *route.Endpoint, res *http.Response, startedAt time.Time, d time.Duration)
}
type RealVarz struct {
sync.Mutex
r *registry.RouteRegistry
activeApps *stats.ActiveApps
topApps *stats.TopApps
varz
}
func NewVarz(r *registry.RouteRegistry) Varz {
x := &RealVarz{r: r}
x.activeApps = stats.NewActiveApps()
x.topApps = stats.NewTopApps()
x.All = NewHttpMetric()
x.Tags.Component = make(map[string]*HttpMetric)
return x
}
func (x *RealVarz) MarshalJSON() ([]byte, error) {
x.Lock()
defer x.Unlock()
x.varz.Urls = x.r.NumUris()
x.varz.Droplets = x.r.NumEndpoints()
x.varz.RequestsPerSec = x.varz.All.Rate.Rate1()
millis_per_nano := int64(1000000)
x.varz.MillisSinceLastRegistryUpdate = time.Since(x.r.TimeOfLastUpdate()).Nanoseconds() / millis_per_nano
x.updateTop()
d := make(map[string]interface{})
transform(x.varz.All, d)
transform(x.varz, d)
delete(d, "all")
return json.Marshal(d)
}
func (x *RealVarz) updateTop() {
t := time.Now().Add(-1 * time.Minute)
y := x.topApps.TopSince(t, 10)
x.varz.TopApps = make([]topAppsEntry, 0)
for _, z := range y {
x.varz.TopApps = append(x.varz.TopApps, topAppsEntry{
ApplicationId: z.ApplicationId,
RequestsPerSecond: z.Requests / int64(stats.TopAppsEntryLifetime.Seconds()),
RequestsPerMinute: z.Requests,
})
}
}
func (x *RealVarz) ActiveApps() *stats.ActiveApps {
return x.activeApps
}
func (x *RealVarz) CaptureBadRequest(*http.Request) {
x.Lock()
x.BadRequests++
x.Unlock()
}
func (x *RealVarz) CaptureBadGateway(*http.Request) {
x.Lock()
x.BadGateways++
x.Unlock()
}
func (x *RealVarz) CaptureAppStats(b *route.Endpoint, t time.Time) {
if b.ApplicationId != "" {
x.activeApps.Mark(b.ApplicationId, t)
x.topApps.Mark(b.ApplicationId, t)
}
}
func (x *RealVarz) CaptureRoutingRequest(b *route.Endpoint, req *http.Request) {
x.Lock()
var t string
var ok bool
t, ok = b.Tags["component"]
if ok {
x.varz.Tags.Component.CaptureRequest(t)
}
x.varz.All.CaptureRequest()
x.Unlock()
}
func (x *RealVarz) CaptureRoutingResponse(endpoint *route.Endpoint, response *http.Response, startedAt time.Time, duration time.Duration) {
x.Lock()
var tags string
var ok bool
tags, ok = endpoint.Tags["component"]
if ok {
x.varz.Tags.Component.CaptureResponse(tags, response, duration)
}
x.CaptureAppStats(endpoint, startedAt)
x.varz.All.CaptureResponse(response, duration)
x.Unlock()
}
func transform(x interface{}, y map[string]interface{}) error {
var b []byte
var err error
b, err = json.Marshal(x)
if err != nil {
return err
}
err = json.Unmarshal(b, &y)
if err != nil {
return err
}
return nil
}