mirror of
https://github.com/prometheus/prometheus.git
synced 2025-03-05 20:59:13 -08:00
add query labels command to promtool (#4346)
Signed-off-by: Daisy T <daisyts@gmx.com>
This commit is contained in:
parent
afdb66dfac
commit
a3376e8f36
|
@ -89,6 +89,10 @@ func main() {
|
||||||
debugAllCmd := debugCmd.Command("all", "Fetch all debug information.")
|
debugAllCmd := debugCmd.Command("all", "Fetch all debug information.")
|
||||||
debugAllServer := debugAllCmd.Arg("server", "Prometheus server to get all debug information from.").Required().String()
|
debugAllServer := debugAllCmd.Arg("server", "Prometheus server to get all debug information from.").Required().String()
|
||||||
|
|
||||||
|
queryLabelsCmd := queryCmd.Command("labels", "Run labels query.")
|
||||||
|
queryLabelsServer := queryLabelsCmd.Arg("server", "Prometheus server to query.").Required().URL()
|
||||||
|
queryLabelsName := queryLabelsCmd.Arg("name", "Label name to provide label values for.").Required().String()
|
||||||
|
|
||||||
switch kingpin.MustParse(app.Parse(os.Args[1:])) {
|
switch kingpin.MustParse(app.Parse(os.Args[1:])) {
|
||||||
case checkConfigCmd.FullCommand():
|
case checkConfigCmd.FullCommand():
|
||||||
os.Exit(CheckConfig(*configFiles...))
|
os.Exit(CheckConfig(*configFiles...))
|
||||||
|
@ -119,6 +123,9 @@ func main() {
|
||||||
|
|
||||||
case debugAllCmd.FullCommand():
|
case debugAllCmd.FullCommand():
|
||||||
os.Exit(debugAll(*debugAllServer))
|
os.Exit(debugAll(*debugAllServer))
|
||||||
|
|
||||||
|
case queryLabelsCmd.FullCommand():
|
||||||
|
os.Exit(QueryLabels(*queryLabelsServer, *queryLabelsName))
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -519,6 +526,36 @@ func QuerySeries(url *url.URL, matchers []string, start string, end string) int
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// QueryLabels queries for label values against a Prometheus server.
|
||||||
|
func QueryLabels(url *url.URL, name string) int {
|
||||||
|
config := api.Config{
|
||||||
|
Address: url.String(),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create new client.
|
||||||
|
c, err := api.NewClient(config)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, "error creating API client:", err)
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run query against client.
|
||||||
|
api := v1.NewAPI(c)
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
|
||||||
|
val, err := api.LabelValues(ctx, name)
|
||||||
|
cancel()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, "query error:", err)
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, v := range val {
|
||||||
|
fmt.Println(v)
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
func parseTime(s string) (time.Time, error) {
|
func parseTime(s string) (time.Time, error) {
|
||||||
if t, err := strconv.ParseFloat(s, 64); err == nil {
|
if t, err := strconv.ParseFloat(s, 64); err == nil {
|
||||||
s, ns := math.Modf(t)
|
s, ns := math.Modf(t)
|
||||||
|
|
Loading…
Reference in a new issue