-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
metrics_list.go
83 lines (69 loc) · 2.07 KB
/
metrics_list.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package cmd
import (
"fmt"
"os"
"regexp"
"strings"
"text/tabwriter"
"github.com/go-openapi/strfmt"
"github.com/spf13/cobra"
"github.com/cilium/cilium/api/v1/operator/client"
"github.com/cilium/cilium/api/v1/operator/models"
"github.com/cilium/cilium/operator/api"
"github.com/cilium/cilium/pkg/command"
)
var (
matchPattern string
operatorAddr string
)
// MetricsListCmd dumps all metrics into stdout
var MetricsListCmd = &cobra.Command{
Use: "list",
Short: "List all metrics for the operator",
Run: func(cmd *cobra.Command, args []string) {
c := client.NewHTTPClientWithConfig(
strfmt.Default, client.DefaultTransportConfig().WithHost(operatorAddr))
res, err := c.Metrics.GetMetrics(nil)
if err != nil {
log.Fatalf("Cannot get metrics list: %s", err)
}
re, err := regexp.Compile(matchPattern)
if err != nil {
log.Fatalf("Cannot compile regex: %s", err)
}
metrics := make([]*models.Metric, 0, len(res.Payload))
for _, metric := range res.Payload {
if re.MatchString(metric.Name) {
metrics = append(metrics, metric)
}
}
if command.OutputOption() {
if err := command.PrintOutput(metrics); err != nil {
os.Exit(1)
}
return
}
w := tabwriter.NewWriter(os.Stdout, 5, 0, 3, ' ', 0)
fmt.Fprintln(w, "Metric\tLabels\tValue")
for _, metric := range metrics {
label := ""
if len(metric.Labels) > 0 {
labelArray := []string{}
for key, value := range metric.Labels {
labelArray = append(labelArray, fmt.Sprintf(`%s="%s"`, key, value))
}
label = strings.Join(labelArray, " ")
}
fmt.Fprintf(w, "%s\t%s\t%f\n", metric.Name, label, metric.Value)
}
w.Flush()
},
}
func init() {
MetricsCmd.AddCommand(MetricsListCmd)
MetricsListCmd.Flags().StringVarP(&matchPattern, "match-pattern", "p", "", "Show only metrics whose names match matchpattern")
MetricsListCmd.Flags().StringVarP(&operatorAddr, "server-address", "s", api.OperatorAPIServeAddrDefault, "Address of the operator API server")
command.AddOutputOption(MetricsListCmd)
}