-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathquery_handler.go
558 lines (472 loc) · 14.6 KB
/
query_handler.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
package http
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"time"
"github.com/NYTimes/gziphandler"
"github.com/influxdata/flux"
"github.com/influxdata/flux/ast"
"github.com/influxdata/flux/complete"
"github.com/influxdata/flux/csv"
"github.com/influxdata/flux/iocounter"
"github.com/influxdata/flux/parser"
"github.com/influxdata/influxdb"
pcontext "github.com/influxdata/influxdb/context"
"github.com/influxdata/influxdb/http/metric"
"github.com/influxdata/influxdb/kit/check"
"github.com/influxdata/influxdb/kit/tracing"
"github.com/influxdata/influxdb/query"
"github.com/julienschmidt/httprouter"
"github.com/pkg/errors"
prom "github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"
)
const (
fluxPath = "/api/v2/query"
)
// FluxBackend is all services and associated parameters required to construct
// the FluxHandler.
type FluxBackend struct {
influxdb.HTTPErrorHandler
Logger *zap.Logger
QueryEventRecorder metric.EventRecorder
OrganizationService influxdb.OrganizationService
ProxyQueryService query.ProxyQueryService
}
// NewFluxBackend returns a new instance of FluxBackend.
func NewFluxBackend(b *APIBackend) *FluxBackend {
return &FluxBackend{
HTTPErrorHandler: b.HTTPErrorHandler,
Logger: b.Logger.With(zap.String("handler", "query")),
QueryEventRecorder: b.QueryEventRecorder,
ProxyQueryService: b.FluxService,
OrganizationService: b.OrganizationService,
}
}
// HTTPDialect is an encoding dialect that can write metadata to HTTP headers
type HTTPDialect interface {
SetHeaders(w http.ResponseWriter)
}
// FluxHandler implements handling flux queries.
type FluxHandler struct {
*httprouter.Router
influxdb.HTTPErrorHandler
Logger *zap.Logger
Now func() time.Time
OrganizationService influxdb.OrganizationService
ProxyQueryService query.ProxyQueryService
EventRecorder metric.EventRecorder
}
// NewFluxHandler returns a new handler at /api/v2/query for flux queries.
func NewFluxHandler(b *FluxBackend) *FluxHandler {
h := &FluxHandler{
Router: NewRouter(b.HTTPErrorHandler),
Now: time.Now,
HTTPErrorHandler: b.HTTPErrorHandler,
Logger: b.Logger,
ProxyQueryService: b.ProxyQueryService,
OrganizationService: b.OrganizationService,
EventRecorder: b.QueryEventRecorder,
}
// query reponses can optionally be gzip encoded
qh := gziphandler.GzipHandler(http.HandlerFunc(h.handleQuery))
h.Handler("POST", fluxPath, qh)
h.HandlerFunc("POST", "/api/v2/query/ast", h.postFluxAST)
h.HandlerFunc("POST", "/api/v2/query/analyze", h.postQueryAnalyze)
h.HandlerFunc("GET", "/api/v2/query/suggestions", h.getFluxSuggestions)
h.HandlerFunc("GET", "/api/v2/query/suggestions/:name", h.getFluxSuggestion)
return h
}
func (h *FluxHandler) handleQuery(w http.ResponseWriter, r *http.Request) {
const op = "http/handlePostQuery"
span, r := tracing.ExtractFromHTTPRequest(r, "FluxHandler")
defer span.Finish()
ctx := r.Context()
// TODO(desa): I really don't like how we're recording the usage metrics here
// Ideally this will be moved when we solve https://github.com/influxdata/influxdb/issues/13403
var orgID influxdb.ID
var requestBytes int
sw := newStatusResponseWriter(w)
w = sw
defer func() {
h.EventRecorder.Record(ctx, metric.Event{
OrgID: orgID,
Endpoint: r.URL.Path, // This should be sufficient for the time being as it should only be single endpoint.
RequestBytes: requestBytes,
ResponseBytes: sw.responseBytes,
Status: sw.code(),
})
}()
a, err := pcontext.GetAuthorizer(ctx)
if err != nil {
err := &influxdb.Error{
Code: influxdb.EUnauthorized,
Msg: "authorization is invalid or missing in the query request",
Op: op,
Err: err,
}
h.HandleHTTPError(ctx, err, w)
return
}
req, n, err := decodeProxyQueryRequest(ctx, r, a, h.OrganizationService)
if err != nil && err != influxdb.ErrAuthorizerNotSupported {
err := &influxdb.Error{
Code: influxdb.EInvalid,
Msg: "failed to decode request body",
Op: op,
Err: err,
}
h.HandleHTTPError(ctx, err, w)
return
}
orgID = req.Request.OrganizationID
requestBytes = n
// Transform the context into one with the request's authorization.
ctx = pcontext.SetAuthorizer(ctx, req.Request.Authorization)
hd, ok := req.Dialect.(HTTPDialect)
if !ok {
err := &influxdb.Error{
Code: influxdb.EInvalid,
Msg: fmt.Sprintf("unsupported dialect over HTTP: %T", req.Dialect),
Op: op,
}
h.HandleHTTPError(ctx, err, w)
return
}
hd.SetHeaders(w)
cw := iocounter.Writer{Writer: w}
if _, err := h.ProxyQueryService.Query(ctx, &cw, req); err != nil {
if cw.Count() == 0 {
// Only record the error headers IFF nothing has been written to w.
h.HandleHTTPError(ctx, err, w)
return
}
h.Logger.Info("Error writing response to client",
zap.String("handler", "flux"),
zap.Error(err),
)
}
}
type langRequest struct {
Query string `json:"query"`
}
type postFluxASTResponse struct {
AST *ast.Package `json:"ast"`
}
// postFluxAST returns a flux AST for provided flux string
func (h *FluxHandler) postFluxAST(w http.ResponseWriter, r *http.Request) {
span, r := tracing.ExtractFromHTTPRequest(r, "FluxHandler")
defer span.Finish()
var request langRequest
ctx := r.Context()
err := json.NewDecoder(r.Body).Decode(&request)
if err != nil {
h.HandleHTTPError(ctx, &influxdb.Error{
Code: influxdb.EInvalid,
Msg: "invalid json",
Err: err,
}, w)
return
}
pkg := parser.ParseSource(request.Query)
if ast.Check(pkg) > 0 {
err := ast.GetError(pkg)
h.HandleHTTPError(ctx, &influxdb.Error{
Code: influxdb.EInvalid,
Msg: "invalid AST",
Err: err,
}, w)
return
}
res := postFluxASTResponse{
AST: pkg,
}
if err := encodeResponse(ctx, w, http.StatusOK, res); err != nil {
logEncodingError(h.Logger, r, err)
return
}
}
// postQueryAnalyze parses a query and returns any query errors.
func (h *FluxHandler) postQueryAnalyze(w http.ResponseWriter, r *http.Request) {
span, r := tracing.ExtractFromHTTPRequest(r, "FluxHandler")
defer span.Finish()
ctx := r.Context()
var req QueryRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
h.HandleHTTPError(ctx, &influxdb.Error{
Code: influxdb.EInvalid,
Msg: "invalid json",
Err: err,
}, w)
return
}
a, err := req.Analyze()
if err != nil {
h.HandleHTTPError(ctx, err, w)
return
}
if err := encodeResponse(ctx, w, http.StatusOK, a); err != nil {
logEncodingError(h.Logger, r, err)
return
}
}
// fluxParams contain flux funciton parameters as defined by the semantic graph
type fluxParams map[string]string
// suggestionResponse provides the parameters available for a given Flux function
type suggestionResponse struct {
Name string `json:"name"`
Params fluxParams `json:"params"`
}
// suggestionsResponse provides a list of available Flux functions
type suggestionsResponse struct {
Functions []suggestionResponse `json:"funcs"`
}
// getFluxSuggestions returns a list of available Flux functions for the Flux Builder
func (h *FluxHandler) getFluxSuggestions(w http.ResponseWriter, r *http.Request) {
span, r := tracing.ExtractFromHTTPRequest(r, "FluxHandler")
defer span.Finish()
ctx := r.Context()
completer := complete.DefaultCompleter()
names := completer.FunctionNames()
var functions []suggestionResponse
for _, name := range names {
suggestion, err := completer.FunctionSuggestion(name)
if err != nil {
h.HandleHTTPError(ctx, err, w)
return
}
filteredParams := make(fluxParams)
for key, value := range suggestion.Params {
if key == "table" {
continue
}
filteredParams[key] = value
}
functions = append(functions, suggestionResponse{
Name: name,
Params: filteredParams,
})
}
res := suggestionsResponse{Functions: functions}
if err := encodeResponse(ctx, w, http.StatusOK, res); err != nil {
logEncodingError(h.Logger, r, err)
return
}
}
// getFluxSuggestion returns the function parameters for the requested function
func (h *FluxHandler) getFluxSuggestion(w http.ResponseWriter, r *http.Request) {
span, r := tracing.ExtractFromHTTPRequest(r, "FluxHandler")
defer span.Finish()
ctx := r.Context()
name := httprouter.ParamsFromContext(ctx).ByName("name")
completer := complete.DefaultCompleter()
suggestion, err := completer.FunctionSuggestion(name)
if err != nil {
h.HandleHTTPError(ctx, err, w)
return
}
res := suggestionResponse{Name: name, Params: suggestion.Params}
if err := encodeResponse(ctx, w, http.StatusOK, res); err != nil {
logEncodingError(h.Logger, r, err)
return
}
}
// PrometheusCollectors satisifies the prom.PrometheusCollector interface.
func (h *FluxHandler) PrometheusCollectors() []prom.Collector {
// TODO: gather and return relevant metrics.
return nil
}
var _ query.ProxyQueryService = (*FluxService)(nil)
// FluxService connects to Influx via HTTP using tokens to run queries.
type FluxService struct {
Addr string
Token string
InsecureSkipVerify bool
}
// Query runs a flux query against a influx server and sends the results to the io.Writer.
// Will use the token from the context over the token within the service struct.
func (s *FluxService) Query(ctx context.Context, w io.Writer, r *query.ProxyRequest) (flux.Statistics, error) {
span, ctx := tracing.StartSpanFromContext(ctx)
defer span.Finish()
u, err := NewURL(s.Addr, fluxPath)
if err != nil {
return flux.Statistics{}, tracing.LogError(span, err)
}
params := url.Values{}
params.Set(OrgID, r.Request.OrganizationID.String())
u.RawQuery = params.Encode()
qreq, err := QueryRequestFromProxyRequest(r)
if err != nil {
return flux.Statistics{}, tracing.LogError(span, err)
}
var body bytes.Buffer
if err := json.NewEncoder(&body).Encode(qreq); err != nil {
return flux.Statistics{}, tracing.LogError(span, err)
}
hreq, err := http.NewRequest("POST", u.String(), &body)
if err != nil {
return flux.Statistics{}, tracing.LogError(span, err)
}
SetToken(s.Token, hreq)
hreq.Header.Set("Content-Type", "application/json")
hreq.Header.Set("Accept", "text/csv")
hreq = hreq.WithContext(ctx)
hc := NewClient(u.Scheme, s.InsecureSkipVerify)
resp, err := hc.Do(hreq)
if err != nil {
return flux.Statistics{}, tracing.LogError(span, err)
}
defer resp.Body.Close()
if err := CheckError(resp); err != nil {
return flux.Statistics{}, tracing.LogError(span, err)
}
if _, err := io.Copy(w, resp.Body); err != nil {
return flux.Statistics{}, tracing.LogError(span, err)
}
return flux.Statistics{}, nil
}
func (s FluxService) Check(ctx context.Context) check.Response {
return QueryHealthCheck(s.Addr, s.InsecureSkipVerify)
}
var _ query.QueryService = (*FluxQueryService)(nil)
// FluxQueryService implements query.QueryService by making HTTP requests to the /api/v2/query API endpoint.
type FluxQueryService struct {
Addr string
Token string
InsecureSkipVerify bool
}
// Query runs a flux query against a influx server and decodes the result
func (s *FluxQueryService) Query(ctx context.Context, r *query.Request) (flux.ResultIterator, error) {
span, ctx := tracing.StartSpanFromContext(ctx)
defer span.Finish()
u, err := NewURL(s.Addr, fluxPath)
if err != nil {
return nil, tracing.LogError(span, err)
}
params := url.Values{}
params.Set(OrgID, r.OrganizationID.String())
u.RawQuery = params.Encode()
preq := &query.ProxyRequest{
Request: *r,
Dialect: csv.DefaultDialect(),
}
qreq, err := QueryRequestFromProxyRequest(preq)
if err != nil {
return nil, tracing.LogError(span, err)
}
var body bytes.Buffer
if err := json.NewEncoder(&body).Encode(qreq); err != nil {
return nil, tracing.LogError(span, err)
}
hreq, err := http.NewRequest("POST", u.String(), &body)
if err != nil {
return nil, tracing.LogError(span, err)
}
SetToken(s.Token, hreq)
hreq.Header.Set("Content-Type", "application/json")
hreq.Header.Set("Accept", "text/csv")
hreq = hreq.WithContext(ctx)
hc := NewClient(u.Scheme, s.InsecureSkipVerify)
resp, err := hc.Do(hreq)
if err != nil {
return nil, tracing.LogError(span, err)
}
// Can't defer resp.Body.Close here because the CSV decoder depends on reading from resp.Body after this function returns.
if err := CheckError(resp); err != nil {
return nil, tracing.LogError(span, err)
}
decoder := csv.NewMultiResultDecoder(csv.ResultDecoderConfig{})
itr, err := decoder.Decode(resp.Body)
if err != nil {
return nil, tracing.LogError(span, err)
}
return itr, nil
}
func (s FluxQueryService) Check(ctx context.Context) check.Response {
return QueryHealthCheck(s.Addr, s.InsecureSkipVerify)
}
// SimpleQuery runs a flux query with common parameters and returns CSV results.
func SimpleQuery(addr, flux, org, token string) ([]byte, error) {
u, err := NewURL(addr, fluxPath)
if err != nil {
return nil, err
}
params := url.Values{}
params.Set(Org, org)
u.RawQuery = params.Encode()
header := true
qr := &QueryRequest{
Type: "flux",
Query: flux,
Dialect: QueryDialect{
Header: &header,
Delimiter: ",",
CommentPrefix: "#",
DateTimeFormat: "RFC3339",
},
}
var body bytes.Buffer
if err := json.NewEncoder(&body).Encode(qr); err != nil {
return nil, err
}
req, err := http.NewRequest("POST", u.String(), &body)
if err != nil {
return nil, err
}
SetToken(token, req)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "text/csv")
insecureSkipVerify := false
hc := NewClient(u.Scheme, insecureSkipVerify)
res, err := hc.Do(req)
if err != nil {
return nil, err
}
if err := CheckError(res); err != nil {
return nil, err
}
defer res.Body.Close()
return ioutil.ReadAll(res.Body)
}
func QueryHealthCheck(url string, insecureSkipVerify bool) check.Response {
u, err := NewURL(url, "/health")
if err != nil {
return check.Response{
Name: "query health",
Status: check.StatusFail,
Message: errors.Wrap(err, "could not form URL").Error(),
}
}
hc := NewClient(u.Scheme, insecureSkipVerify)
resp, err := hc.Get(u.String())
if err != nil {
return check.Response{
Name: "query health",
Status: check.StatusFail,
Message: errors.Wrap(err, "error getting response").Error(),
}
}
defer resp.Body.Close()
if resp.StatusCode/100 != 2 {
return check.Response{
Name: "query health",
Status: check.StatusFail,
Message: fmt.Sprintf("http error %v", resp.StatusCode),
}
}
var healthResponse check.Response
if err = json.NewDecoder(resp.Body).Decode(&healthResponse); err != nil {
return check.Response{
Name: "query health",
Status: check.StatusFail,
Message: errors.Wrap(err, "error decoding JSON response").Error(),
}
}
return healthResponse
}