-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9034 from hashicorp/dmay-debug-metrics
Add metrics command / output to debug bundle
- Loading branch information
Showing
7 changed files
with
252 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package command | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/hashicorp/nomad/api" | ||
"github.com/posener/complete" | ||
) | ||
|
||
type OperatorMetricsCommand struct { | ||
Meta | ||
} | ||
|
||
func (c *OperatorMetricsCommand) Help() string { | ||
helpText := ` | ||
Usage: nomad operator metrics [options] | ||
Get Nomad metrics | ||
General Options: | ||
` + generalOptionsUsage() + ` | ||
Metrics Specific Options | ||
-pretty | ||
Pretty prints the JSON output | ||
-format <format> | ||
Specify output format (prometheus) | ||
` | ||
|
||
return strings.TrimSpace(helpText) | ||
} | ||
|
||
func (c *OperatorMetricsCommand) Synopsis() string { | ||
return "Retrieve Nomad metrics" | ||
} | ||
|
||
func (c *OperatorMetricsCommand) AutocompleteFlags() complete.Flags { | ||
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient), | ||
complete.Flags{ | ||
"-pretty": complete.PredictAnything, | ||
"-format": complete.PredictAnything, | ||
}) | ||
} | ||
|
||
func (c *OperatorMetricsCommand) Name() string { return "metrics" } | ||
|
||
func (c *OperatorMetricsCommand) Run(args []string) int { | ||
var pretty bool | ||
var format string | ||
|
||
flags := c.Meta.FlagSet(c.Name(), FlagSetClient) | ||
flags.Usage = func() { c.Ui.Output(c.Help()) } | ||
flags.BoolVar(&pretty, "pretty", false, "") | ||
flags.StringVar(&format, "format", "", "") | ||
|
||
if err := flags.Parse(args); err != nil { | ||
c.Ui.Error(fmt.Sprintf("Error parsing flags: %s", err)) | ||
return 1 | ||
} | ||
|
||
args = flags.Args() | ||
if l := len(args); l != 0 { | ||
c.Ui.Error("This command takes no arguments") | ||
c.Ui.Error(commandErrorText(c)) | ||
return 1 | ||
} | ||
|
||
client, err := c.Meta.Client() | ||
if err != nil { | ||
c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err)) | ||
return 1 | ||
} | ||
|
||
params := map[string]string{} | ||
|
||
if pretty { | ||
params["pretty"] = "1" | ||
} | ||
|
||
if len(format) > 0 { | ||
params["format"] = format | ||
} | ||
|
||
query := &api.QueryOptions{ | ||
Params: params, | ||
} | ||
|
||
bs, err := client.Operator().Metrics(query) | ||
if err != nil { | ||
c.Ui.Error(fmt.Sprintf("Error getting metrics: %v", err)) | ||
return 1 | ||
} | ||
|
||
resp := string(bs[:]) | ||
c.Ui.Output(resp) | ||
|
||
return 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package command | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/mitchellh/cli" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var _ cli.Command = &OperatorMetricsCommand{} | ||
|
||
func TestCommand_Metrics_Cases(t *testing.T) { | ||
t.Parallel() | ||
|
||
srv, _, url := testServer(t, false, nil) | ||
defer srv.Shutdown() | ||
|
||
ui := cli.NewMockUi() | ||
cmd := &OperatorMetricsCommand{Meta: Meta{Ui: ui}} | ||
|
||
cases := []struct { | ||
name string | ||
args []string | ||
expectedCode int | ||
expectedOutput string | ||
expectedError string | ||
}{ | ||
{ | ||
"pretty print json", | ||
[]string{"-address=" + url, "-pretty"}, | ||
0, | ||
"{", | ||
"", | ||
}, | ||
{ | ||
"prometheus format", | ||
[]string{"-address=" + url, "-format", "prometheus"}, | ||
0, | ||
"# HELP", | ||
"", | ||
}, | ||
{ | ||
"bad argument", | ||
[]string{"-address=" + url, "-foo", "bar"}, | ||
1, | ||
"Usage: nomad operator metrics", | ||
"flag provided but not defined: -foo", | ||
}, | ||
{ | ||
"bad address - no protocol", | ||
[]string{"-address=foo"}, | ||
1, | ||
"", | ||
"Error getting metrics: Get \"/v1/metrics\": unsupported protocol scheme", | ||
}, | ||
{ | ||
"bad address - fake host", | ||
[]string{"-address=http://foo"}, | ||
1, | ||
"", | ||
"no such host", | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
t.Run(c.name, func(t *testing.T) { | ||
code := cmd.Run(c.args) | ||
out := ui.OutputWriter.String() | ||
outerr := ui.ErrorWriter.String() | ||
|
||
require.Equalf(t, code, c.expectedCode, "expected exit code %d, got: %d: %s", c.expectedCode, code, outerr) | ||
require.Contains(t, out, c.expectedOutput, "expected output \"%s\", got \"%s\"", c.expectedOutput, out) | ||
require.Containsf(t, outerr, c.expectedError, "expected error \"%s\", got \"%s\"", c.expectedError, outerr) | ||
|
||
ui.OutputWriter.Reset() | ||
ui.ErrorWriter.Reset() | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
abfcb10
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: