-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
168 lines (137 loc) · 4.96 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
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"time"
"github.com/armon/go-metrics"
"github.com/influxdata/influxdb/influxql"
"github.com/ve-interactive/influx-protector/logger"
"github.com/ve-interactive/influx-protector/rules"
"github.com/ve-interactive/influx-protector/version"
)
type errorResponses struct {
Results []*errorResponse `json:"results"`
}
type errorResponse struct {
Error string `json:"error"`
}
const (
defaultPort = ":8087"
defaultPortUsage = "default server port, ':8087', ':8086'..."
defaultTarget = "http://127.0.0.1:8086"
defaultTargetUsage = "default redirect url, 'http://127.0.0.1:8086'"
defaultVerbose = false
defaultVerboseUsage = "--verbose"
defaultVersion = false
defaultVersionUsage = "--version"
defaultBuckets = 1000
defaultBucketsUsage = "default buckets 1000"
defaultMaxDuration = time.Hour * 24 * 90
defaultMaxDurationUsage = "default max duration 2160h (90 days)"
defaultSlowQuery = 1000
defaultSlowQueryUsage = "default slowquery time in milliseconds 1000"
defaultStatsdAddress = "localhost:8125"
defaultStatsdAddressUsage = "<statsd_address>:<port>"
)
func main() {
// flags
port := flag.String("port", defaultPort, defaultPortUsage)
target := flag.String("target", defaultTarget, defaultTargetUsage)
verbose := flag.Bool("verbose", defaultVerbose, defaultVerboseUsage)
vsn := flag.Bool("version", defaultVersion, defaultVersionUsage)
maxbuckets := flag.Int("maxbuckets", defaultBuckets, defaultBucketsUsage)
maxduration := flag.Duration("maxduration", defaultMaxDuration, defaultMaxDurationUsage)
slowquery := flag.Int64("slowquery", defaultSlowQuery, defaultSlowQueryUsage)
statsdAddress := flag.String("statsdaddress", defaultStatsdAddress, defaultStatsdAddressUsage)
flag.Parse()
if *vsn {
fmt.Printf("influx-protector version %s", version.Version)
fmt.Println()
return
}
logger := logger.NewLogger(*verbose, *slowquery)
sink, _ := metrics.NewStatsdSink(*statsdAddress)
metrics.NewGlobal(metrics.DefaultConfig("influx-protector"), sink)
logger.Info("Influx Protector Version %s", version.Version)
logger.Info("server will run on: %s", *port)
logger.Info("redirecting to: %s", *target)
purl, _ := url.Parse(*target)
proxy := httputil.NewSingleHostReverseProxy(purl)
options := &rules.Options{
MaxBuckets: *maxbuckets,
SlowQuery: *slowquery,
MaxDuration: *maxduration,
}
// server
http.HandleFunc("/query", queryFunc(logger, options, proxy))
http.HandleFunc("/ping", pingFunc(logger, options, proxy))
http.HandleFunc("/write", writeFunc(logger, options, proxy))
http.ListenAndServe(*port, nil)
}
func generateErrorResp(err error, w http.ResponseWriter) {
body, jsErr := json.Marshal(&errorResponses{
Results: []*errorResponse{&errorResponse{
Error: err.Error(),
}},
})
if jsErr != nil {
http.Error(w, jsErr.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(body)
}
func setHeaders(w http.ResponseWriter) {
w.Header().Set("X-Influx-Protector-Version", version.Version)
}
func queryFunc(logger *logger.Logger, options *rules.Options, proxy *httputil.ReverseProxy) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
setHeaders(w)
inputQuery := strings.TrimSpace(r.URL.Query().Get("q"))
defer logger.Query(time.Now(), inputQuery, options)
defer metrics.MeasureSince([]string{"queries", "timing"}, time.Now())
query, err := influxql.NewParser(strings.NewReader(inputQuery)).ParseStatement()
if err != nil {
logger.Error(inputQuery, err)
metrics.IncrCounter([]string{"queries", "malformed"}, 1)
generateErrorResp(err, w)
return
}
if err := rules.RunRules(query, options); err != nil {
logger.Error(inputQuery, err)
metrics.IncrCounter([]string{"queries", "blocked"}, 1)
generateErrorResp(err, w)
return
}
metrics.IncrCounter([]string{"queries", "accepted"}, 1)
proxy.ServeHTTP(w, r)
}
}
func pingFunc(logger *logger.Logger, options *rules.Options, proxy *httputil.ReverseProxy) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
setHeaders(w)
proxy.ServeHTTP(w, r)
}
}
func writeFunc(logger *logger.Logger, options *rules.Options, proxy *httputil.ReverseProxy) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
setHeaders(w)
defer metrics.MeasureSince([]string{"writes", "timing"}, time.Now())
buf, _ := ioutil.ReadAll(r.Body)
rdr1 := ioutil.NopCloser(bytes.NewBuffer(buf))
body := string(buf[:len(buf)])
lines := strings.Split(body, "\n")
r.Body = rdr1
datapoints := float32(len(lines))
metrics.IncrCounter([]string{"writes", "points"}, datapoints)
metrics.SetGauge([]string{"writes", "batchsize"}, datapoints)
proxy.ServeHTTP(w, r)
}
}