-
Notifications
You must be signed in to change notification settings - Fork 1
/
monitor.go
192 lines (154 loc) · 4.46 KB
/
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
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
package service
import (
"context"
"encoding/json"
"github.com/samsarahq/thunder/graphql"
"github.com/samsarahq/thunder/graphql/schemabuilder"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
type MonitorService struct {
Address string
App string
Cluster string
DashboardAddr string
}
func NewMonitorService(addr, app, cluster, dashboardAddr string) *MonitorService {
return &MonitorService{
Address: addr,
App: app,
Cluster: cluster,
DashboardAddr: dashboardAddr,
}
}
func (fs *MonitorService) empty(ctx context.Context, args struct {
Empty bool
}) bool {
return args.Empty
}
func (fs *MonitorService) Dashboard(ctx context.Context, args struct{}) string {
return fs.DashboardAddr
}
func (ms *MonitorService) RangeQuery(ctx context.Context, args struct {
Query string
Start uint32
End uint32
Step uint32
}) (string, error) {
args.Query = strings.ReplaceAll(args.Query, "$app", ms.App)
args.Query = strings.ReplaceAll(args.Query, "$cluster", ms.Cluster)
param := url.Values{}
param.Set("query", args.Query)
param.Set("start", strconv.Itoa(int(args.Start)))
param.Set("end", strconv.Itoa(int(args.End)))
param.Set("step", strconv.Itoa(int(args.Step)))
resp, err := http.DefaultClient.Get(ms.Address + "/api/v1/query_range?" + param.Encode())
if err != nil {
return "", err
}
b, err := ioutil.ReadAll(resp.Body)
return string(b), nil
}
func (ms *MonitorService) RangeQueryURL(ctx context.Context, args struct {
Query string
Start uint32
End uint32
Step uint32
}) (string, error) {
_, _, err := permissions(ctx, ADMIN)
if err != nil {
return "", err
}
args.Query = strings.ReplaceAll(args.Query, "$app", ms.App)
args.Query = strings.ReplaceAll(args.Query, "$cluster", ms.Cluster)
param := url.Values{}
param.Set("query", args.Query)
param.Set("start", strconv.Itoa(int(args.Start)))
param.Set("end", strconv.Itoa(int(args.End)))
param.Set("step", strconv.Itoa(int(args.Step)))
return ms.Address + "/api/v1/query_range?" + param.Encode(), nil
}
func (ms *MonitorService) Query(ctx context.Context, args struct {
Query string
}) (string, error) {
_, _, err := permissions(ctx, ADMIN)
if err != nil {
return "", err
}
args.Query = strings.ReplaceAll(args.Query, "$app", ms.App)
args.Query = strings.ReplaceAll(args.Query, "$cluster", ms.Cluster)
param := url.Values{}
param.Set("query", args.Query)
resp, err := http.DefaultClient.Get(ms.Address + "/api/v1/query?" + param.Encode())
if err != nil {
return "", err
}
b, err := ioutil.ReadAll(resp.Body)
return string(b), nil
}
type FuseClient struct {
Name string `json:"__name__"`
App string `json:"app"`
Cluster string `json:"cluster"`
Hostip string `json:"hostip"`
Instance string `json:"instance"`
Job string `json:"job"`
Monitor string `json:"monitor"`
Role string `json:"role"`
Service string `json:"service"`
}
type FuseRecord struct {
Metric FuseClient `json:"metric"`
Value float64 `json:"value"`
}
func (ms *MonitorService) FuseClientList(ctx context.Context, args struct{}) ([]*FuseRecord, error) {
_, _, err := permissions(ctx, ADMIN)
if err != nil {
return nil, err
}
query := `up{app="$app", role="fuseclient", cluster="$cluster"}`
query = strings.ReplaceAll(query, "$app", ms.App)
query = strings.ReplaceAll(query, "$cluster", ms.Cluster)
param := url.Values{}
param.Set("query", query)
param.Set("time", strconv.Itoa(int(time.Now().Unix())))
resp, err := http.DefaultClient.Get(ms.Address + "/api/v1/query?" + param.Encode())
if err != nil {
return nil, err
}
b, err := ioutil.ReadAll(resp.Body)
result := struct {
Data struct {
Result []*struct {
Metric FuseClient `json:"metric"`
Value []interface{} `json:"value"`
} `json:"result"`
} `json:"data"`
}{}
if err := json.Unmarshal(b, &result); err != nil {
return nil, err
}
frs := make([]*FuseRecord, 0, len(result.Data.Result))
for _, r := range result.Data.Result {
frs = append(frs, &FuseRecord{
Metric: r.Metric,
Value: r.Value[0].(float64),
})
}
return frs, nil
}
func (ms *MonitorService) Schema() *graphql.Schema {
schema := schemabuilder.NewSchema()
query := schema.Query()
query.FieldFunc("Query", ms.Query)
query.FieldFunc("RangeQuery", ms.RangeQuery)
query.FieldFunc("RangeQueryURL", ms.RangeQueryURL)
query.FieldFunc("_empty", ms.empty)
query.FieldFunc("Dashboard", ms.Dashboard)
query.FieldFunc("FuseClientList", ms.FuseClientList)
return schema.MustBuild()
}