-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseable.go
More file actions
196 lines (182 loc) · 4.8 KB
/
Copy pathparseable.go
File metadata and controls
196 lines (182 loc) · 4.8 KB
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
package tools
import (
"bytes"
"crypto/tls"
"encoding/json"
"io"
"log"
"net/http"
"os"
"strconv"
)
// These variables must be set by main.go before calling RegisterParseableTools
var (
ParseableBaseURL string
ParseableUser string
ParseablePass string
)
// package-level HTTP client; initialized in init() to respect UNSECURE env var
var HTTPClient *http.Client
func init() {
// UNSECURE environment variable controls whether TLS verification is skipped.
// Accepts the same values as strconv.ParseBool (true/1/t etc.).
unsecureEnv := os.Getenv("UNSECURE")
if ok, _ := strconv.ParseBool(unsecureEnv); ok {
HTTPClient = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
log.Printf("UNSECURE=true: HTTP client will skip TLS verification")
} else {
HTTPClient = http.DefaultClient
}
}
func addBasicAuth(req *http.Request) {
req.SetBasicAuth(ParseableUser, ParseablePass)
}
func doParseableQuery(query string, streamName string, startTime string, endTime string) ([]map[string]interface{}, error) {
payload := map[string]string{
"query": query,
"streamName": streamName,
"startTime": startTime,
"endTime": endTime,
}
jsonPayload, _ := json.Marshal(payload)
url := ParseableBaseURL + parseableSQLPath
httpReq, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonPayload))
if err != nil {
return nil, err
}
httpReq.Header.Set("Content-Type", "application/json")
addBasicAuth(httpReq)
resp, err := HTTPClient.Do(httpReq)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
// Try to unmarshal as array of rows
var arrResult []map[string]interface{}
if err := json.Unmarshal(body, &arrResult); err != nil {
return nil, err
}
return arrResult, nil
}
func listParseableStreams() ([]string, error) {
url := ParseableBaseURL + "/api/v1/logstream"
httpReq, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
addBasicAuth(httpReq)
resp, err := HTTPClient.Do(httpReq)
if err != nil {
return nil, err
}
defer func() {
if err := resp.Body.Close(); err != nil {
log.Printf("failed to close response body: %v", err)
}
}()
var apiResult []struct {
Name string `json:"name"`
}
if err := json.NewDecoder(resp.Body).Decode(&apiResult); err != nil {
return nil, err
}
streams := make([]string, 0, len(apiResult))
for _, s := range apiResult {
streams = append(streams, s.Name)
}
return streams, nil
}
func getParseableSchema(stream string) (map[string]string, error) {
url := ParseableBaseURL + "/api/v1/logstream/" + stream + "/schema"
httpReq, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
addBasicAuth(httpReq)
resp, err := HTTPClient.Do(httpReq)
if err != nil {
return nil, err
}
defer func() {
if err := resp.Body.Close(); err != nil {
log.Printf("failed to close response body: %v", err)
}
}()
var result struct {
Fields []struct {
Name string `json:"name"`
DataType json.RawMessage `json:"data_type"`
} `json:"fields"`
}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, err
}
schema := make(map[string]string)
for _, field := range result.Fields {
var dtStr string
if err := json.Unmarshal(field.DataType, &dtStr); err == nil {
schema[field.Name] = dtStr
continue
}
schema[field.Name] = string(field.DataType)
}
return schema, nil
}
func getParseableStats(streamName string) (map[string]interface{}, error) {
url := ParseableBaseURL + "/api/v1/logstream/" + streamName + "/stats"
stats, m, err := doSimpleGet(url)
if err != nil {
return m, err
}
return stats, nil
}
func getParseableInfo(streamName string) (map[string]interface{}, error) {
url := ParseableBaseURL + "/api/v1/logstream/" + streamName + "/info"
info, m, err := doSimpleGet(url)
if err != nil {
return m, err
}
return info, nil
}
func getParseableAbout() (map[string]interface{}, error) {
url := ParseableBaseURL + "/api/v1/about"
about, m, err := doSimpleGet(url)
if err != nil {
return m, err
}
return about, nil
}
func getParseableRoles() (map[string]interface{}, error) {
url := ParseableBaseURL + "/api/v1/roles"
roles, m, err := doSimpleGet(url)
if err != nil {
return m, err
}
return roles, nil
}
func doSimpleGet(url string) (map[string]interface{}, map[string]interface{}, error) {
httpReq, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, nil, err
}
addBasicAuth(httpReq)
resp, err := HTTPClient.Do(httpReq)
if err != nil {
return nil, nil, err
}
defer func() {
if err := resp.Body.Close(); err != nil {
log.Printf("failed to close response body: %v", err)
}
}()
var stats map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&stats); err != nil {
return nil, nil, err
}
return stats, nil, nil
}