-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
156 lines (146 loc) · 4.55 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
package main
import (
"fmt"
"io"
"net/http"
"net/url"
"regexp"
"github.com/alecthomas/kingpin/v2"
log "github.com/sirupsen/logrus"
)
var (
verbose = kingpin.Flag("verbose", "Verbose mode.").Short('v').Bool()
upstream = kingpin.Flag("upstream.addr", "upstream to connect to").Required().String()
upstreamPrefixPath = kingpin.Flag("upstream.prefix-path", "upstream prefix path to prepend").String()
listenAddr = kingpin.Flag("proxy.listen-addr", "address the proxy will listen on").Required().String()
urlPattern = regexp.MustCompile(`^/([^/]+)(/api/v.+)$`)
supportedPathPattern = regexp.MustCompile(`^/api/v1/(query|query_range|query_exemplars|series|label/[a-zA-Z0-9_]+/values)$`)
)
func handleQuery(filter string, rw http.ResponseWriter, r *http.Request) {
log.WithFields(log.Fields{"method": r.Method, "path": r.URL.String(), "filter": filter}).Debug("handling request")
params := &url.Values{}
err := r.ParseForm()
if err != nil {
log.WithFields(log.Fields{"err": err}).Warn("failed to parse query string")
rw.WriteHeader(http.StatusBadRequest)
return
}
for k, vv := range r.Form {
log.WithFields(log.Fields{"k": k}).Debug("handling form key")
switch k {
case "query":
if len(vv) != 1 {
log.WithFields(log.Fields{"method": r.Method, "path": r.URL.String()}).Warn("wrong number of query params")
rw.WriteHeader(http.StatusBadRequest)
return
}
val, err := addQueryFilter(filter, vv[0])
if err != nil {
log.WithFields(log.Fields{"val": vv[0], "err": err}).Warn("failed to add filter")
rw.WriteHeader(http.StatusBadRequest)
return
}
params.Set(k, val)
case "match[]":
log.WithFields(log.Fields{"val": vv}).Debug("rewriting match")
for _, v := range vv {
val, err := addQueryFilter(filter, v)
if err != nil {
log.WithFields(log.Fields{"val": v, "err": err}).Warn("failed to add filter")
rw.WriteHeader(http.StatusBadRequest)
return
}
params.Add(k, val)
}
case "start":
fallthrough
case "end":
fallthrough
case "step":
fallthrough
case "time":
for _, v := range vv {
params.Add(k, v)
}
default:
log.WithFields(log.Fields{"key": k, "values": vv}).Warn("unknown param")
continue
}
}
url := &url.URL{
Scheme: "http",
Host: *upstream,
Path: fmt.Sprintf("%s%s", *upstreamPrefixPath, r.URL.Path), //FIXME
RawQuery: params.Encode(),
}
log.WithFields(log.Fields{"url": url.String()}).Debug("starting request to upstream")
resp, err := http.Get(url.String())
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
log.WithFields(log.Fields{"err": err}).Warn("upstream request failed")
return
}
h := rw.Header()
for k, vv := range resp.Header {
if k == "Content-Length" {
continue
}
for _, v := range vv {
log.WithFields(log.Fields{"header": k, "value": v}).Debug("copying response header")
h.Add(k, v)
}
}
rw.WriteHeader(resp.StatusCode)
_, err = io.Copy(rw, resp.Body)
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
log.WithFields(log.Fields{"err": err}).Warn("forwarding upstream response failed")
return
}
}
func handleUnsupported(rw http.ResponseWriter, r *http.Request) {
rw.WriteHeader(http.StatusBadRequest)
rw.Write([]byte("Unsupported\n"))
log.WithFields(log.Fields{"method": r.Method, "path": r.URL.String()}).Warn("unsupported request")
}
type router struct {
}
func (r router) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
if req.Method != "GET" && req.Method != "POST" {
rw.WriteHeader(http.StatusBadRequest)
rw.Write([]byte("Unsupported method\n"))
log.WithFields(log.Fields{"method": req.Method, "path": req.URL.String()}).Warn("unsupported method")
return
}
path := req.URL.Path
m := urlPattern.FindStringSubmatch(path)
if len(m) != 3 {
handleUnsupported(rw, req)
return
}
filter := "{" + m[1] + "}"
apiPath := m[2]
if !supportedPath(apiPath) {
rw.WriteHeader(http.StatusBadRequest)
rw.Write([]byte("prometheus-filter-proxy: Unsupported path\n"))
log.WithFields(log.Fields{"method": req.Method, "path": req.URL.String()}).Warn("unsupported path")
return
}
req.URL.Path = apiPath
handleQuery(filter, rw, req)
}
func supportedPath(path string) bool {
return supportedPathPattern.MatchString(path)
}
func main() {
kingpin.Parse()
if *verbose {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}
log.WithFields(log.Fields{"upstream.url": *upstream, "proxy.listen-addr": *listenAddr}).Info("Starting")
router := router{}
http.Handle("/", router)
log.Fatal(http.ListenAndServe(*listenAddr, nil))
}