forked from goodrain/rainbond
-
Notifications
You must be signed in to change notification settings - Fork 2
/
service_monitor.go
77 lines (70 loc) · 2.76 KB
/
service_monitor.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
package controller
import (
"net/http"
"github.com/wutong-paas/wutong/api/client/prometheus"
"github.com/go-chi/chi"
"github.com/wutong-paas/wutong/api/handler"
api_model "github.com/wutong-paas/wutong/api/model"
ctxutil "github.com/wutong-paas/wutong/api/util/ctx"
httputil "github.com/wutong-paas/wutong/util/http"
)
// AddServiceMonitors add service monitor
func (t *TenantEnvStruct) AddServiceMonitors(w http.ResponseWriter, r *http.Request) {
var add api_model.AddServiceMonitorRequestStruct
ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &add, nil)
if !ok {
return
}
serviceID := r.Context().Value(ctxutil.ContextKey("service_id")).(string)
tenantEnvID := r.Context().Value(ctxutil.ContextKey("tenant_env_id")).(string)
tsm, err := handler.GetServiceManager().AddServiceMonitor(tenantEnvID, serviceID, add)
if err != nil {
httputil.ReturnBcodeError(r, w, err)
return
}
httputil.ReturnSuccess(r, w, tsm)
}
// DeleteServiceMonitors delete service monitor
func (t *TenantEnvStruct) DeleteServiceMonitors(w http.ResponseWriter, r *http.Request) {
serviceID := r.Context().Value(ctxutil.ContextKey("service_id")).(string)
tenantEnvID := r.Context().Value(ctxutil.ContextKey("tenant_env_id")).(string)
name := chi.URLParam(r, "name")
tsm, err := handler.GetServiceManager().DeleteServiceMonitor(tenantEnvID, serviceID, name)
if err != nil {
httputil.ReturnBcodeError(r, w, err)
return
}
httputil.ReturnSuccess(r, w, tsm)
}
// UpdateServiceMonitors update service monitor
func (t *TenantEnvStruct) UpdateServiceMonitors(w http.ResponseWriter, r *http.Request) {
var update api_model.UpdateServiceMonitorRequestStruct
ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &update, nil)
if !ok {
return
}
name := chi.URLParam(r, "name")
serviceID := r.Context().Value(ctxutil.ContextKey("service_id")).(string)
tenantEnvID := r.Context().Value(ctxutil.ContextKey("tenant_env_id")).(string)
tsm, err := handler.GetServiceManager().UpdateServiceMonitor(tenantEnvID, serviceID, name, update)
if err != nil {
httputil.ReturnBcodeError(r, w, err)
return
}
httputil.ReturnSuccess(r, w, tsm)
}
// GetMonitorMetrics get monitor metrics
func GetMonitorMetrics(w http.ResponseWriter, r *http.Request) {
target := r.FormValue("target")
var metricMetadatas []prometheus.Metadata
if target == "tenant_env" {
metricMetadatas = handler.GetMonitorHandle().GetTenantEnvMonitorMetrics(r.FormValue("tenant_env"))
}
if target == "app" {
metricMetadatas = handler.GetMonitorHandle().GetAppMonitorMetrics(r.FormValue("tenant_env"), r.FormValue("app"))
}
if target == "component" {
metricMetadatas = handler.GetMonitorHandle().GetComponentMonitorMetrics(r.FormValue("tenant_env"), r.FormValue("component"))
}
httputil.ReturnSuccess(r, w, metricMetadatas)
}