-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.go
582 lines (485 loc) · 14.9 KB
/
client.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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
package es
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"path"
"strconv"
"strings"
"time"
simplejson "github.com/bitly/go-simplejson"
"github.com/grafana/grafana-aws-sdk/pkg/sigv4"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
"github.com/grafana/open-distro-for-elasticsearch-grafana-datasource/pkg/tsdb"
"golang.org/x/net/context/ctxhttp"
)
var (
clientLog = log.New()
)
// TODO: use real settings for HTTP client
var newDatasourceHttpClient = func(ds *backend.DataSourceInstanceSettings) (*http.Client, error) {
jsonDataStr := ds.JSONData
jsonData, err := simplejson.NewJson([]byte(jsonDataStr))
if err != nil {
return nil, err
}
tlsSkipVerify := jsonData.Get("tlsSkipVerify").MustBool(false)
var transport http.RoundTripper = &http.Transport{
TLSClientConfig: &tls.Config{
Renegotiation: tls.RenegotiateFreelyAsClient,
InsecureSkipVerify: tlsSkipVerify,
},
Proxy: http.ProxyFromEnvironment,
Dial: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
}
// Add SigV4 middleware if enabled
if jsonData != nil && jsonData.Get("sigV4Auth").MustBool() {
log.DefaultLogger.Debug("SigV4 auth enabled")
sigV4Config, err := GetSigV4Config(ds)
if err != nil {
return nil, err
}
transport = sigv4.New(sigV4Config, transport)
}
return &http.Client{
Timeout: 30 * time.Second,
Transport: transport,
}, nil
}
func GetSigV4Config(ds *backend.DataSourceInstanceSettings) (*sigv4.Config, error) {
decrypted := ds.DecryptedSecureJSONData
jsonDataStr := ds.JSONData
jsonData, err := simplejson.NewJson([]byte(jsonDataStr))
if err != nil {
return nil, err
}
sigV4Config := &sigv4.Config{
Service: "es", // Always "es" for elasticsearch/open distro
AccessKey: decrypted["sigV4AccessKey"],
SecretKey: decrypted["sigV4SecretKey"],
Region: jsonData.Get("sigV4Region").MustString(),
AssumeRoleARN: jsonData.Get("sigV4AssumeRoleArn").MustString(),
AuthType: jsonData.Get("sigV4AuthType").MustString(),
Profile: jsonData.Get("sigV4Profile").MustString(),
ExternalID: jsonData.Get("sigV4ExternalId").MustString(),
}
return sigV4Config, nil
}
// Client represents a client which can interact with elasticsearch api
type Client interface {
GetVersion() int
GetTimeField() string
GetMinInterval(queryInterval string) (time.Duration, error)
GetIndex() string
ExecuteMultisearch(r *MultiSearchRequest) (*MultiSearchResponse, error)
MultiSearch() *MultiSearchRequestBuilder
ExecutePPLQuery(r *PPLRequest) (*PPLResponse, error)
PPL() *PPLRequestBuilder
EnableDebug()
}
// NewClient creates a new elasticsearch client
var NewClient = func(ctx context.Context, ds *backend.DataSourceInstanceSettings, timeRange *backend.TimeRange) (Client, error) {
jsonDataStr := ds.JSONData
jsonData, err := simplejson.NewJson([]byte(jsonDataStr))
if err != nil {
return nil, err
}
version, err := jsonData.Get("esVersion").Int()
if err != nil {
return nil, fmt.Errorf("elasticsearch version is required, err=%v", err)
}
timeField, err := jsonData.Get("timeField").String()
if err != nil {
return nil, fmt.Errorf("elasticsearch time field name is required, err=%v", err)
}
indexInterval := jsonData.Get("interval").MustString()
ip, err := newIndexPattern(indexInterval, ds.Database)
if err != nil {
return nil, err
}
indices, err := ip.GetIndices(timeRange)
if err != nil {
return nil, err
}
index, err := ip.GetPPLIndex()
if err != nil {
return nil, err
}
clientLog.Debug("Creating new client", "version", version, "timeField", timeField, "indices", strings.Join(indices, ", "), "PPL index", index)
switch version {
case 2, 5, 56, 60, 70:
return &baseClientImpl{
ctx: ctx,
ds: ds,
version: version,
timeField: timeField,
indices: indices,
index: index,
timeRange: timeRange,
}, nil
}
return nil, fmt.Errorf("elasticsearch version=%d is not supported", version)
}
type baseClientImpl struct {
ctx context.Context
ds *backend.DataSourceInstanceSettings
version int
timeField string
indices []string
index string
timeRange *backend.TimeRange
debugEnabled bool
}
func (c *baseClientImpl) GetVersion() int {
return c.version
}
func (c *baseClientImpl) GetTimeField() string {
return c.timeField
}
func (c *baseClientImpl) GetIndex() string {
return c.index
}
func (c *baseClientImpl) GetMinInterval(queryInterval string) (time.Duration, error) {
intervalJSON := simplejson.New()
intervalJSON.Set("interval", queryInterval)
return tsdb.GetIntervalFrom(c.ds, intervalJSON, 5*time.Second)
}
func (c *baseClientImpl) getSettings() *simplejson.Json {
settings, _ := simplejson.NewJson(c.ds.JSONData)
return settings
}
type multiRequest struct {
header map[string]interface{}
body interface{}
interval tsdb.Interval
}
func (c *baseClientImpl) executeBatchRequest(uriPath, uriQuery string, requests []*multiRequest) (*response, error) {
bytes, err := c.encodeBatchRequests(requests)
if err != nil {
return nil, err
}
return c.executeRequest(http.MethodPost, uriPath, uriQuery, bytes)
}
func (c *baseClientImpl) encodeBatchRequests(requests []*multiRequest) ([]byte, error) {
clientLog.Debug("Encoding batch requests to json", "batch requests", len(requests))
start := time.Now()
payload := bytes.Buffer{}
for _, r := range requests {
reqHeader, err := json.Marshal(r.header)
if err != nil {
return nil, err
}
payload.WriteString(string(reqHeader) + "\n")
reqBody, err := json.Marshal(r.body)
if err != nil {
return nil, err
}
body := string(reqBody)
body = strings.ReplaceAll(body, "$__interval_ms", strconv.FormatInt(r.interval.Milliseconds(), 10))
body = strings.ReplaceAll(body, "$__interval", r.interval.Text)
payload.WriteString(body + "\n")
}
elapsed := time.Since(start)
clientLog.Debug("Encoded batch requests to json", "took", elapsed)
return payload.Bytes(), nil
}
func (c *baseClientImpl) executeRequest(method, uriPath, uriQuery string, body []byte) (*response, error) {
u, err := url.Parse(c.ds.URL)
if err != nil {
return nil, err
}
u.Path = path.Join(u.Path, uriPath)
u.RawQuery = uriQuery
var req *http.Request
if method == http.MethodPost {
req, err = http.NewRequest(http.MethodPost, u.String(), bytes.NewBuffer(body))
} else {
req, err = http.NewRequest(http.MethodGet, u.String(), nil)
}
if err != nil {
return nil, err
}
clientLog.Debug("Executing request", "url", req.URL.String(), "method", method)
var reqInfo *SearchRequestInfo
if c.debugEnabled {
reqInfo = &SearchRequestInfo{
Method: req.Method,
Url: req.URL.String(),
Data: string(body),
}
}
req.Header.Set("User-Agent", "Grafana")
req.Header.Set("Content-Type", "application/json")
secureJsonData := c.ds.DecryptedSecureJSONData
if c.ds.BasicAuthEnabled {
clientLog.Debug("Request configured to use basic authentication")
basicAuthPassword := secureJsonData["basicAuthPassword"]
req.SetBasicAuth(c.ds.BasicAuthUser, basicAuthPassword)
}
if !c.ds.BasicAuthEnabled && c.ds.User != "" {
clientLog.Debug("Request configured to use basic authentication")
password := secureJsonData["password"]
req.SetBasicAuth(c.ds.User, password)
}
httpClient, err := newDatasourceHttpClient(c.ds)
if err != nil {
return nil, err
}
start := time.Now()
defer func() {
elapsed := time.Since(start)
clientLog.Debug("Executed request", "took", elapsed)
}()
//nolint:bodyclose
resp, err := ctxhttp.Do(c.ctx, httpClient, req)
if err != nil {
return nil, err
}
return &response{
httpResponse: resp,
reqInfo: reqInfo,
}, nil
}
func (c *baseClientImpl) ExecuteMultisearch(r *MultiSearchRequest) (*MultiSearchResponse, error) {
clientLog.Debug("Executing multisearch", "search requests", len(r.Requests))
multiRequests := c.createMultiSearchRequests(r.Requests)
queryParams := c.getMultiSearchQueryParameters()
clientRes, err := c.executeBatchRequest("_msearch", queryParams, multiRequests)
if err != nil {
return nil, err
}
res := clientRes.httpResponse
defer res.Body.Close()
clientLog.Debug("Received multisearch response", "code", res.StatusCode, "status", res.Status, "content-length", res.ContentLength)
start := time.Now()
clientLog.Debug("Decoding multisearch json response")
var bodyBytes []byte
if c.debugEnabled {
tmpBytes, err := ioutil.ReadAll(res.Body)
if err != nil {
clientLog.Error("failed to read http response bytes", "error", err)
} else {
bodyBytes = make([]byte, len(tmpBytes))
copy(bodyBytes, tmpBytes)
res.Body = ioutil.NopCloser(bytes.NewBuffer(tmpBytes))
}
}
var msr MultiSearchResponse
dec := json.NewDecoder(res.Body)
err = dec.Decode(&msr)
if err != nil {
return nil, err
}
elapsed := time.Since(start)
clientLog.Debug("Decoded multisearch json response", "took", elapsed)
msr.Status = res.StatusCode
if c.debugEnabled {
bodyJSON, err := simplejson.NewFromReader(bytes.NewBuffer(bodyBytes))
var data *simplejson.Json
if err != nil {
clientLog.Error("failed to decode http response into json", "error", err)
} else {
data = bodyJSON
}
msr.DebugInfo = &SearchDebugInfo{
Request: clientRes.reqInfo,
Response: &SearchResponseInfo{
Status: res.StatusCode,
Data: data,
},
}
}
return &msr, nil
}
func (c *baseClientImpl) createMultiSearchRequests(searchRequests []*SearchRequest) []*multiRequest {
multiRequests := []*multiRequest{}
for _, searchReq := range searchRequests {
mr := multiRequest{
header: map[string]interface{}{
"search_type": "query_then_fetch",
"ignore_unavailable": true,
"index": strings.Join(c.indices, ","),
},
body: searchReq,
interval: searchReq.Interval,
}
if c.version == 2 {
mr.header["search_type"] = "count"
}
if c.version >= 56 && c.version < 70 {
maxConcurrentShardRequests := c.getSettings().Get("maxConcurrentShardRequests").MustInt(256)
mr.header["max_concurrent_shard_requests"] = maxConcurrentShardRequests
}
multiRequests = append(multiRequests, &mr)
}
return multiRequests
}
func (c *baseClientImpl) getMultiSearchQueryParameters() string {
if c.version >= 70 {
maxConcurrentShardRequests := c.getSettings().Get("maxConcurrentShardRequests").MustInt(5)
return fmt.Sprintf("max_concurrent_shard_requests=%d", maxConcurrentShardRequests)
}
return ""
}
func (c *baseClientImpl) MultiSearch() *MultiSearchRequestBuilder {
return NewMultiSearchRequestBuilder(c.GetVersion())
}
func (c *baseClientImpl) EnableDebug() {
c.debugEnabled = true
}
type pplRequest struct {
body interface{}
}
func (c *baseClientImpl) executePPLRequest(uriPath string, request *pplRequest) (*pplresponse, error) {
bytes, err := c.encodePPLRequests(request)
if err != nil {
return nil, err
}
return c.executePPLQueryRequest(http.MethodPost, uriPath, bytes)
}
func (c *baseClientImpl) encodePPLRequests(requests *pplRequest) ([]byte, error) {
clientLog.Debug("Encoding PPL requests to json")
start := time.Now()
reqBody, err := json.Marshal(requests.body)
if err != nil {
return nil, err
}
body := string(reqBody)
// replace the escaped characters in time range filtering
body = strings.ReplaceAll(body, "\\u003c", "<")
body = strings.ReplaceAll(body, "\\u003e", ">")
elapsed := time.Since(start)
clientLog.Debug("Encoded PPL requests to json", "took", elapsed)
return []byte(body + "\n"), nil
}
func (c *baseClientImpl) executePPLQueryRequest(method, uriPath string, body []byte) (*pplresponse, error) {
u, err := url.Parse(c.ds.URL)
if err != nil {
return nil, err
}
u.Path = path.Join(u.Path, uriPath)
var req *http.Request
if method == http.MethodPost {
req, err = http.NewRequest(http.MethodPost, u.String(), bytes.NewBuffer(body))
} else {
req, err = http.NewRequest(http.MethodGet, u.String(), nil)
}
if err != nil {
return nil, err
}
clientLog.Debug("Executing request", "url", req.URL.String(), "method", method)
var reqInfo *PPLRequestInfo
if c.debugEnabled {
reqInfo = &PPLRequestInfo{
Method: req.Method,
URL: req.URL.String(),
Data: string(body),
}
}
req.Header.Set("User-Agent", "Grafana")
req.Header.Set("Content-Type", "application/json")
secureJsonData := c.ds.DecryptedSecureJSONData
if c.ds.BasicAuthEnabled {
clientLog.Debug("Request configured to use basic authentication")
basicAuthPassword := secureJsonData["basicAuthPassword"]
req.SetBasicAuth(c.ds.BasicAuthUser, basicAuthPassword)
}
if !c.ds.BasicAuthEnabled && c.ds.User != "" {
clientLog.Debug("Request configured to use basic authentication")
password := secureJsonData["password"]
req.SetBasicAuth(c.ds.User, password)
}
httpClient, err := newDatasourceHttpClient(c.ds)
if err != nil {
return nil, err
}
start := time.Now()
defer func() {
elapsed := time.Since(start)
clientLog.Debug("Executed request", "took", elapsed)
}()
//nolint:bodyclose
resp, err := ctxhttp.Do(c.ctx, httpClient, req)
if err != nil {
return nil, err
}
return &pplresponse{
httpResponse: resp,
reqInfo: reqInfo,
}, nil
}
func (c *baseClientImpl) ExecutePPLQuery(r *PPLRequest) (*PPLResponse, error) {
clientLog.Debug("Executing PPL")
req := createPPLRequest(r)
clientRes, err := c.executePPLRequest("_opendistro/_ppl", req)
if err != nil {
return nil, err
}
resp := clientRes.httpResponse
defer resp.Body.Close()
clientLog.Debug("Received PPL response", "code", resp.StatusCode, "status", resp.Status, "content-length", resp.ContentLength)
start := time.Now()
clientLog.Debug("Decoding PPL json response")
var bodyBytes []byte
if c.debugEnabled {
tmpBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
clientLog.Error("failed to read http response bytes", "error", err)
} else {
bodyBytes = make([]byte, len(tmpBytes))
copy(bodyBytes, tmpBytes)
resp.Body = ioutil.NopCloser(bytes.NewBuffer(tmpBytes))
}
}
var pr PPLResponse
dec := json.NewDecoder(resp.Body)
err = dec.Decode(&pr)
if err != nil {
return nil, err
}
elapsed := time.Since(start)
clientLog.Debug("Decoded PPL json response", "took", elapsed)
pr.Status = resp.StatusCode
if c.debugEnabled {
bodyJSON, err := simplejson.NewFromReader(bytes.NewBuffer(bodyBytes))
var data *simplejson.Json
if err != nil {
clientLog.Error("failed to decode http response into json", "error", err)
} else {
data = bodyJSON
}
pr.DebugInfo = &PPLDebugInfo{
Request: clientRes.reqInfo,
Response: &PPLResponseInfo{
Status: resp.StatusCode,
Data: data,
},
}
}
if err != nil {
return nil, err
}
return &pr, nil
}
func createPPLRequest(request *PPLRequest) *pplRequest {
return &pplRequest{
body: request,
}
}
func (c *baseClientImpl) PPL() *PPLRequestBuilder {
return NewPPLRequestBuilder(c.GetIndex())
}