Question: how to get the value of a given label of a metric?

20 views
Skip to first unread message

吴雷

unread,
Sep 29, 2020, 5:03:46 AM9/29/20
to Prometheus Users
  Hi, all!
I've get a metric through some experssions, for example, the expression is discovering_paas_clusters{cluster_id="XXX",exporter_replica="XXX"} ,
and the metric i get is
discovering_paas_clusters{cluster_id="XXX",cluster_status="Hibernation",exporter_replica="XXX"}=1.
Now what i want to get is the value of the label "cluster_status". How can it achieve the goal?
Thank you!  

Brian Candler

unread,
Sep 29, 2020, 6:46:04 AM9/29/20
to Prometheus Users
Remember that a PromQL query in general gives more than one timeseries in its result, and each timeseries will have a distinct set of labels.

Labels are strings, and the label's value is already there in the results.  What exactly do you want to do with the label value(s)?

* If you are sending a PromQL query via the API, then you'll get a JSON object as the response, so you just extract the values of interest directly.

* If you want to count the number of metrics with each value of a given label, then you can use PromQL:  count by (cluster_status) (... rest of query ...)

* If you're using Grafana then you can use labels in graph legends, e.g.  Cluster: {{instance}} {{cluster_status}}
and you can set variables based on label values, e.g. label_values(discovering_paas_clusters, cluster_status)

Reply all
Reply to author
Forward
0 new messages