If you want to do this inside prometheus, then you want
recording rules.
Note that with recording rules you're not really creating a new "variable", you're creating a whole new timeseries. However this is what you need, since your second query wants to perform aggregation_over_time() on these precomputed values.
It'll take a week until you have enough data from your recording rule to use, unless you
backfill.
Your other option is to substitute the first expression into the two places where you want to use it in the second expression. But in this case, you'll need to use a
subquery, e.g.
(some - long - expression)[1w:1h]
rather than
metric[1w]
The subquery shown evaluates (some - long - expression) over a 1w period at 1h intervals to create a range vector. Note that unlike a simple metric range vector, you need to give it the sampling interval. Or you can write
(some - long - expression[1w:]
but this will just use whatever global default evaluation interval you have configured (which might be, say, 15 seconds or 1 minute)
P.S. Are you sure that query you showed is valid? The first part is fine, but the end doesn't make sense to me, and I can't see this syntax permitted either by
operators or
functions.
node_memory_MemTotal_bytes{job="kubernetes-service-endpoints",cluster="$cluster",instance=~"$node"} * on(instance) group_left(nodename) group(node_uname_info{})by(instance,nodename)
I would write this as simply:
node_memory_MemTotal_bytes{job="kubernetes-service-endpoints",cluster="$cluster",instance=~"$node"} * on(instance) group_left(nodename) node_uname_info
assuming that:
- N instances of the LHS match to 1 instance of the RHS with a corresponding "instance" label
- you want to add the "nodename" label from the RHS to all the timeseries returned by the LHS