Sum of different Time Series for common lable

19 views
Skip to first unread message

SHRIKRISHNA KHUPASANGIKAR

unread,
Apr 19, 2020, 7:58:48 AM4/19/20
to Prometheus Users

I have two counters like below . Both counter have common label interface_name.

Counter A= {app_kubernetes_io_component="xyz-rest-ep",interface_name="N7",command="create","job="kubernetes-pods",monitor="prometheus",namespace="xyz-foo",pod="xyz-rest-ep-9f5686bc-dvtjr",pod_template_hash="9f5686bc",}



Counter B= {app_kubernetes_io_component="xyz-rest-ep",interface_name="N7",command="delete",job="kubernetes-pods",monitor="prometheus",namespace="xyz-foo",pod="xyz-rest-ep-9f5686bc-dvtjr",pod_template_hash="9f5686bc",target_base_url="http://192.168.102.50:7044/"}



I would like to have addition of counter A and counter B by interface_name lable.

Note that Counter B have extra label as target_base_url which is absent in Counter A

Kindly let us know how to achieve this in Prometheus query.

Stuart Clark

unread,
Apr 19, 2020, 8:15:15 AM4/19/20
to SHRIKRISHNA KHUPASANGIKAR, Prometheus Users

You can add them if they have the same labels, so use an aggregation operator (e.g. sum()) with "by" or "without".

-- 
Stuart Clark

SHRIKRISHNA KHUPASANGIKAR

unread,
Apr 19, 2020, 8:54:44 AM4/19/20
to Prometheus Users
Thanks Stuart !
I am aggregating Counter A by interface_name and in same way Counter B. Each give me correct output. But when i do Counter A + Counter B nothing is coming.

Query.JPG

Brian Candler

unread,
Apr 19, 2020, 9:15:32 AM4/19/20
to Prometheus Users
You are adding different *metrics*?? Usually this is the wrong thing to do, because different metrics have different meanings, and adding different types of things generally makes no sense.  This might mean you want a single metric with more labels to distinguish them.

However in the case of sum by, you're in luck.  The output of sum by is an instance vector with no metric name, so you can combine them directly.  For example:

sum by (instance) (node_network_receive_bytes_total) + sum by (instance) (node_network_transmit_bytes_total)

works just fine.  Just make sure that the LH and RH have the same set of labels, which in this case is just the (instance) label that has been grouped on.

If you are desperate, there are ugly frigs to combine different metrics directly, but I strongly discourage this sort of abuse:

sum by (instance) ({__name__=~"node_network_receive_bytes_total|node_network_transmit_bytes_total"})

label_replace(up, "__name__", "down","__name__","(.*)")

Reply all
Reply to author
Forward
0 new messages