-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathgrafana.go
262 lines (237 loc) · 9.31 KB
/
grafana.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
// Copyright 2020 MatrixOrigin.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package grafana
import (
"context"
"net/http"
"github.com/K-Phoen/grabana"
"github.com/K-Phoen/grabana/axis"
"github.com/K-Phoen/grabana/graph"
"github.com/K-Phoen/grabana/row"
"github.com/K-Phoen/grabana/singlestat"
"github.com/K-Phoen/grabana/table"
"github.com/K-Phoen/grabana/target/prometheus"
"github.com/K-Phoen/grabana/variable/interval"
)
var (
folderName = "Matrixcube"
)
// DashboardCreator matrixcube grafana dashboard creator
type DashboardCreator struct {
cli *grabana.Client
dataSource string
}
// NewDashboardCreator returns a dashboard creator
// eyJrIjoiQU5nZWJNOWNha0JJRDdQTTFWSnVSanFBMkhscGMyaksiLCJuIjoiYXBpa2V5MDAxIiwiaWQiOjF9
func NewDashboardCreator(grafana, apiKey, dataSource string) *DashboardCreator {
return &DashboardCreator{
cli: grabana.NewClient(http.DefaultClient, grafana, apiKey),
dataSource: dataSource,
}
}
// Create create dashboard
func (c *DashboardCreator) Create() error {
folder, err := c.createFolder()
if err != nil {
return err
}
return c.createRaftDashboard(folder)
}
func (c *DashboardCreator) createFolder() (*grabana.Folder, error) {
folder, err := c.cli.GetFolderByTitle(context.Background(), folderName)
if err != nil && err != grabana.ErrFolderNotFound {
return nil, err
}
if folder == nil {
folder, err = c.cli.CreateFolder(context.Background(), folderName)
if err != nil {
return nil, err
}
}
return folder, nil
}
func (c *DashboardCreator) createRaftDashboard(folder *grabana.Folder) error {
db := grabana.NewDashboardBuilder("Raftstore Status",
grabana.AutoRefresh("5s"),
grabana.Tags([]string{"generated"}),
grabana.VariableAsInterval(
"interval",
interval.Values([]string{"30s", "1m", "5m", "10m", "30m", "1h", "6h", "12h"}),
),
c.storageRow(),
c.requestRow(),
c.shardsRow(),
c.raftLogRow(),
c.raftInternalRow(),
c.promgramInternalRow())
_, err := c.cli.UpsertDashboard(context.Background(), folder, db)
return err
}
func (c *DashboardCreator) raftLogRow() grabana.DashboardBuilderOption {
return grabana.Row(
"Raft log status",
c.withGraph("50% raft log append time", 4,
`histogram_quantile(0.50, sum(rate(matrixcube_raftstore_raft_log_append_duration_seconds_bucket[$interval])) by (le, instance))`,
"{{ instance }}", axis.Unit("s"), axis.Min(0)),
c.withGraph("99% raft log append time", 4,
`histogram_quantile(0.99, sum(rate(matrixcube_raftstore_raft_log_append_duration_seconds_bucket[$interval])) by (le, instance))`,
"{{ instance }}", axis.Unit("s"), axis.Min(0)),
c.withGraph("99.99% raft log append time", 4,
`histogram_quantile(0.9999, sum(rate(matrixcube_raftstore_raft_log_append_duration_seconds_bucket[$interval])) by (le, instance))`,
"{{ instance }}", axis.Unit("s"), axis.Min(0)),
c.withGraph("50% raft log applied time", 4,
`histogram_quantile(0.50, sum(rate(matrixcube_raftstore_raft_log_apply_duration_seconds_bucket[$interval])) by (le, instance))`,
"{{ instance }}", axis.Unit("s"), axis.Min(0)),
c.withGraph("99% raft log applied time", 4,
`histogram_quantile(0.99, sum(rate(matrixcube_raftstore_raft_log_apply_duration_seconds_bucket[$interval])) by (le, instance))`,
"{{ instance }}", axis.Unit("s"), axis.Min(0)),
c.withGraph("99.99% raft log applied time", 4,
`histogram_quantile(0.9999, sum(rate(matrixcube_raftstore_raft_log_apply_duration_seconds_bucket[$interval])) by (le, instance))`,
"{{ instance }}", axis.Unit("s"), axis.Min(0)),
c.withGraph("50% raft log size", 4,
`histogram_quantile(0.50, sum(rate(matrixcube_raftstore_raft_proposal_log_bytes_bucket[$interval])) by (le, instance))`,
"{{ instance }}", axis.Unit("bytes"), axis.Min(0)),
c.withGraph("99% raft log size", 4,
`histogram_quantile(0.99, sum(rate(matrixcube_raftstore_raft_proposal_log_bytes_bucket[$interval])) by (le, instance))`,
"{{ instance }}", axis.Unit("bytes"), axis.Min(0)),
c.withGraph("99.99% raft log size", 4,
`histogram_quantile(0.9999, sum(rate(matrixcube_raftstore_raft_proposal_log_bytes_bucket[$interval])) by (le, instance))`,
"{{ instance }}", axis.Unit("bytes"), axis.Min(0)),
c.withGraph("50% raft snapshot size", 4,
`histogram_quantile(0.50, sum(rate(matrixcube_raftstore_snapshot_size_bytes_bucket[$interval])) by (le, instance))`,
"{{ instance }}", axis.Unit("bytes"), axis.Min(0)),
c.withGraph("99% raft snapshot size", 4,
`histogram_quantile(0.99, sum(rate(matrixcube_raftstore_snapshot_size_bytes_bucket[$interval])) by (le, instance))`,
"{{ instance }}", axis.Unit("bytes"), axis.Min(0)),
c.withGraph("99.99% raft snapshot size", 4,
`histogram_quantile(0.9999, sum(rate(matrixcube_raftstore_snapshot_size_bytes_bucket[$interval])) by (le, instance))`,
"{{ instance }}", axis.Unit("bytes"), axis.Min(0)),
c.withGraph("50% raft snapshot build time", 4,
`histogram_quantile(0.50, sum(rate(matrixcube_raftstore_snapshot_building_duration_seconds_bucket[$interval])) by (le, instance))`,
"{{ instance }}", axis.Unit("s"), axis.Min(0)),
c.withGraph("99% raft snapshot build time", 4,
`histogram_quantile(0.99, sum(rate(matrixcube_raftstore_snapshot_building_duration_seconds_bucket[$interval])) by (le, instance))`,
"{{ instance }}", axis.Unit("s"), axis.Min(0)),
c.withGraph("99.99% raft snapshot build time", 4,
`histogram_quantile(0.9999, sum(rate(matrixcube_raftstore_snapshot_building_duration_seconds_bucket[$interval])) by (le, instance))`,
"{{ instance }}", axis.Unit("s"), axis.Min(0)),
)
}
func (c *DashboardCreator) raftInternalRow() grabana.DashboardBuilderOption {
return grabana.Row(
"Raft internal status",
c.withGraph("Raft normal commands", 3,
"sum(rate(matrixcube_raftstore_command_normal_total[$interval])) by (type)",
"{{ type }}"),
c.withGraph("Raft admin commands", 3,
"sum(rate(matrixcube_raftstore_command_admin_total[$interval])) by (type, status)",
"{{ type }}({{ status }})"),
c.withGraph("Handled raft event", 3,
"sum(rate(matrixcube_raftstore_raft_ready_handled_total[$interval])) by (type)",
"{{ type }}"),
c.withGraph("Sended raft messages", 3,
"sum(rate(matrixcube_raftstore_raft_sent_msg_total[$interval])) by (type)",
"{{ type }}"),
)
}
func (c *DashboardCreator) shardsRow() grabana.DashboardBuilderOption {
return grabana.Row(
"Shards status",
c.withTable("Shards count overview", 4,
"sum(matrixcube_raftstore_store_shard_total) by (type)",
"{{ type }}"),
c.withTable("Shards count per node", 4,
`sum(matrixcube_raftstore_store_shard_total{type="shards"}) by (instance)`,
"{{ instance }}"),
c.withTable("Leader count per node", 4,
`sum(matrixcube_raftstore_store_shard_total{type="leader"}) by (instance)`,
"{{ instance }}"),
)
}
func (c *DashboardCreator) promgramInternalRow() grabana.DashboardBuilderOption {
return grabana.Row(
"Promgram internal status",
c.withGraph("Queue", 6,
"sum(matrixcube_raftstore_queue_size) by (type)",
"{{ type }}"),
c.withGraph("Batching", 6,
"sum(matrixcube_raftstore_batch_size) by (type)",
"{{ type }}"),
)
}
func (c *DashboardCreator) requestRow() grabana.DashboardBuilderOption {
return grabana.Row(
"Request status",
c.withGraph("Requests received", 12,
"sum(rate(matrixcube_raftstore_command_normal_total[$interval]))",
"All requests"),
)
}
func (c *DashboardCreator) storageRow() grabana.DashboardBuilderOption {
return grabana.Row(
"Overview status",
row.WithSingleStat(
"Storage Total",
singlestat.Height("200px"),
singlestat.Span(4),
singlestat.WithPrometheusTarget(
"sum(matrixcube_raftstore_store_storage_bytes{type='total'})"),
singlestat.Unit("bytes"),
),
row.WithSingleStat(
"Storage Used",
singlestat.Height("200px"),
singlestat.Span(4),
singlestat.WithPrometheusTarget(
"sum(matrixcube_raftstore_store_storage_bytes{type='total'})-sum(matrixcube_raftstore_store_storage_bytes{type='free'})"),
singlestat.Unit("bytes"),
),
row.WithSingleStat(
"Storage Available",
singlestat.Height("200px"),
singlestat.Span(4),
singlestat.WithPrometheusTarget(
"sum(matrixcube_raftstore_store_storage_bytes{type='free'})"),
singlestat.Unit("bytes"),
),
)
}
func (c *DashboardCreator) withGraph(title string, span float32, pql string, legend string, opts ...axis.Option) row.Option {
return row.WithGraph(
title,
graph.Span(span),
graph.Height("400px"),
graph.DataSource(c.dataSource),
graph.WithPrometheusTarget(
pql,
prometheus.Legend(legend),
),
graph.LeftYAxis(opts...),
)
}
func (c *DashboardCreator) withTable(title string, span float32, pql string, legend string) row.Option {
return row.WithTable(
title,
table.Span(span),
table.Height("400px"),
table.DataSource(c.dataSource),
table.WithPrometheusTarget(
pql,
prometheus.Legend(legend)),
table.AsTimeSeriesAggregations([]table.Aggregation{
{Label: "Current", Type: table.Current},
{Label: "Max", Type: table.Max},
{Label: "Min", Type: table.Min},
}),
)
}