-
Notifications
You must be signed in to change notification settings - Fork 3
/
admin.go
344 lines (329 loc) · 8.98 KB
/
admin.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
// Copyright 2022 Cockroach Labs Inc.
//
// 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,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package collection defines the sub command to run visus collection utilities.
package collection
import (
"bufio"
"bytes"
"errors"
"fmt"
"math"
"os"
"strings"
"time"
"github.com/cockroachlabs/visus/internal/collector"
"github.com/cockroachlabs/visus/internal/database"
"github.com/cockroachlabs/visus/internal/store"
"github.com/creasty/defaults"
"github.com/jackc/pgx/v5/pgtype"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/expfmt"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)
var (
databaseURL = ""
microsecondsPerSecond = int64(math.Pow10(6))
)
// metricDef yaml metric definition
type metricDef struct {
Name string
Kind string
Help string
}
// config yaml collection definition
type config struct {
Name string
Frequency int
MaxResults int
Enabled bool
Query string
Labels []string
Metrics []metricDef
Scope string `default:"node"`
}
func marshal(collection *store.Collection) ([]byte, error) {
metrics := make([]metricDef, 0)
for _, m := range collection.Metrics {
metric := metricDef{
Name: m.Name,
Kind: string(m.Kind),
Help: m.Help,
}
metrics = append(metrics, metric)
}
config := &config{
Name: collection.Name,
Frequency: int(collection.Frequency.Microseconds / microsecondsPerSecond),
MaxResults: collection.MaxResult,
Enabled: collection.Enabled,
Query: collection.Query,
Labels: collection.Labels,
Metrics: metrics,
Scope: string(collection.Scope),
}
return yaml.Marshal(config)
}
// listCmd list all the collections in the datababse
func listCmd() *cobra.Command {
c := &cobra.Command{
Use: "list",
Example: `./visus collection list --url "postgresql://root@localhost:26257/defaultdb?sslmode=disable" `,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
conn, err := database.New(ctx, databaseURL)
if err != nil {
return err
}
store := store.New(conn)
collections, err := store.GetCollectionNames(ctx)
if err != nil {
fmt.Print("Error retrieving collections")
return err
}
for _, coll := range collections {
fmt.Printf("%s\n", coll)
}
return nil
},
}
return c
}
func getCmd() *cobra.Command {
c := &cobra.Command{
Use: "get",
Args: cobra.ExactArgs(1),
Example: `./visus collection get collection_name --url "postgresql://root@localhost:26257/defaultdb?sslmode=disable" `,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
collectionName := args[0]
conn, err := database.New(ctx, databaseURL)
if err != nil {
return err
}
store := store.New(conn)
collection, err := store.GetCollection(ctx, collectionName)
if err != nil {
fmt.Printf("Error retrieving collection %s.", collectionName)
return err
}
if collection == nil {
fmt.Printf("Collection %s not found\n", collectionName)
} else {
res, err := marshal(collection)
if err != nil {
fmt.Printf("Unabled to marshall %s\n", collectionName)
}
fmt.Println(string(res))
}
return nil
},
}
return c
}
func testCmd() *cobra.Command {
var interval time.Duration
var count int
c := &cobra.Command{
Use: "test",
Args: cobra.ExactArgs(1),
Example: `./visus collection test collection_name --url "postgresql://root@localhost:26257/defaultdb?sslmode=disable" `,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
collectionName := args[0]
conn, err := database.ReadOnly(ctx, databaseURL)
if err != nil {
return err
}
st := store.New(conn)
coll, err := st.GetCollection(ctx, collectionName)
if err != nil {
fmt.Printf("Error retrieving collection %s.", collectionName)
return err
}
if coll == nil {
fmt.Printf("Collection %s not found\n", collectionName)
} else {
collector := collector.New(coll.Name, coll.Labels, coll.Query).
WithMaxResults(coll.MaxResult)
for _, m := range coll.Metrics {
switch m.Kind {
case store.Gauge:
collector.AddGauge(m.Name, m.Help)
case store.Counter:
collector.AddCounter(m.Name, m.Help)
}
}
for i := 1; i <= count || count == 0; i++ {
if i > 1 {
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(interval):
}
}
fmt.Printf("\n---- %s %s -----\n", time.Now().Format("01-02-2006 15:04:05"), coll.Name)
collector.Collect(ctx, conn)
gathering, err := prometheus.DefaultGatherer.Gather()
if err != nil {
fmt.Printf("Error collecting metrics %s.", err)
return err
}
for _, mf := range gathering {
if strings.HasPrefix(*mf.Name, collectionName) {
expfmt.MetricFamilyToText(os.Stdout, mf)
}
}
}
}
return nil
},
}
f := c.Flags()
f.DurationVar(&interval, "interval", 10*time.Second, "interval of collection")
f.IntVar(&count, "count", 1, "number of times to run the collection. Specify 0 for continuos collection")
return c
}
func deleteCmd() *cobra.Command {
c := &cobra.Command{
Use: "delete",
Args: cobra.ExactArgs(1),
Example: `./visus collection delete collection_name --url "postgresql://root@localhost:26257/defaultdb?sslmode=disable" `,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
collectionName := args[0]
conn, err := database.New(ctx, databaseURL)
if err != nil {
return err
}
store := store.New(conn)
err = store.DeleteCollection(ctx, args[0])
if err != nil {
fmt.Printf("Error deleting collection %s.\n", collectionName)
return err
}
fmt.Printf("Collection %s deleted.\n", collectionName)
return nil
},
}
return c
}
func putCmd() *cobra.Command {
var file string
c := &cobra.Command{
Use: "put",
Args: cobra.ExactArgs(0),
Example: `./visus collection put --yaml config.yaml --url "postgresql://root@localhost:26257/defaultdb?sslmode=disable" `,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
if file == "" {
return errors.New("yaml configuration required")
}
conn, err := database.New(ctx, databaseURL)
if err != nil {
return err
}
var data []byte
if file == "-" {
var buffer bytes.Buffer
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
buffer.Write(scanner.Bytes())
buffer.WriteString("\n")
}
if err := scanner.Err(); err != nil {
log.Errorf("reading standard input: %s", err.Error())
}
data = buffer.Bytes()
} else {
data, err = os.ReadFile(file)
if err != nil {
return err
}
}
config := &config{}
err = yaml.Unmarshal(data, &config)
if err != nil {
return err
}
if err := defaults.Set(config); err != nil {
return err
}
metrics := make([]store.Metric, 0)
for _, m := range config.Metrics {
metrics = append(metrics, store.Metric{
Name: m.Name,
Kind: store.Kind(m.Kind),
Help: m.Help,
})
}
var scope store.Scope
switch strings.ToLower(config.Scope) {
case "node":
scope = store.Node
case "cluster":
scope = store.Cluster
default:
return errors.New("invalid scope")
}
if config.Query == "" {
return errors.New("query must be specified")
}
if config.Name == "" {
return errors.New("name must be specified")
}
collection := &store.Collection{
Name: config.Name,
Enabled: config.Enabled,
Scope: scope,
MaxResult: config.MaxResults,
Frequency: pgtype.Interval{
Valid: true,
Microseconds: int64(config.Frequency) * microsecondsPerSecond,
},
Query: config.Query,
Labels: config.Labels,
Metrics: metrics,
}
store := store.New(conn)
err = store.PutCollection(ctx, collection)
if err != nil {
fmt.Printf("Error inserting collection %s.\n", config.Name)
return err
}
fmt.Printf("Collection %s inserted.\n", config.Name)
return nil
},
}
f := c.Flags()
f.StringVar(&file, "yaml", "", "file containing the configuration")
return c
}
// Command runs the collection tools to view and manage the configuration in the database.
func Command() *cobra.Command {
c := &cobra.Command{
Use: "collection",
}
f := c.PersistentFlags()
c.AddCommand(
getCmd(),
listCmd(),
deleteCmd(),
putCmd(),
testCmd())
f.StringVar(&databaseURL, "url", "",
"Connection URL, of the form: postgresql://[user[:passwd]@]host[:port]/[db][?parameters...]")
return c
}