github.com/hernad/nomad@v1.6.112/command/metrics.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package command
     5  
     6  import (
     7  	"fmt"
     8  	"strings"
     9  
    10  	"github.com/hernad/nomad/api"
    11  	"github.com/posener/complete"
    12  )
    13  
    14  type OperatorMetricsCommand struct {
    15  	Meta
    16  }
    17  
    18  func (c *OperatorMetricsCommand) Help() string {
    19  	helpText := `
    20  Usage: nomad operator metrics [options]
    21  
    22  Get Nomad metrics
    23  
    24  General Options:
    25  
    26    ` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace) + `
    27  
    28  Metrics Specific Options
    29  
    30    -pretty
    31      Pretty prints the JSON output
    32  
    33    -format <format>
    34      Specify output format (prometheus)
    35  
    36    -json
    37      Output the allocation in its JSON format.
    38  
    39    -t
    40      Format and display allocation using a Go template.
    41  
    42  `
    43  
    44  	return strings.TrimSpace(helpText)
    45  }
    46  
    47  func (c *OperatorMetricsCommand) Synopsis() string {
    48  	return "Retrieve Nomad metrics"
    49  }
    50  
    51  func (c *OperatorMetricsCommand) AutocompleteFlags() complete.Flags {
    52  	return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
    53  		complete.Flags{
    54  			"-pretty": complete.PredictAnything,
    55  			"-format": complete.PredictAnything,
    56  			"-json":   complete.PredictNothing,
    57  			"-t":      complete.PredictAnything,
    58  		})
    59  }
    60  
    61  func (c *OperatorMetricsCommand) Name() string { return "metrics" }
    62  
    63  func (c *OperatorMetricsCommand) Run(args []string) int {
    64  	var pretty, json bool
    65  	var format, tmpl string
    66  
    67  	flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
    68  	flags.Usage = func() { c.Ui.Output(c.Help()) }
    69  	flags.BoolVar(&pretty, "pretty", false, "")
    70  	flags.StringVar(&format, "format", "", "")
    71  	flags.BoolVar(&json, "json", false, "")
    72  	flags.StringVar(&tmpl, "t", "", "")
    73  
    74  	if err := flags.Parse(args); err != nil {
    75  		c.Ui.Error(fmt.Sprintf("Error parsing flags: %s", err))
    76  		return 1
    77  	}
    78  
    79  	args = flags.Args()
    80  	if l := len(args); l != 0 {
    81  		c.Ui.Error("This command takes no arguments")
    82  		c.Ui.Error(commandErrorText(c))
    83  		return 1
    84  	}
    85  
    86  	client, err := c.Meta.Client()
    87  	if err != nil {
    88  		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
    89  		return 1
    90  	}
    91  
    92  	params := map[string]string{}
    93  
    94  	if pretty {
    95  		params["pretty"] = "1"
    96  	}
    97  
    98  	if len(format) > 0 {
    99  		params["format"] = format
   100  	}
   101  
   102  	query := &api.QueryOptions{
   103  		Params: params,
   104  	}
   105  
   106  	if json || len(tmpl) > 0 {
   107  		metrics, _, err := client.Operator().MetricsSummary(query)
   108  		if err != nil {
   109  			c.Ui.Error(fmt.Sprintf("Error querying metrics: %v", err))
   110  			return 1
   111  		}
   112  
   113  		out, err := Format(json, tmpl, metrics)
   114  		if err != nil {
   115  			c.Ui.Error(err.Error())
   116  			return 1
   117  		}
   118  
   119  		c.Ui.Output(out)
   120  		return 0
   121  	} else {
   122  		bs, err := client.Operator().Metrics(query)
   123  		if err != nil {
   124  			c.Ui.Error(fmt.Sprintf("Error getting metrics: %v", err))
   125  			return 1
   126  		}
   127  
   128  		resp := string(bs)
   129  		c.Ui.Output(resp)
   130  	}
   131  
   132  	return 0
   133  }