add two metric values PromQL

667 views
Skip to first unread message

Roland V

unread,
Mar 8, 2020, 4:06:37 PM3/8/20
to Prometheus Users
Hello Good People,

I'm working on some reporting and am in the process of calculating the summary of two metrics: virtual_machine_instance_up & alertmanager_silence_info
Both produce Gauge values (0 or 1). 

I tried to find a way to add these values together and present the output as a new metric to my users.

I'm able to cherry pick some of the entries based on the matchers I want them to be joined on:
(sum(virtual_machine_instance_up{resource_name="fake-vm-02"}) + sum(alertmanager_silence_info{resource_name="fake-vm-02"}))

This works great. However, I'm looking to find a way to list all values from virtual_machine_instance_up, and add either the "matching" vector of alertmanager_silence_info or simply 0 if there's no match.

What I tried next is to have the same dimensional labels for both metrics, and try to add them that way:
sum by(resource_group, resource_name) (
  virtual_machine_instance_up
)
+
sum by(resource_group, resource_name) (
  alertmanager_silence_info
)

Which again works great, but the results aren't exactly what I hoped for. Only those vectors are listed from virtual_machine_instance_up, which have a matching vector in alertmanager_silence_info.

Next I tried incorporating "vector(0)" in the above query, which is my goto "metric" for cases where the underlying metric doesn't produce any value. Sadly this didn't help achieve what I was looking for either:
sum by(resource_group, resource_name) (
  virtual_machine_instance_up
)
+
(sum by(resource_group, resource_name) (
  alertmanager_silence_info
)) OR vector(0)

I wonder if you can recommend a way to improve my last query, or if I should take another approach.

For those curious: the metrics were taken from exporters azure-resources-exporter & alertmanager-silences-exporter.

Thanks ahead for your replies,
Roland

Julien Pivotto

unread,
Mar 8, 2020, 4:10:27 PM3/8/20
to Roland V, Prometheus Users
On 08 Mar 13:06, Roland V wrote:
> Hello Good People,
>
> I'm working on some reporting and am in the process of calculating the
> summary of two metrics: *virtual_machine_instance_up* &
> *alertmanager_silence_info*.
> Both produce Gauge values (0 or 1).
>
> I tried to find a way to add these values together and present the output
> as a new metric to my users.
>
> I'm able to cherry pick some of the entries based on the matchers I want
> them to be joined on:
> (sum(virtual_machine_instance_up{resource_name="fake-vm-02"}) + sum(
> alertmanager_silence_info{resource_name="fake-vm-02"}))
>
> This works great. However, I'm looking to find a way to list all values
> from *virtual_machine_instance_up*, and add either the "matching" vector of
> *alertmanager_silence_info* or simply 0 if there's no match.
>
> What I tried next is to have the same dimensional labels for both metrics,
> and try to add them that way:
> sum by(resource_group, resource_name) (
> virtual_machine_instance_up
> )
> +
> sum by(resource_group, resource_name) (
> alertmanager_silence_info
> )
>

I would recommend:

virtual_machine_instance_up
+ alertmanager_silence_info
or virtual_machine_instance_up


If labels don't match:

sum by(resource_group, resource_name) (
virtual_machine_instance_up
)
+
sum by(resource_group, resource_name) (
alertmanager_silence_info
)
or
sum by(resource_group, resource_name) (
virtual_machine_instance_up
)


using "or" seems to be what you need



> Which again works great, but the results aren't exactly what I hoped for.
> Only those vectors are listed from *virtual_machine_instance_up*, which
> have a matching vector in *alertmanager_silence_info*.
>
> Next I tried incorporating "vector(0)" in the above query, which is my goto
> "metric" for cases where the underlying metric doesn't produce any value.
> Sadly this didn't help achieve what I was looking for either:
> sum by(resource_group, resource_name) (
> virtual_machine_instance_up
> )
> +
> (sum by(resource_group, resource_name) (
> alertmanager_silence_info
> )) OR vector(0)
>
> I wonder if you can recommend a way to improve my last query, or if I
> should take another approach.
>
> For those curious: the metrics were taken from exporters
> azure-resources-exporter
> <https://github.com/FXinnovation/azure-resources-exporter> &
> alertmanager-silences-exporter
> <https://github.com/FXinnovation/alertmanager-silences-exporter>.
>
> Thanks ahead for your replies,
> Roland
>
> --
> 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/e15f320b-61b1-4799-855a-c90369c31ba4%40googlegroups.com.


--
(o- Julien Pivotto
//\ Open-Source Consultant
V_/_ Inuits - https://www.inuits.eu
signature.asc

Roland V

unread,
Mar 8, 2020, 4:20:34 PM3/8/20
to Prometheus Users
Hello Julien,

Thanks a lot that seemed to do the trick.

Have a great rest of the weekend!

Roland
> To unsubscribe from this group and stop receiving emails from it, send an email to promethe...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages