<p>I am using <code>kube-prometheus-stack</code> and the yaml snippets you see below are part of a <code>PrometheusRule</code> definition.</p>
<p>This is a completely hypothetical scenario, the simplest one I could think of that illustrates my point.</p>
<p>Given this kind of metric:</p>
<pre><code>cpu_usage{job=&quot;job-1&quot;, must_be_lower_than=&quot;50&quot;} 33.72
cpu_usage{job=&quot;job-2&quot;, must_be_lower_than=&quot;80&quot;} 56.89
# imagine there are plenty more lines here
# with various different values for the must_be_lower_than label
# ...
</code></pre>
<p>I'd like to have alerts that check the label <code>must_be_lower_than</code> and alert. Something like this (this doesn't work the way it's written now, just trying to demonstrate):</p>
<pre><code>alert: CpuUsageTooHigh
annotations:
  message: 'On job {{ $labels.job }}, the cpu usage has been above {{ $labels.must_be_lower_than }}% for 5 minutes.'
expr: cpu_usage &gt; $must_be_lower_than
for: 5m
</code></pre>
<p><strong>P.S</strong> I already know I can define alerts like this:</p>
<pre class="lang-yaml prettyprint-override"><code>alert: CpuUsageTooHigh50
annotations:
  message: 'On job {{ $labels.job }}, the cpu usage has been above 50% for 5 minutes.'
expr: cpu_usage{must_be_lower_than=&quot;50&quot;} &gt; 50
for: 5m
---
alert: CpuUsageTooHigh80
annotations:
  message: 'On job {{ $labels.job }}, the cpu usage has been above 80% for 5 minutes.'
expr: cpu_usage{must_be_lower_than=&quot;80&quot;} &gt; 80
for: 5m
</code></pre>
<p>This is not what I'm looking for, because I have to manually define alerts for <em>some</em> of the various values of the <code>must_be_lower_than</code> label.</p>

<p>There is currently no way in Prometheus to have this kind of &quot;templating&quot;.</p>
<p>The only way to get something near would be to use recording rules that that define the maximum value for the label:</p>
<pre><code>rules:
- record: max_cpu_usage
  expr: vector(50)
  labels:
    must_be_lower_than:&quot;50&quot;
- record: max_cpu_usage
  expr: vector(80)
  labels:
    must_be_lower_than:&quot;80&quot;
# ... other possible values
</code></pre>
<p>Then use it in your alerting rule:</p>
<pre><code>alert: CpuUsageTooHigh
annotations:
  message: 'On job {{ $labels.job }}, the cpu usage has been above {{ $labels.must_be_lower_than}}% for 5 minutes.'
expr: cpu_usage &gt; ON(must_be_lower_than) GROUP_LEFT max_cpu_usage
for: 5m
</code></pre>
