Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
868 views
in Technique[技术] by (71.8m points)

promql - Prometheus: How can I multiply a vector by the average of another vector

Situation

I have a counter with values from a power meter (KWh since it was installed). On my Grafana dashboard I like to show how much money was spend on electric power during the given time range.

Doing this with a fixed price per KWh is easy: increase(loxone_values{control="Stromverbrauch Z?hler"}[$__range]) * 0.22

I can show the last value with a "Stat" in Grafana and get the amount of money spend.

Problem

Obviously this fails if the price per KWh changes, which is does every hour in my case. I have a second metric that holds the current price. I like to multiply the KWh by the average price.

I think I can do this with group_left but failed to construct a working query.

My first approach increase(loxone_values{control="Stromverbrauch Z?hler"}[$__range]) * group_left avg(loxone_values{control="Strompreis aktuell"}) which gives a parse error.

It looks like group_left needs something to match the vectors. But there is no common tag (beside instance but that might change in the future).

Anyway I gave that a try but increase(loxone_values{control="Stromverbrauch Z?hler"}[$__range]) * on (instance) group_left avg(loxone_values{control="Strompreis aktuell"}) gives me an empty result.

question from:https://stackoverflow.com/questions/65651811/prometheus-how-can-i-multiply-a-vector-by-the-average-of-another-vector

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This should do it:

increase(loxone_values{control="Stromverbrauch Z?hler"}[$__range]) * scalar(avg(loxone_values{control="Strompreis aktuell"}))

Explanation:

You can multiply an instant vector by an instant vector with exactly the same labels and label values or by a scalar. The result(s) of increase(loxone_values{control="Stromverbrauch Z?hler"}[$__range]) must have labels, whereas avg(loxone_values{control="Strompreis aktuell"}) will return one instant vector with no labels. Using scalar converts the instant vector to a scalar and all results of increase(loxone_values{control="Stromverbrauch Z?hler"}[$__range]) will be multiplied by that, irrelevant of their labels.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...