Skip to content

Commit

Permalink
Merge pull request #561 from fujiwara/metric-names
Browse files Browse the repository at this point in the history
Add metric-names subcommand.
  • Loading branch information
yseto authored May 16, 2023
2 parents 2b370ee + d363c7d commit ee2bd4a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
2 changes: 2 additions & 0 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/mackerelio/mkr/checks"
"github.com/mackerelio/mkr/dashboards"
"github.com/mackerelio/mkr/hosts"
"github.com/mackerelio/mkr/metric_names"
"github.com/mackerelio/mkr/metrics"
"github.com/mackerelio/mkr/monitors"
"github.com/mackerelio/mkr/org"
Expand Down Expand Up @@ -39,4 +40,5 @@ var Commands = []cli.Command{
checks.Command,
wrap.Command,
aws_integrations.Command,
metric_names.Command,
}
53 changes: 53 additions & 0 deletions metric_names/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package metric_names

import (
"os"

"github.com/mackerelio/mkr/format"
"github.com/mackerelio/mkr/jq"
"github.com/mackerelio/mkr/logger"
"github.com/mackerelio/mkr/mackerelclient"
"github.com/urfave/cli"
)

var Command = cli.Command{
Name: "metric-names",
Usage: "Fetch metric names",
ArgsUsage: "[--host | -H <hostId>] [--service | -s <service>] [--jq <formula>]",
Description: `
Fetch metric names of 'host metric' or 'service metric'.
Requests "/api/v0/hosts/<hostId>/metric-names" or "/api/v0/services/<serviceName>/metric-names".
See https://mackerel.io/api-docs/entry/hosts#metric-names, https://mackerel.io/api-docs/entry/services#metric-names
`,
Action: doMetricNames,
Flags: []cli.Flag{
cli.StringFlag{Name: "host, H", Value: "", Usage: "Fetch host metric names of <hostID>."},
cli.StringFlag{Name: "service, s", Value: "", Usage: "Fetch service metric names of <service>."},
jq.CommandLineFlag,
},
}

func doMetricNames(c *cli.Context) error {
optHostID := c.String("host")
optService := c.String("service")
jq := c.String("jq")

client := mackerelclient.NewFromContext(c)

if optHostID != "" {
metricNames, err := client.ListHostMetricNames(optHostID)
logger.DieIf(err)

err = format.PrettyPrintJSON(os.Stdout, metricNames, jq)
logger.DieIf(err)
} else if optService != "" {
metricNames, err := client.ListServiceMetricNames(optService)
logger.DieIf(err)

err = format.PrettyPrintJSON(os.Stdout, metricNames, jq)
logger.DieIf(err)
} else {
cli.ShowCommandHelpAndExit(c, "metric-names", 1)
}
return nil
}

0 comments on commit ee2bd4a

Please sign in to comment.