If you want to do this inside prometheus, then you want recording rules 
<https://prometheus.io/docs/prometheus/latest/configuration/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 
<https://prometheus.io/docs/prometheus/latest/storage/#backfilling-for-recording-rules>
.

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 
<https://prometheus.io/docs/prometheus/latest/querying/basics/#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 
<https://prometheus.io/docs/prometheus/latest/querying/operators/#group-modifiers>
 
or functions 
<https://prometheus.io/docs/prometheus/latest/querying/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

On Tuesday, 13 December 2022 at 08:03:32 UTC [email protected] wrote:

> Hello All,
>
> I wanted to store the below Prometheus query into an variable.
> (node_memory_MemTotal_bytes{job="kubernetes-service-endpoints",cluster="$cluster",instance=~"$node"}
>  
> * on(instance) group_left(nodename) 
> group(node_uname_info{})by(instance,nodename)-(node_memory_MemFree_bytes{job="kubernetes-service-endpoints",cluster="$cluster",instance=~"$node"}
>  
> * on(instance) group_left(nodename) 
> group(node_uname_info{})by(instance,nodename) ))
>
> and use it in second query ,
> z = (sum(rate(metric)[1m]) - sum(avg_over_time(metric[1w])) / 
> sum(stddev_over_time(metric[1w])) 
>
> Is there any way to achieve this.
> Thankyou
>
>
>

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/prometheus-users/9b9f0a7b-c221-4cd6-bdf6-683c64418e0dn%40googlegroups.com.

Reply via email to