-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
prometheus_handlers.go
518 lines (457 loc) · 17 KB
/
prometheus_handlers.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
package handlers
import (
"context"
"encoding/gob"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"sync"
"github.com/gofrs/uuid"
"github.com/gorilla/mux"
"github.com/layer5io/meshery/server/models"
"github.com/layer5io/meshery/server/models/connections"
"github.com/layer5io/meshkit/models/events"
)
func init() {
gob.Register(&models.PrometheusClient{})
}
// swagger:route GET /api/system/meshsync/grafana SystemAPI idMeshSyncGrafana
// Handle GET request for mesh-sync grafana
//
// Fetches Prometheus and Grafana
// responses:
// 200: v1ServicesMapResponseWrapper
// ScanPromGrafanaHandler - fetches Prometheus and Grafana
// func (h *Handler) ScanPromGrafanaHandler(w http.ResponseWriter, req *http.Request, prefObj *models.Preference, user *models.User, provider models.Provider) {
// errs := []string{}
// var wg sync.WaitGroup
// customK8scontexts, ok := req.Context().Value(models.KubeClustersKey).([]models.K8sContext)
// if ok && len(customK8scontexts) > 0 {
// for _, mk8scontext := range customK8scontexts {
// wg.Add(1)
// go func(mk8scontext models.K8sContext) {
// defer wg.Done()
// k8sconfig, err := mk8scontext.GenerateKubeConfig()
// if err != nil {
// errs = append(errs, err.Error())
// h.log.Error(err)
// return
// }
// availablePromGrafana, err := helpers.ScanPromGrafana(k8sconfig, mk8scontext.Name)
// if err != nil {
// errs = append(errs, err.Error())
// h.log.Error(err)
// return
// }
// if err = json.NewEncoder(w).Encode(availablePromGrafana); err != nil {
// obj := "payloads"
// h.log.Error(ErrMarshal(err, obj))
// errs = append(errs, ErrMarshal(err, obj).Error())
// return
// }
// }(mk8scontext)
// }
// }
// if len(errs) != 0 {
// http.Error(w, mergeMsgs(errs), http.StatusInternalServerError)
// }
// wg.Wait()
// }
// swagger:route GET /api/system/meshsync/prometheus SystemAPI idMeshSyncPrometheus
// Handle GET request for fetching prometheus
//
// Fetches Prometheus
// responses:
// 200: v1ServicesMapResponseWrapper
// ScanPrometheusHandler - fetches Prometheus
// func (h *Handler) ScanPrometheusHandler(w http.ResponseWriter, req *http.Request, prefObj *models.Preference, user *models.User, provider models.Provider) {
// errs := []string{}
// var wg sync.WaitGroup
// customK8scontexts, ok := req.Context().Value(models.KubeClustersKey).([]models.K8sContext)
// if ok && len(customK8scontexts) > 0 {
// for _, mk8scontext := range customK8scontexts {
// wg.Add(1)
// go func(mk8scontext models.K8sContext) {
// defer wg.Done()
// k8sconfig, err := mk8scontext.GenerateKubeConfig()
// if err != nil {
// errs = append(errs, err.Error())
// h.log.Error(err)
// return
// }
// availablePromGrafana, err := helpers.ScanPrometheus(k8sconfig, mk8scontext.Name)
// if err != nil {
// errs = append(errs, err.Error())
// h.log.Error(err)
// return
// }
// if err = json.NewEncoder(w).Encode(availablePromGrafana); err != nil {
// obj := "payloads"
// h.log.Error(ErrMarshal(err, obj))
// errs = append(errs, ErrMarshal(err, obj).Error())
// return
// }
// }(mk8scontext)
// }
// }
// if len(errs) != 0 {
// http.Error(w, mergeMsgs(errs), http.StatusInternalServerError)
// }
// wg.Wait()
// }
// swagger:route GET /api/telemetry/metrics/grafana/scan GrafanaAPI idGetGrafana
// Handle GET request for Grafana
//
// Fetches and returns Grafana
// responses:
// 200: v1ServicesMapResponseWrapper
// ScanGrafanaHandler - fetches Grafana
// func (h *Handler) ScanGrafanaHandler(w http.ResponseWriter, req *http.Request, prefObj *models.Preference, user *models.User, provider models.Provider) {
// errs := []string{}
// var wg sync.WaitGroup
// customK8scontexts, ok := req.Context().Value(models.KubeClustersKey).([]models.K8sContext)
// if ok && len(customK8scontexts) > 0 {
// for _, mk8scontext := range customK8scontexts {
// wg.Add(1)
// go func(mk8scontext models.K8sContext) {
// defer wg.Done()
// k8sconfig, err := mk8scontext.GenerateKubeConfig()
// if err != nil {
// errs = append(errs, err.Error())
// h.log.Error(err)
// return
// }
// availablePromGrafana, err := helpers.ScanGrafana(k8sconfig, mk8scontext.Name)
// if err != nil {
// errs = append(errs, err.Error())
// h.log.Error(err)
// return
// }
// if err = json.NewEncoder(w).Encode(availablePromGrafana); err != nil {
// obj := "payloads"
// h.log.Error(ErrMarshal(err, obj))
// errs = append(errs, ErrMarshal(err, obj).Error())
// return
// }
// }(mk8scontext)
// }
// }
// if len(errs) != 0 {
// http.Error(w, mergeMsgs(errs), http.StatusInternalServerError)
// }
// wg.Wait()
// }
// swagger:route GET /api/telemetry/metrics/config PrometheusAPI idGetPrometheusConfig
// Handle GET for Prometheus configuration
//
// Used for fetching Prometheus configuration
// responses:
// 200: prometheusConfigResponseWrapper
// swagger:route POST /api/telemetry/metrics/config PrometheusAPI idPostPrometheusConfig
// Handle POST for Prometheus configuration
//
// Used for persisting Prometheus configuration
// responses:
// 200:
// swagger:route DELETE /api/telemetry/metrics/config PrometheusAPI idDeletePrometheusConfig
// Handle DELETE for Prometheus configuration
//
// Used for deleting Prometheus configuration
// responses:
// 200:
// PrometheusConfigHandler is used for persisting prometheus configuration
func (h *Handler) PrometheusConfigHandler(w http.ResponseWriter, req *http.Request, prefObj *models.Preference, user *models.User, provider models.Provider) {
// if req.Method != http.MethodPost && req.Method != http.MethodDelete {
// w.WriteHeader(http.StatusNotFound)
// return
// }
if req.Method == http.MethodGet {
req = mux.SetURLVars(req, map[string]string{"connectionKind": "prometheus"})
h.GetConnectionsByKind(w, req, prefObj, user, provider)
return
}
token, _ := req.Context().Value(models.TokenCtxKey).(string)
sysID := h.SystemID
userUUID := uuid.FromStringOrNil(user.ID)
eventBuilder := events.NewEvent().ActedUpon(userUUID).WithCategory("connection").WithAction("update").FromSystem(*sysID).FromUser(userUUID).WithDescription("Failed to interact with the connection.")
if req.Method == http.MethodPost {
promURL := req.FormValue("prometheusURL")
promKey := req.FormValue("prometheusKey")
connName := req.FormValue("prometheusConnectionName")
credName := req.FormValue("prometheusCredentialName")
u, err := url.Parse(promURL)
if err != nil {
return
}
if strings.Contains(promURL, u.RequestURI()) {
promURL = strings.TrimSuffix(promURL, u.RequestURI())
}
promConn := map[string]interface{}{
"url": promURL,
}
promCred := map[string]interface{}{
"secret": promKey,
}
if err := h.config.PrometheusClient.Validate(req.Context(), promURL, promKey); err != nil {
h.log.Error(models.ErrPrometheusScan(err))
http.Error(w, models.ErrPrometheusScan(err).Error(), http.StatusInternalServerError)
return
}
userUUID := uuid.FromStringOrNil(user.ID)
credential, err := provider.SaveUserCredential(token, &models.Credential{
UserID: &userUUID,
Type: "prometheus",
Secret: promCred,
Name: credName,
})
if err != nil {
_err := models.ErrPersistCredential(err)
event := eventBuilder.WithDescription(fmt.Sprintf("Unable to persist credential information for the connection %s", credName)).
WithSeverity(events.Error).WithMetadata(map[string]interface{}{"error": _err}).Build()
_ = provider.PersistEvent(event)
go h.config.EventBroadcaster.Publish(userUUID, event)
http.Error(w, _err.Error(), http.StatusInternalServerError)
return
}
connection, err := provider.SaveConnection(&models.ConnectionPayload{
Kind: "prometheus",
Type: "observability",
SubType: "monitoring",
Status: connections.CONNECTED,
MetaData: promConn,
CredentialSecret: promCred,
Name: connName,
CredentialID: &credential.ID,
}, token, false)
if err != nil {
_err := models.ErrPersistConnection(err)
event := eventBuilder.WithDescription(fmt.Sprintf("Unable to perisit the \"%s\" connection details", connName)).WithMetadata(map[string]interface{}{"error": _err}).Build()
_ = provider.PersistEvent(event)
go h.config.EventBroadcaster.Publish(userUUID, event)
http.Error(w, _err.Error(), http.StatusInternalServerError)
return
}
event := eventBuilder.WithDescription(fmt.Sprintf("Connection %s with prometheus created at %s", connName, promURL)).WithSeverity(events.Success).ActedUpon(connection.ID).Build()
_ = provider.PersistEvent(event)
go h.config.EventBroadcaster.Publish(userUUID, event)
h.log.Debug("Prometheus URL %s successfully saved", promURL)
} else if req.Method == http.MethodDelete {
http.Error(w, "API is deprecated, please use connections API", http.StatusGone)
return
}
err := provider.RecordPreferences(req, user.UserID, prefObj)
if err != nil {
h.log.Error(ErrRecordPreferences(err))
http.Error(w, ErrRecordPreferences(err).Error(), http.StatusInternalServerError)
return
}
_, _ = w.Write([]byte("{}"))
}
// swagger:route GET /api/telemetry/metrics/ping/{connectionID} PrometheusAPI idGetPrometheusPing
// Handle GET request for Prometheus Ping
//
// Used to ping prometheus
// responses:
// 200:
// PrometheusPingHandler - fetches server version to simulate ping
func (h *Handler) PrometheusPingHandler(w http.ResponseWriter, req *http.Request, prefObj *models.Preference, _ *models.User, p models.Provider) {
token, _ := req.Context().Value(models.TokenCtxKey).(string)
connectionID := uuid.FromStringOrNil(mux.Vars(req)["connectionID"])
connection, statusCode, err := p.GetConnectionByID(token, connectionID, "prometheus")
if err != nil {
http.Error(w, err.Error(), statusCode)
return
}
url, _ := connection.Metadata["url"].(string)
cred, statusCode, err := p.GetCredentialByID(token, connection.CredentialID)
if err != nil {
http.Error(w, err.Error(), statusCode)
return
}
apiKeyOrBasicAuth, _ := cred.Secret["secret"].(string)
if err := h.config.PrometheusClient.Validate(req.Context(), url, apiKeyOrBasicAuth); err != nil {
h.log.Error(models.ErrPrometheusScan(err))
http.Error(w, models.ErrPrometheusScan(err).Error(), http.StatusInternalServerError)
return
}
_, _ = w.Write([]byte("{}"))
}
// swagger:route POST /api/telemetry/metrics/board_import PrometheusAPI idPostPrometheusBoardImport
// Handle POST request for Prometheus board import
//
// Used for importing Grafana board for Prometheus
// responses:
// 200: prometheusBoardImportRespWrapper
// GrafanaBoardImportForPrometheusHandler accepts a Grafana board json, parses it and returns the list of panels
func (h *Handler) GrafanaBoardImportForPrometheusHandler(w http.ResponseWriter, req *http.Request, prefObj *models.Preference, _ *models.User, _ models.Provider) {
defer func() {
_ = req.Body.Close()
}()
boardData, err := io.ReadAll(req.Body)
if err != nil {
h.log.Error(ErrRequestBody(err))
http.Error(w, ErrRequestBody(err).Error(), http.StatusInternalServerError)
return
}
board, err := h.config.PrometheusClient.ImportGrafanaBoard(req.Context(), boardData)
if err != nil {
h.log.Error(ErrPrometheusBoards(err))
http.Error(w, ErrPrometheusBoards(err).Error(), http.StatusInternalServerError)
return
}
err = json.NewEncoder(w).Encode(board)
if err != nil {
obj := "board instance"
h.log.Error(models.ErrMarshal(err, obj))
http.Error(w, models.ErrMarshal(err, obj).Error(), http.StatusInternalServerError)
return
}
}
// swagger:route GET /api/telemetry/metrics/query/{connectionID} PrometheusAPI idGetPrometheusQuery
// Handle GET request for Prometheus Query
//
// Used to prometheus queries
// responses:
// 200:
// PrometheusQueryHandler handles prometheus queries
func (h *Handler) PrometheusQueryHandler(w http.ResponseWriter, req *http.Request, prefObj *models.Preference, _ *models.User, p models.Provider) {
token, _ := req.Context().Value(models.TokenCtxKey).(string)
connectionID := uuid.FromStringOrNil(mux.Vars(req)["connectionID"])
connection, statusCode, err := p.GetConnectionByID(token, connectionID, "prometheus")
if err != nil {
http.Error(w, err.Error(), statusCode)
return
}
url, _ := connection.Metadata["url"].(string)
reqQuery := req.URL.Query()
data, err := h.config.PrometheusClientForQuery.Query(req.Context(), url, &reqQuery)
if err != nil {
h.log.Error(ErrPrometheusQuery(err))
http.Error(w, ErrPrometheusQuery(err).Error(), http.StatusInternalServerError)
return
}
_, _ = w.Write(data)
}
// PrometheusQueryRangeHandler handles prometheus range queries
func (h *Handler) PrometheusQueryRangeHandler(w http.ResponseWriter, req *http.Request, prefObj *models.Preference, user *models.User, provider models.Provider) {
token, _ := req.Context().Value(models.TokenCtxKey).(string)
connectionID := uuid.FromStringOrNil(mux.Vars(req)["connectionID"])
connection, statusCode, err := provider.GetConnectionByID(token, connectionID, "prometheus")
if err != nil {
http.Error(w, err.Error(), statusCode)
return
}
reqQuery := req.URL.Query()
url, _ := connection.Metadata["url"].(string)
testUUID := reqQuery.Get("uuid")
if testUUID != "" {
q := reqQuery.Get("query")
h.config.QueryTracker.AddOrFlagQuery(req.Context(), testUUID, q, false)
}
data, err := h.config.PrometheusClientForQuery.QueryRange(req.Context(), url, &reqQuery)
if err != nil {
h.log.Error(ErrPrometheusQuery(err))
http.Error(w, ErrPrometheusQuery(err).Error(), http.StatusInternalServerError)
return
}
_, _ = w.Write(data)
}
// swagger:route GET /api/telemetry/metrics/static-board PrometheusAPI idGetPrometheusStaticBoard
// Handle GET request for Prometheus static board
//
// Used to fetch the static board
// responses:
// 200: prometheusStaticBoardRespWrapper
// PrometheusStaticBoardHandler returns the static board
func (h *Handler) PrometheusStaticBoardHandler(w http.ResponseWriter, req *http.Request, prefObj *models.Preference, _ *models.User, provider models.Provider) {
token, _ := req.Context().Value(models.TokenCtxKey).(string)
connectionID := uuid.FromStringOrNil(mux.Vars(req)["connectionID"])
connection, statusCode, err := provider.GetConnectionByID(token, connectionID, "prometheus")
if err != nil {
http.Error(w, err.Error(), statusCode)
return
}
url, _ := connection.Metadata["url"].(string)
result := map[string]*models.GrafanaBoard{}
resultLock := &sync.Mutex{}
resultWG := &sync.WaitGroup{}
boardFunc := map[string]func(context.Context, string) (*models.GrafanaBoard, error){
"cluster": h.config.PrometheusClient.GetClusterStaticBoard,
"node": h.config.PrometheusClient.GetNodesStaticBoard,
}
for key, bfunc := range boardFunc {
resultWG.Add(1)
go func(k string, bfun func(context.Context, string) (*models.GrafanaBoard, error)) {
defer resultWG.Done()
board, err := bfun(req.Context(), url)
if err != nil {
// error is already logged
return
}
resultLock.Lock()
defer resultLock.Unlock()
result[k] = board
}(key, bfunc)
}
resultWG.Wait()
if len(result) != len(boardFunc) {
h.log.Error(ErrStaticBoards)
http.Error(w, ErrStaticBoards.Error(), http.StatusInternalServerError)
return
}
err = json.NewEncoder(w).Encode(result)
if err != nil {
obj := "board instance"
h.log.Error(models.ErrMarshal(err, obj))
http.Error(w, models.ErrMarshal(err, obj).Error(), http.StatusInternalServerError)
return
}
}
// swagger:route POST /api/telemetry/metrics/boards/{connectionID} PrometheusAPI idPostPrometheusBoard
// Handle POST request for Prometheus board
//
// Used to persist selected board and panels
// responses:
// 200:
// SaveSelectedPrometheusBoardsHandler persists selected board and panels
func (h *Handler) SaveSelectedPrometheusBoardsHandler(w http.ResponseWriter, req *http.Request, prefObj *models.Preference, user *models.User, provider models.Provider) {
defer func() {
_ = req.Body.Close()
}()
body, err := io.ReadAll(req.Body)
if err != nil {
h.log.Error(ErrRequestBody(err))
http.Error(w, ErrRequestBody(err).Error(), http.StatusInternalServerError)
return
}
boards := []*models.SelectedGrafanaConfig{}
err = json.Unmarshal(body, &boards)
if err != nil {
obj := "request body"
h.log.Error(models.ErrUnmarshal(err, obj))
http.Error(w, models.ErrUnmarshal(err, obj).Error(), http.StatusBadRequest)
return
}
token, _ := req.Context().Value(models.TokenCtxKey).(string)
connectionID := uuid.FromStringOrNil(mux.Vars(req)["connectionID"])
connection, statusCode, err := provider.GetConnectionByID(token, connectionID, "prometheus")
if err != nil {
http.Error(w, err.Error(), statusCode)
return
}
if len(boards) > 0 {
connection.Metadata["prometheus_boards"] = boards
} else {
delete(connection.Metadata, "prometheus_boards")
}
updatedConnection, err := provider.UpdateConnection(req, connection)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
h.log.Debug("Board selection updated", updatedConnection.Metadata)
_, _ = w.Write([]byte("{}"))
}