-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetrics.go
More file actions
182 lines (162 loc) · 3.66 KB
/
metrics.go
File metadata and controls
182 lines (162 loc) · 3.66 KB
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
package main
import (
"context"
"log"
"sync"
"time"
)
type metricsState struct {
mu sync.Mutex
totalRequests uint64
cacheHits uint64
cacheMisses uint64
upstreamFetches uint64
upstreamStatus2xx uint64
upstreamStatus4xx uint64
upstreamStatus5xx uint64
upstreamStatus429 uint64
clientCancels uint64
upstreamTimeouts uint64
throttleWaits uint64
throttleWaitDuration time.Duration
bytesDownloaded uint64
bytesServed uint64
bytesSaved uint64
}
type requestSummary struct {
url string
statusCode int
cache string
source string
upstreamStatus int
bytesServed int64
bytesDownloaded int64
throttleWait time.Duration
errorClass string
totalDuration time.Duration
}
var aggregateMetrics metricsState
func (m *metricsState) recordRequest() {
m.mu.Lock()
defer m.mu.Unlock()
m.totalRequests++
}
func (m *metricsState) recordCacheHit(bytesServed, bytesSaved int64) {
m.mu.Lock()
defer m.mu.Unlock()
m.cacheHits++
m.bytesServed += uint64(bytesServed)
m.bytesSaved += uint64(bytesSaved)
}
func (m *metricsState) recordCacheMiss() {
m.mu.Lock()
defer m.mu.Unlock()
m.cacheMisses++
}
func (m *metricsState) recordUpstreamFetch() {
m.mu.Lock()
defer m.mu.Unlock()
m.upstreamFetches++
}
func (m *metricsState) recordUpstreamStatus(statusCode int) {
m.mu.Lock()
defer m.mu.Unlock()
switch {
case statusCode == 429:
m.upstreamStatus429++
m.upstreamStatus4xx++
case statusCode >= 500:
m.upstreamStatus5xx++
case statusCode >= 400:
m.upstreamStatus4xx++
case statusCode >= 200:
m.upstreamStatus2xx++
}
}
func (m *metricsState) recordClientCancel() {
m.mu.Lock()
defer m.mu.Unlock()
m.clientCancels++
}
func (m *metricsState) recordUpstreamTimeout() {
m.mu.Lock()
defer m.mu.Unlock()
m.upstreamTimeouts++
}
func (m *metricsState) recordThrottleWait(wait time.Duration) {
if wait <= 0 {
return
}
m.mu.Lock()
defer m.mu.Unlock()
m.throttleWaits++
m.throttleWaitDuration += wait
}
func (m *metricsState) addBytesDownloaded(bytes int64) {
if bytes <= 0 {
return
}
m.mu.Lock()
defer m.mu.Unlock()
m.bytesDownloaded += uint64(bytes)
}
func (m *metricsState) addBytesServed(bytes int64) {
if bytes <= 0 {
return
}
m.mu.Lock()
defer m.mu.Unlock()
m.bytesServed += uint64(bytes)
}
func (m *metricsState) logSnapshot() {
m.mu.Lock()
defer m.mu.Unlock()
log.Printf(
"metrics total_requests=%d cache_hits=%d cache_misses=%d upstream_fetches=%d upstream_2xx=%d upstream_4xx=%d upstream_5xx=%d upstream_429=%d client_cancels=%d upstream_timeouts=%d throttle_waits=%d throttle_wait_total=%s bytes_downloaded=%d bytes_served=%d bytes_saved=%d",
m.totalRequests,
m.cacheHits,
m.cacheMisses,
m.upstreamFetches,
m.upstreamStatus2xx,
m.upstreamStatus4xx,
m.upstreamStatus5xx,
m.upstreamStatus429,
m.clientCancels,
m.upstreamTimeouts,
m.throttleWaits,
m.throttleWaitDuration,
m.bytesDownloaded,
m.bytesServed,
m.bytesSaved,
)
}
func startMetricsReporter(ctx context.Context) {
if metricsInterval <= 0 {
return
}
ticker := time.NewTicker(metricsInterval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
aggregateMetrics.logSnapshot()
}
}
}
func logRequestSummary(summary requestSummary) {
log.Printf(
"request url=%q status=%d cache=%s source=%s upstream_status=%d bytes_served=%d bytes_downloaded=%d throttle_wait=%s total=%s error=%q",
summary.url,
summary.statusCode,
summary.cache,
summary.source,
summary.upstreamStatus,
summary.bytesServed,
summary.bytesDownloaded,
summary.throttleWait,
summary.totalDuration,
summary.errorClass,
)
}