-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
292 lines (262 loc) · 7.16 KB
/
main.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
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"net/http"
"net/http/httputil"
"net/url"
"strconv"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
)
var (
requestCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "prometheus",
Subsystem: "shield",
Name: "requests_total",
Help: "Total number of requests received by prometheus shield",
}, []string{"method", "path"},
)
hitCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "prometheus",
Subsystem: "shield",
Name: "requests_cache_hit_total",
Help: "Total number of requests served from cache",
}, []string{"method", "path"},
)
requestDuration = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "prometheus",
Subsystem: "shield",
Name: "request_duration_seconds",
Help: "Duration of request made handled by prometheus shield",
Buckets: prometheus.ExponentialBuckets(0.01, 5, 3),
},
[]string{"method", "path"},
)
errorCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "prometheus",
Subsystem: "shield",
Name: "errors_total",
Help: "Total number of error encountered in prometheus shield",
},
[]string{"area"},
)
)
// Cache Item //
type cacheItem struct {
content []byte
expires time.Time
}
func newCacheItem(content []byte, ttl time.Duration) *cacheItem {
return &cacheItem{
content: content,
expires: time.Now().Add(ttl),
}
}
func (i *cacheItem) expired() bool {
return i.expires.Before(time.Now())
}
// Cache //
type cache struct {
sync.RWMutex
cache map[string]*cacheItem
}
func (c *cache) cacheKey(req *http.Request) string {
return req.Method + req.URL.String()
}
func (c *cache) get(req *http.Request) *cacheItem {
key := c.cacheKey(req)
c.RLock()
item, ok := c.cache[key]
c.RUnlock()
if !ok {
return nil
}
if item.expired() {
return nil
}
return item
}
func (c *cache) store(req *http.Request, resp *http.Response, ttl time.Duration) error {
respB, err := httputil.DumpResponse(resp, true)
if err != nil {
return err
}
key := c.cacheKey(req)
c.Lock()
c.cache[key] = newCacheItem(respB, ttl)
c.Unlock()
return nil
}
type proxy struct {
http.RoundTripper
*httputil.ReverseProxy
cache cache
ttl time.Duration
}
func (p *proxy) RoundTrip(req *http.Request) (*http.Response, error) {
ci := p.cache.get(req)
if ci != nil {
log.Debug(" - cache hit")
hitCounter.WithLabelValues(req.Method, req.URL.Path).Inc() // Path is bounded by switch in serveHTTP
return http.ReadResponse(bufio.NewReader(bytes.NewBuffer(ci.content)), req)
}
log.Debug(" - cache miss")
resp, err := p.RoundTripper.RoundTrip(req)
if err != nil {
errorCounter.WithLabelValues("RoundTrip").Inc()
return nil, err
}
if err := p.cache.store(req, resp, p.ttl); err != nil {
errorCounter.WithLabelValues("CacheStore").Inc()
log.Warn(err)
}
return resp, nil
}
func (p *proxy) ServeHTTP(w http.ResponseWriter, reqIn *http.Request) {
log.WithFields(log.Fields{"method": reqIn.Method}).Info(reqIn.URL.String())
boundedPath := reqIn.URL.Path
start := time.Now()
now := time.Now()
nowHour := now.Truncate(1 * time.Hour)
oneHourAgo := now.Add(-1 * time.Hour)
req := &http.Request{}
defer func() {
requestCounter.WithLabelValues(reqIn.Method, boundedPath).Inc()
requestDuration.WithLabelValues(reqIn.Method, boundedPath).Observe(float64(time.Since(start)))
log.WithFields(log.Fields{"method": req.Method}).Debug(req.URL.String())
}()
switch reqIn.URL.Path {
case "/api/v1/series":
*req = *reqIn
req.URL.RawQuery = url.Values{
"start": []string{strconv.FormatInt(nowHour.Add(-1*time.Hour).Unix(), 10)},
"end": []string{strconv.FormatInt(nowHour.Unix(), 10)},
"match[]": reqIn.URL.Query()["match[]"],
}.Encode()
p.ReverseProxy.ServeHTTP(w, req)
case "/api/v1/label/__name__/values":
*req = *reqIn
req.URL.RawQuery = ""
p.ReverseProxy.ServeHTTP(w, req)
case "/api/v1/query":
*req = *reqIn
req.URL.RawQuery = url.Values{
"query": reqIn.URL.Query()["query"],
}.Encode()
p.ReverseProxy.ServeHTTP(w, req)
case "/api/v1/query_range":
*req = *reqIn // Copy request
params := req.URL.Query()
start, err := timeStr(params.Get("start"))
if err != nil {
errorCounter.WithLabelValues("ParseRequest").Inc()
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if start.Before(oneHourAgo) {
log.Debugf(" start(%s) is earlier than oneHourAgo(%s)", start, oneHourAgo)
start = oneHourAgo
}
if start.After(now) {
log.Debugf(" start(%s) is after now(%s)", start, now)
start = now
}
end, err := timeStr(params.Get("end"))
if err != nil {
errorCounter.WithLabelValues("ParseRequest").Inc()
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if end.Before(oneHourAgo) {
log.Debugf(" end(%s) is earlier than oneHourAgo(%s)", end, oneHourAgo)
end = oneHourAgo
}
if end.After(now) {
log.Debugf(" end(%s) is after now(%s)", end, now)
end = now
}
step, err := strconv.Atoi(params.Get("step"))
if err != nil {
errorCounter.WithLabelValues("ParseRequest").Inc()
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if step < 15 {
step = 15
}
req.URL = &url.URL{
Scheme: req.URL.Scheme,
Path: req.URL.Path,
RawQuery: url.Values{
"query": params["query"],
"start": []string{strconv.FormatInt(start.Truncate(p.ttl).Unix(), 10)},
"end": []string{strconv.FormatInt(end.Truncate(p.ttl).Unix(), 10)},
"step": []string{strconv.Itoa(step)},
}.Encode(),
}
p.ReverseProxy.ServeHTTP(w, req)
default:
boundedPath = "" // Make sure path is bounded
errorCounter.WithLabelValues("NotAllowed").Inc()
http.Error(w, "Not allowed", http.StatusForbidden)
}
}
func newProxy(promURL *url.URL, ttl time.Duration) *proxy {
p := &proxy{
ReverseProxy: httputil.NewSingleHostReverseProxy(promURL),
RoundTripper: http.DefaultTransport,
cache: cache{cache: map[string]*cacheItem{}},
ttl: ttl,
}
p.ReverseProxy.Transport = p // hrmm...
return p
}
var (
logLevelMap = map[string]log.Level{
"debug": log.DebugLevel,
"info": log.InfoLevel,
"warn": log.WarnLevel,
"error": log.ErrorLevel,
"fatal": log.FatalLevel,
"panic": log.PanicLevel,
}
)
func main() {
var (
promAddr = flag.String("u", "http://localhost:9090", "URL of prometheus server")
listenAddr = flag.String("l", "0.0.0.0:9191", "Address to listen on")
ttln = flag.Int("t", 60, "TTL")
logLevel = flag.String("ll", "info", "Log level")
)
flag.Parse()
ll, ok := logLevelMap[*logLevel]
if !ok {
log.Fatal("Invalid log level (-ll) given:", *logLevel)
}
log.SetLevel(ll)
promURL, err := url.Parse(*promAddr)
if err != nil {
log.Fatal(err)
}
ttl := time.Duration(*ttln) * time.Second
http.Handle("/metrics", promhttp.Handler())
http.Handle("/", newProxy(promURL, ttl))
log.Fatal(http.ListenAndServe(*listenAddr, nil))
}
func timeStr(ts string) (t time.Time, err error) {
start, err := strconv.Atoi(ts)
if err != nil {
return t, fmt.Errorf("Couldn't parse %s: %s", ts, err)
}
return time.Unix(int64(start), 0), nil
}