forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource_proxy_service.go
122 lines (104 loc) · 3.51 KB
/
source_proxy_service.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
package http
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"github.com/influxdata/flux"
"github.com/influxdata/flux/lang"
platform "github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/kit/check"
"github.com/influxdata/influxdb/v2/kit/tracing"
"github.com/influxdata/influxdb/v2/query"
"github.com/influxdata/influxdb/v2/query/influxql"
)
type SourceProxyQueryService struct {
Addr string
InsecureSkipVerify bool
platform.SourceFields
}
func (s *SourceProxyQueryService) Query(ctx context.Context, w io.Writer, req *query.ProxyRequest) (flux.Statistics, error) {
switch req.Request.Compiler.CompilerType() {
case influxql.CompilerType:
return s.queryInfluxQL(ctx, w, req)
case lang.FluxCompilerType:
return s.queryFlux(ctx, w, req)
}
return flux.Statistics{}, fmt.Errorf("compiler type not supported")
}
func (s *SourceProxyQueryService) queryFlux(ctx context.Context, w io.Writer, req *query.ProxyRequest) (flux.Statistics, error) {
span, ctx := tracing.StartSpanFromContext(ctx)
defer span.Finish()
u, err := NewURL(s.Addr, "/api/v2/query")
if err != nil {
return flux.Statistics{}, tracing.LogError(span, err)
}
var body bytes.Buffer
if err := json.NewEncoder(&body).Encode(req); 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)
}
hreq.Header.Set("Authorization", fmt.Sprintf("Token %s", s.Token))
hreq.Header.Set("Content-Type", "application/json")
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 *SourceProxyQueryService) queryInfluxQL(ctx context.Context, w io.Writer, req *query.ProxyRequest) (flux.Statistics, error) {
span, ctx := tracing.StartSpanFromContext(ctx)
defer span.Finish()
compiler, ok := req.Request.Compiler.(*influxql.Compiler)
if !ok {
return flux.Statistics{}, tracing.LogError(span, fmt.Errorf("compiler is not of type 'influxql'"))
}
u, err := NewURL(s.Addr, "/query")
if err != nil {
return flux.Statistics{}, tracing.LogError(span, err)
}
body := url.Values{}
body.Add("db", compiler.DB)
body.Add("org", compiler.Cluster)
body.Add("q", compiler.Query)
body.Add("rp", compiler.RP)
hreq, err := http.NewRequest("POST", u.String(), strings.NewReader(body.Encode()))
if err != nil {
return flux.Statistics{}, tracing.LogError(span, err)
}
hreq.Header.Set("Content-Type", "application/x-www-form-urlencoded")
hreq.Header.Set("Authorization", fmt.Sprintf("Token %s", s.Token))
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 *SourceProxyQueryService) Check(context.Context) check.Response {
return QueryHealthCheck(s.Addr, s.InsecureSkipVerify)
}