-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathklogs.go
More file actions
245 lines (203 loc) · 7.59 KB
/
klogs.go
File metadata and controls
245 lines (203 loc) · 7.59 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
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
package klogs
import (
"encoding/json"
"net/http"
"strconv"
"time"
"github.com/kobsio/kobs/pkg/kube/clusters"
"github.com/kobsio/kobs/pkg/log"
"github.com/kobsio/kobs/pkg/middleware/errresponse"
"github.com/kobsio/kobs/pkg/satellite/plugins/plugin"
"github.com/kobsio/kobs/plugins/plugin-klogs/pkg/instance"
"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
"go.uber.org/zap"
)
// PluginType is the type which must be used for the klogs plugin.
const PluginType = "klogs"
// Router implements the router for the klogs plugin, which can be registered in the router for our rest api. It contains
// the api routes for the klogs plugin and it's configuration.
type Router struct {
*chi.Mux
instances []instance.Instance
}
// getInstance returns a klogs instance by it's name. If we couldn't found an instance with the provided name and the
// provided name is "default" we return the first instance from the list. The first instance in the list is also the
// first one configured by the user and can be used as default one.
func (router *Router) getInstance(name string) instance.Instance {
for _, i := range router.instances {
if i.GetName() == name {
return i
}
}
if name == "default" && len(router.instances) > 0 {
return router.instances[0]
}
return nil
}
func (router *Router) getFields(w http.ResponseWriter, r *http.Request) {
name := r.Header.Get("x-kobs-plugin")
filter := r.URL.Query().Get("filter")
fieldType := r.URL.Query().Get("fieldType")
log.Debug(r.Context(), "Get fields parameters", zap.String("name", name), zap.String("filter", filter), zap.String("fieldType", fieldType))
i := router.getInstance(name)
if i == nil {
log.Error(r.Context(), "Could not find instance name", zap.String("name", name))
errresponse.Render(w, r, nil, http.StatusBadRequest, "Could not find instance name")
return
}
fields := i.GetFields(filter, fieldType)
log.Debug(r.Context(), "Get fields result", zap.Int("fieldsCount", len(fields)))
render.JSON(w, r, fields)
}
// 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/klogs Fluent Bit plugin and provides a custom query language to
// get the logs from ClickHouse ingested via kobsio/klogs.
func (router *Router) getLogs(w http.ResponseWriter, r *http.Request) {
name := r.Header.Get("x-kobs-plugin")
query := r.URL.Query().Get("query")
order := r.URL.Query().Get("order")
orderBy := r.URL.Query().Get("orderBy")
timeStart := r.URL.Query().Get("timeStart")
timeEnd := r.URL.Query().Get("timeEnd")
log.Debug(r.Context(), "Get logs paramters", zap.String("name", name), zap.String("query", query), zap.String("order", order), zap.String("orderBy", orderBy), zap.String("timeStart", timeStart), zap.String("timeEnd", timeEnd))
i := router.getInstance(name)
if i == nil {
log.Error(r.Context(), "Could not find instance name", zap.String("name", name))
errresponse.Render(w, r, nil, http.StatusBadRequest, "Could not find instance name")
return
}
parsedTimeStart, err := strconv.ParseInt(timeStart, 10, 64)
if err != nil {
log.Error(r.Context(), "Could not parse start time", zap.Error(err))
errresponse.Render(w, r, err, http.StatusBadRequest, "Could not parse start time")
return
}
parsedTimeEnd, err := strconv.ParseInt(timeEnd, 10, 64)
if err != nil {
log.Error(r.Context(), "Could not parse end time", zap.Error(err))
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 {
// We do not set the processing status code, so that the queries always are returning a 200. This is
// necessary because Go doesn't allow to set a new status code once the header was written.
// See: https://github.com/golang/go/issues/36734
// For that we also have to handle errors, when the status code is 200 in the React UI.
// See plugins/klogs/src/components/page/Logs.tsx#L64
// w.WriteHeader(http.StatusProcessing)
w.Write([]byte("\n"))
f.Flush()
}
}
}
}()
defer func() {
done <- true
}()
documents, fields, count, took, buckets, err := i.GetLogs(r.Context(), query, order, orderBy, 1000, parsedTimeStart, parsedTimeEnd)
if err != nil {
log.Error(r.Context(), "Could not get logs", zap.Error(err))
errresponse.Render(w, r, err, http.StatusBadRequest, "Could not get logs")
return
}
data := struct {
Documents []map[string]any `json:"documents"`
Fields []instance.Field `json:"fields"`
Count int64 `json:"count"`
Took int64 `json:"took"`
Buckets []instance.Bucket `json:"buckets"`
}{
documents,
fields,
count,
took,
buckets,
}
render.JSON(w, r, data)
}
// getAggregation returns the columns and rows for the user given aggregation request. The aggregation data must
// provided in the body of the request and is the run against the specified Clichouse instance.
func (router *Router) getAggregation(w http.ResponseWriter, r *http.Request) {
name := r.Header.Get("x-kobs-plugin")
log.Debug(r.Context(), "Get aggregation paramters", zap.String("name", name))
i := router.getInstance(name)
if i == nil {
log.Error(r.Context(), "Could not find instance name", zap.String("name", name))
errresponse.Render(w, r, nil, http.StatusBadRequest, "Could not find instance name")
return
}
var aggregationData instance.Aggregation
err := json.NewDecoder(r.Body).Decode(&aggregationData)
if err != nil {
log.Error(r.Context(), "Could not decode request body", zap.Error(err))
errresponse.Render(w, r, err, http.StatusBadRequest, "Could not decode request body")
return
}
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.WriteHeader(http.StatusProcessing)
w.Write([]byte("\n"))
f.Flush()
}
}
}
}()
defer func() {
done <- true
}()
rows, columns, err := i.GetAggregation(r.Context(), aggregationData)
if err != nil {
log.Error(r.Context(), "Error while running aggregation", zap.Error(err))
errresponse.Render(w, r, err, http.StatusBadRequest, "Error while running aggregation")
return
}
data := struct {
Rows []map[string]any `json:"rows"`
Columns []string `json:"columns"`
}{
rows,
columns,
}
render.JSON(w, r, data)
}
// Mount mounts the klogs plugin routes in the plugins router of a kobs satellite instance.
func Mount(instances []plugin.Instance, clustersClient clusters.Client) (chi.Router, error) {
var klogsInstances []instance.Instance
for _, i := range instances {
klogsInstance, err := instance.New(i.Name, i.Options)
if err != nil {
return nil, err
}
klogsInstances = append(klogsInstances, klogsInstance)
}
router := Router{
chi.NewRouter(),
klogsInstances,
}
router.Get("/fields", router.getFields)
router.Get("/logs", router.getLogs)
router.Post("/aggregation", router.getAggregation)
return router, nil
}