Selecting specific lables from Prometheus metrics

88 views
Skip to first unread message

Hello World

unread,
May 10, 2021, 3:08:50 AM5/10/21
to Prometheus Users
Hi All,

Let us assume that we have some metrics like this,
up {instance="instance1", job="job1", namespace="namespace1", svc_name="first_service"}
up {instance="instance1", job="job2", namespace="namespace2", svc_name="second_service"}

is there a way to select specific labels from the server instead of getting all the labels in the result? like the svc_name, namespace.etc
something like this,

up{namespace="namespace1", svc_name="first_service"}
up{namespace="namespace2", svc_name="second_service"}

This is like selecting a couple of columns from a table containing 20 columns.

Thanks.

Hello World

unread,
May 10, 2021, 12:09:16 PM5/10/21
to Prometheus Users
This is not filtering the labels by values here I only want to select couple of labels in the results instead of getting all the labels over the wire.
Also, I'm not using this for visualization, I'm trying to write a backend API which fetches the data from prometheus and runs some business logic on the data.

Thanks for your time.

Matt Wilder

unread,
May 10, 2021, 3:33:22 PM5/10/21
to Hello World, Prometheus Users
You need to aggregate the data.  Most likely what you are looking for (given your example) is

sum by (namespace, svc_name) up

If sum is not the right aggregation, you can use any of the aggregation operators.

--
You received this message because you are subscribed to the Google Groups "Prometheus Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to prometheus-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/prometheus-users/5494d002-4f35-4799-971a-cb1e12a281c5n%40googlegroups.com.

This e-mail and all information in, attached to, or linked via this e-mail (together the ‘e-mail’) is confidential and may be legally privileged. It is intended solely for the intended addressee(s). Access to, or any onward transmission, of this e-mail by any other person is not authorised. If you are not the intended recipient, you are requested to immediately alert the sender of this e-mail and to immediately delete this e-mail. Any disclosure in any form of all or part of this e-mail, or of any the parties to it, including any copying, distribution or any action taken or omitted to be taken in reliance on it, is prohibited and may be unlawful. 


This e-mail is not, and is not intended to be, and should not be construed as being, (a) any offer, solicitation, or promotion of any kind; (b) the basis of any investment or other decision(s);  (c) any recommendation to buy, sell or transact in any manner any good(s), product(s) or service(s), nor engage in any investment(s) or other transaction(s) or activities;  or (d) the provision of, or related to, any advisory service(s) or activities, including regarding any investment, tax, legal, financial, accounting, consulting or any other related service(s).

Hello World

unread,
May 11, 2021, 1:57:57 PM5/11/21
to Prometheus Users
Hi Matt,
Thanks for the response.
Is there a way to select all the values without any aggregation?

Similar to what we do in database applications,
Movies
Name             Director                  Year                 IMDB_Rating
   X1                     Y1                      2020                       9.1
   X2                     Y2                      2021                       8.4
   X3                     Y3                      2019                       7.6

Now, an API is only interested in the Names and Years of all the titles, so, we just want to select Name and Year from the above table, and the result should look something like this
Name             Year
   X1               2020
   X2               2021
   X3               2019

is this possible with Prometheus? This would be very useful as we would transfer only the required fields over the network. 

Matt Wilder

unread,
May 14, 2021, 3:25:42 PM5/14/21
to Hello World, Prometheus Users
On Tue, May 11, 2021 at 10:58 AM Hello World <misc.he...@gmail.com> wrote:
Hi Matt,
Thanks for the response.
Is there a way to select all the values without any aggregation?

Similar to what we do in database applications,
Movies
Name             Director                  Year                 IMDB_Rating
   X1                     Y1                      2020                       9.1
   X2                     Y2                      2021                       8.4
   X3                     Y3                      2019                       7.6

Now, an API is only interested in the Names and Years of all the titles, so, we just want to select Name and Year from the above table, and the result should look something like this
Name             Year
   X1               2020
   X2               2021
   X3               2019

Given this example, sum by (Name, Year) will give you exactly what you want.  
You can't drop labels from a tsdb query without doing aggregation, because the query would lose information.
In the above example, what would happen if a movie had two directors?

The Prometheus TSDB is not a general purpose database like MySQL, it is specifically designed around storing numerical values with labels.
If all you care about is purely the values of the Name and Year labels, then you should use the labels and values APIs.

<prom host:port>/api/v1/label/Name/values
<prom host:port>/api/v1/label/Year/values
 
Reply all
Reply to author
Forward
0 new messages