-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathclickhouse.go
228 lines (189 loc) · 6.62 KB
/
clickhouse.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
package clickhouse
import (
"net/http"
"strconv"
"time"
"github.com/kobsio/kobs/pkg/api/clusters"
"github.com/kobsio/kobs/pkg/api/middleware/errresponse"
"github.com/kobsio/kobs/pkg/api/plugins/plugin"
"github.com/kobsio/kobs/plugins/clickhouse/pkg/instance"
"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
"github.com/sirupsen/logrus"
)
// Route is the route under which the plugin should be registered in our router for the rest api.
const Route = "/clickhouse"
var (
log = logrus.WithFields(logrus.Fields{"package": "clickhouse"})
)
// Config is the structure of the configuration for the clickhouse plugin.
type Config []instance.Config
type logsResponse struct {
Documents []map[string]interface{} `json:"documents"`
Fields []string `json:"fields"`
Offset int64 `json:"offset"`
}
// Router implements the router for the resources plugin, which can be registered in the router for our rest api.
type Router struct {
*chi.Mux
clusters *clusters.Clusters
instances []*instance.Instance
}
func (router *Router) getInstance(name string) *instance.Instance {
for _, i := range router.instances {
if i.Name == name {
return i
}
}
return nil
}
func (router *Router) getSQL(w http.ResponseWriter, r *http.Request) {
name := chi.URLParam(r, "name")
query := r.URL.Query().Get("query")
log.WithFields(logrus.Fields{"name": name, "query": query}).Tracef("getSQL")
i := router.getInstance(name)
if i == nil {
errresponse.Render(w, r, nil, http.StatusBadRequest, "Could not find instance name")
return
}
rows, columns, err := i.GetSQL(r.Context(), query)
if err != nil {
errresponse.Render(w, r, err, http.StatusBadRequest, "Could not get result for SQL query")
return
}
data := struct {
Rows [][]interface{} `json:"rows"`
Columns []string `json:"columns"`
}{
rows,
columns,
}
render.JSON(w, r, data)
}
// getLogs implements the special handling when the user selected the "logs" options for the "view" configuration. This
// options is intended to use together with the kobsio/fluent-bit-clickhouse Fluent Bit plugin and provides a custom
// query language to get the logs from ClickHouse.
// Next to the query and time range, a user can also provide a limit and offset to page through all the logs. The limit
// shouldn't be larger then 1000 and if the offset is empty we use 0, which indicates a new query in our React UI.
func (router *Router) getLogs(w http.ResponseWriter, r *http.Request) {
name := chi.URLParam(r, "name")
query := r.URL.Query().Get("query")
order := r.URL.Query().Get("order")
orderBy := r.URL.Query().Get("orderBy")
maxDocuments := r.URL.Query().Get("maxDocuments")
limit := r.URL.Query().Get("limit")
offset := r.URL.Query().Get("offset")
timeStart := r.URL.Query().Get("timeStart")
timeEnd := r.URL.Query().Get("timeEnd")
log.WithFields(logrus.Fields{"name": name, "query": query, "order": order, "orderBy": orderBy, "maxDocuments": maxDocuments, "limit": limit, "offset": offset, "timeStart": timeStart, "timeEnd": timeEnd}).Tracef("getLogs")
i := router.getInstance(name)
if i == nil {
errresponse.Render(w, r, nil, http.StatusBadRequest, "Could not find instance name")
return
}
parsedLimit, err := strconv.ParseInt(limit, 10, 64)
if err != nil || parsedLimit > 1000 {
errresponse.Render(w, r, err, http.StatusBadRequest, "Could not parse limit")
return
}
parsedOffset := int64(0)
if offset != "" {
parsedOffset, err = strconv.ParseInt(offset, 10, 64)
if err != nil {
errresponse.Render(w, r, err, http.StatusBadRequest, "Could not parse offset")
return
}
}
parsedMaxDocuments := int64(1000)
if maxDocuments != "" {
parsedMaxDocuments, err = strconv.ParseInt(maxDocuments, 10, 64)
if err != nil {
errresponse.Render(w, r, err, http.StatusBadRequest, "Could not parse maxDocuments")
return
}
}
parsedTimeStart, err := strconv.ParseInt(timeStart, 10, 64)
if err != nil {
errresponse.Render(w, r, err, http.StatusBadRequest, "Could not parse start time")
return
}
parsedTimeEnd, err := strconv.ParseInt(timeEnd, 10, 64)
if err != nil {
errresponse.Render(w, r, err, http.StatusBadRequest, "Could not parse end time")
return
}
// Query for larger time ranges can took several minutes to be completed. To avoid that the connection is closed for
// these long running requests by a load balancer which sits infront of kobs, we are writing a newline character
// every 10 seconds. We shouldn't write sth. else, because this would make parsing the response in the React UI more
// diffucult and with the newline character parsing works in the same ways as it was before.
done := make(chan bool)
go func() {
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()
for {
select {
case <-done:
return
case <-ticker.C:
if f, ok := w.(http.Flusher); ok {
w.Write([]byte("\n"))
w.WriteHeader(http.StatusContinue)
f.Flush()
}
}
}
}()
documents, fields, count, took, buckets, newOffset, newTimeStart, err := i.GetLogs(r.Context(), query, order, orderBy, parsedMaxDocuments, parsedLimit, parsedOffset, parsedTimeStart, parsedTimeEnd)
if err != nil {
errresponse.Render(w, r, err, http.StatusBadRequest, "Could not get logs")
return
}
done <- true
data := struct {
Documents []map[string]interface{} `json:"documents"`
Fields []string `json:"fields"`
Count int64 `json:"count"`
Took int64 `json:"took"`
Buckets []instance.Bucket `json:"buckets"`
Offset int64 `json:"offset"`
TimeStart int64 `json:"timeStart"`
}{
documents,
fields,
count,
took,
buckets,
newOffset,
newTimeStart,
}
render.JSON(w, r, data)
}
// Register returns a new router which can be used in the router for the kobs rest api.
func Register(clusters *clusters.Clusters, plugins *plugin.Plugins, config Config) chi.Router {
var instances []*instance.Instance
for _, cfg := range config {
instance, err := instance.New(cfg)
if err != nil {
log.WithError(err).WithFields(logrus.Fields{"name": cfg.Name}).Fatalf("Could not create ClickHouse instance")
}
instances = append(instances, instance)
var options map[string]interface{}
options = make(map[string]interface{})
options["type"] = cfg.Type
plugins.Append(plugin.Plugin{
Name: cfg.Name,
DisplayName: cfg.DisplayName,
Description: cfg.Description,
Type: "clickhouse",
Options: options,
})
}
router := Router{
chi.NewRouter(),
clusters,
instances,
}
router.Get("/sql/{name}", router.getSQL)
router.Get("/logs/{name}", router.getLogs)
return router
}