-
Notifications
You must be signed in to change notification settings - Fork 139
/
helper.go
249 lines (225 loc) · 5.66 KB
/
helper.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
package http
import (
"fmt"
"net/http"
"strings"
"sync/atomic"
"time"
"github.com/go-graphite/carbonapi/carbonapipb"
"github.com/go-graphite/carbonapi/cmd/carbonapi/config"
"github.com/go-graphite/carbonapi/pkg/parser"
"github.com/lomik/zapwriter"
"go.uber.org/zap"
)
type responseFormat int
// for testing
var timeNow = time.Now
const (
jsonFormat responseFormat = iota
treejsonFormat
pngFormat
csvFormat
rawFormat
svgFormat
protoV2Format
protoV3Format
pickleFormat
completerFormat
)
func (r responseFormat) String() string {
switch r {
case jsonFormat:
return "json"
case pickleFormat:
return "pickle"
case protoV2Format:
return "protobuf3"
case protoV3Format:
return "carbonapi_v3_pb"
case treejsonFormat:
return "treejson"
case pngFormat:
return "png"
case csvFormat:
return "csv"
case rawFormat:
return "raw"
case svgFormat:
return "svg"
case completerFormat:
return "completer"
default:
return "unknown"
}
}
func (r responseFormat) ValidFindFormat() bool {
switch r {
case jsonFormat:
return true
case pickleFormat:
return true
case protoV2Format:
return true
case protoV3Format:
return true
case completerFormat:
return true
case csvFormat:
return true
case rawFormat:
return true
case treejsonFormat:
return true
default:
return false
}
}
func (r responseFormat) ValidRenderFormat() bool {
switch r {
case jsonFormat:
return true
case pickleFormat:
return true
case protoV2Format:
return true
case protoV3Format:
return true
case pngFormat:
return true
case svgFormat:
return true
case csvFormat:
return true
case rawFormat:
return true
default:
return false
}
}
var knownFormats = map[string]responseFormat{
"json": jsonFormat,
"pickle": pickleFormat,
"treejson": treejsonFormat,
"protobuf": protoV2Format,
"protobuf3": protoV2Format,
"carbonapi_v2_pb": protoV2Format,
"carbonapi_v3_pb": protoV3Format,
"png": pngFormat,
"csv": csvFormat,
"raw": rawFormat,
"svg": svgFormat,
"completer": completerFormat,
}
const (
contentTypeJSON = "application/json"
contentTypeProtobuf = "application/x-protobuf"
contentTypeJavaScript = "text/javascript"
contentTypeRaw = "text/plain"
contentTypePickle = "application/pickle"
contentTypePNG = "image/png"
contentTypeCSV = "text/csv"
contentTypeSVG = "image/svg+xml"
)
func getFormat(r *http.Request, defaultFormat responseFormat) (responseFormat, bool, string) {
format := r.FormValue("format")
if format == "" && (parser.TruthyBool(r.FormValue("rawData")) || parser.TruthyBool(r.FormValue("rawdata"))) {
return rawFormat, true, format
}
if format == "" {
return defaultFormat, true, format
}
f, ok := knownFormats[format]
return f, ok, format
}
func writeResponse(w http.ResponseWriter, returnCode int, b []byte, format responseFormat, jsonp string) {
//TODO: Simplify that switch
switch format {
case jsonFormat:
if jsonp != "" {
w.Header().Set("Content-Type", contentTypeJavaScript)
w.WriteHeader(returnCode)
_, _ = w.Write([]byte(jsonp))
_, _ = w.Write([]byte{'('})
_, _ = w.Write(b)
_, _ = w.Write([]byte{')'})
} else {
w.Header().Set("Content-Type", contentTypeJSON)
w.WriteHeader(returnCode)
_, _ = w.Write(b)
}
case protoV2Format, protoV3Format:
w.Header().Set("Content-Type", contentTypeProtobuf)
w.WriteHeader(returnCode)
_, _ = w.Write(b)
case rawFormat:
w.Header().Set("Content-Type", contentTypeRaw)
w.WriteHeader(returnCode)
_, _ = w.Write(b)
case pickleFormat:
w.Header().Set("Content-Type", contentTypePickle)
w.WriteHeader(returnCode)
_, _ = w.Write(b)
case csvFormat:
w.Header().Set("Content-Type", contentTypeCSV)
_, _ = w.Write(b)
case pngFormat:
w.Header().Set("Content-Type", contentTypePNG)
w.WriteHeader(returnCode)
_, _ = w.Write(b)
case svgFormat:
w.Header().Set("Content-Type", contentTypeSVG)
w.WriteHeader(returnCode)
_, _ = w.Write(b)
}
}
func bucketRequestTimes(req *http.Request, t time.Duration) {
logger := zapwriter.Logger("slow")
ms := t.Nanoseconds() / int64(time.Millisecond)
bucket := int(ms / 100)
if bucket < config.Config.Upstreams.Buckets {
atomic.AddInt64(&TimeBuckets[bucket], 1)
} else {
// Too big? Increment overflow bucket
atomic.AddInt64(&TimeBuckets[config.Config.Upstreams.Buckets], 1)
}
if t > config.Config.Upstreams.SlowLogThreshold {
referer := req.Header.Get("Referer")
logger.Warn("Slow Request",
zap.Duration("time", t),
zap.Duration("slowLogThreshold", config.Config.Upstreams.SlowLogThreshold),
zap.String("url", req.URL.String()),
zap.String("referer", referer),
)
}
}
func splitRemoteAddr(addr string) (string, string) {
tmp := strings.Split(addr, ":")
if len(tmp) < 1 {
return "unknown", "unknown"
}
if len(tmp) == 1 {
return tmp[0], ""
}
return tmp[0], tmp[1]
}
func buildParseErrorString(target, e string, err error) string {
msg := fmt.Sprintf("%s\n\n%-20s: %s\n", http.StatusText(http.StatusBadRequest), "Target", target)
if err != nil {
msg += fmt.Sprintf("%-20s: %s\n", "Error", err.Error())
}
if e != "" {
msg += fmt.Sprintf("%-20s: %s\n%-20s: %s\n",
"Parsed so far", target[0:len(target)-len(e)],
"Could not parse", e)
}
return msg
}
func deferredAccessLogging(accessLogger *zap.Logger, accessLogDetails *carbonapipb.AccessLogDetails, t time.Time, logAsError bool) {
accessLogDetails.Runtime = time.Since(t).Seconds()
if logAsError {
accessLogger.Error("request failed", zap.Any("data", *accessLogDetails))
} else {
accessLogDetails.HTTPCode = http.StatusOK
accessLogger.Info("request served", zap.Any("data", *accessLogDetails))
}
}