-
Notifications
You must be signed in to change notification settings - Fork 0
/
humio.go
129 lines (111 loc) · 3.04 KB
/
humio.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httputil"
"strconv"
"go.uber.org/zap"
)
type client struct {
httpClient *http.Client
token string
baseURL string
}
type startQueryPayload struct {
QueryString string `json:"queryString"`
Start string `json:"start"`
End string `json:"end"`
IsLive bool `json:"isLive"`
}
func (c *client) startQueryJob(query, repo, metricName, start, end string, labels []MetricLabel) (queryJob, error) {
postData := startQueryPayload{
QueryString: query,
Start: start,
End: end,
IsLive: true,
}
var reader bytes.Buffer
err := json.NewEncoder(&reader).Encode(&postData)
if err != nil {
return queryJob{}, err
}
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/api/v1/repositories/%s/queryjobs", c.baseURL, repo), &reader)
if err != nil {
return queryJob{}, err
}
response, err := c.do(req)
if err != nil {
return queryJob{}, err
}
var queryResponse queryJob
err = json.NewDecoder(response.Body).Decode(&queryResponse)
if err != nil {
return queryJob{}, err
}
queryResponse.Timespan = start
queryResponse.Repo = repo
queryResponse.MetricName = metricName
queryResponse.MetricLabels = labels
return queryResponse, nil
}
func (c *client) stopQueryJob(id, repo string) error {
req, err := http.NewRequest(http.MethodDelete, fmt.Sprintf("%s/api/v1/repositories/%s/queryjobs/%s", c.baseURL, repo, id), nil)
if err != nil {
return err
}
_, err = c.do(req)
if err != nil {
return err
}
return nil
}
func (c *client) pollQueryJob(id, repo string) (queryJobData, error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/api/v1/repositories/%s/queryjobs/%s", c.baseURL, repo, id), nil)
if err != nil {
return queryJobData{}, err
}
response, err := c.do(req)
if err != nil {
return queryJobData{}, err
}
var queryJobDataResponse queryJobData
err = json.NewDecoder(response.Body).Decode(&queryJobDataResponse)
if err != nil {
return queryJobData{}, err
}
return queryJobDataResponse, nil
}
func (c *client) do(req *http.Request) (*http.Response, error) {
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.token))
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
response, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
if response.StatusCode != http.StatusOK {
body, err := ioutil.ReadAll(response.Body)
if err != nil {
zap.L().Sugar().Errorf("read body failed: %v", err)
body = []byte("failed to read body")
}
requestDump, err := httputil.DumpRequestOut(req, true)
if err != nil {
zap.L().Sugar().Debugf("Failed to dump request for logging")
} else {
zap.L().Sugar().Debugf("Failed request dump: %s", requestDump)
}
return nil, fmt.Errorf("request not OK: %s: body: %s", response.Status, body)
}
return response, nil
}
func parseFloat(input interface{}) (float64, error) {
value, err := strconv.ParseFloat(input.(string), 64)
if err != nil {
return 0, err
}
return value, nil
}