go.ligato.io/vpp-agent/v3@v3.5.0/cmd/agentctl/commands/metrics.go (about) 1 // Copyright (c) 2019 Cisco and/or its affiliates. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at: 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package commands 16 17 import ( 18 "bytes" 19 "context" 20 "fmt" 21 "text/tabwriter" 22 23 "github.com/spf13/cobra" 24 25 "go.ligato.io/vpp-agent/v3/cmd/agentctl/api/types" 26 agentcli "go.ligato.io/vpp-agent/v3/cmd/agentctl/cli" 27 ) 28 29 func NewMetricsCommand(cli agentcli.Cli) *cobra.Command { 30 cmd := &cobra.Command{ 31 Use: "metrics", 32 Short: "Get runtime metrics", 33 } 34 cmd.AddCommand( 35 newMetricsListCommand(cli), 36 newMetricsGetCommand(cli), 37 ) 38 return cmd 39 } 40 41 func newMetricsListCommand(cli agentcli.Cli) *cobra.Command { 42 var opts MetricsListOptions 43 44 cmd := &cobra.Command{ 45 Use: "list [PATTERN]", 46 Aliases: []string{"list", "l"}, 47 Short: "List metrics", 48 Args: cobra.ArbitraryArgs, 49 RunE: func(cmd *cobra.Command, args []string) error { 50 opts.Refs = args 51 return runMetricsList(cli, opts) 52 }, 53 DisableFlagsInUseLine: true, 54 } 55 return cmd 56 } 57 58 type MetricsListOptions struct { 59 Refs []string 60 } 61 62 func runMetricsList(cli agentcli.Cli, opts MetricsListOptions) error { 63 ctx, cancel := context.WithCancel(context.Background()) 64 defer cancel() 65 66 allModels, err := cli.Client().ModelList(ctx, types.ModelListOptions{ 67 Class: "metrics", 68 }) 69 if err != nil { 70 return err 71 } 72 73 models := filterModelsByRefs(allModels, opts.Refs) 74 75 var buf bytes.Buffer 76 w := tabwriter.NewWriter(&buf, 0, 0, 2, ' ', 0) 77 fmt.Fprintf(w, "METRIC\tPROTO MESSAGE\t\n") 78 for _, model := range models { 79 fmt.Fprintf(w, "%s\t%s\t\n", model.Name, model.ProtoName) 80 } 81 if err := w.Flush(); err != nil { 82 return err 83 } 84 85 fmt.Fprint(cli.Out(), buf.String()) 86 return nil 87 } 88 89 func newMetricsGetCommand(cli agentcli.Cli) *cobra.Command { 90 var opts MetricsGetOptions 91 92 cmd := &cobra.Command{ 93 Use: "get METRIC", 94 Short: "Get metrics data", 95 Args: cobra.MinimumNArgs(1), 96 RunE: func(cmd *cobra.Command, args []string) error { 97 opts.Metrics = args 98 return runMetricsGet(cli, opts) 99 }, 100 } 101 return cmd 102 } 103 104 type MetricsGetOptions struct { 105 Metrics []string 106 } 107 108 func runMetricsGet(cli agentcli.Cli, opts MetricsGetOptions) error { 109 metric := opts.Metrics[0] 110 111 ctx, cancel := context.WithCancel(context.Background()) 112 defer cancel() 113 114 data, err := cli.Client().GetMetricData(ctx, metric) 115 if err != nil { 116 return err 117 } 118 119 if err := formatAsTemplate(cli.Out(), "json", data); err != nil { 120 return err 121 } 122 123 return nil 124 }