<p>I'm working with Prometheus alerts, and I would like to dynamically add a 'team' label to all of my alerts based on a regex pattern. I have an example alert:</p>
<pre><code>expr: label_replace(label_replace(increase(kube_pod_container_status_restarts_total{job=&quot;kube-state-metrics&quot;,namespace=~&quot;.*&quot;,pod!~&quot;app-test-.*&quot;}[30m]) &gt; 2, &quot;team&quot;, &quot;data&quot;, &quot;container&quot;, &quot;.*test.*&quot;), &quot;team&quot;, &quot;data&quot;, &quot;pod&quot;, &quot;.*test.*&quot;)
</code></pre>
<p>This example alert adds the 'team' label with the value 'data' for metrics matching the regex pattern &quot;.test.&quot; in the 'container' and 'pod' labels.</p>
<p>However, I want to apply this logic to all of my alerts, not just this specific one. Is there a way to do this dynamically in Prometheus or Alertmanager? Any guidance would be appreciated.</p>
<p>I tried using the <strong>label_replace</strong> function in the expression of the alert, and it worked as expected for the specific alert mentioned above. I was expecting to find a way to apply this label addition to all of my alerts without having to modify each alert expression individually.</p>
<p>Is there a way to achieve this? Any help or guidance would be greatly appreciated.</p>

TomerA
<p>AFAIK, there is no possibility to add labels to your alerts based on condition without rewriting all rules.</p>
<p>Best solution for your exact question is to create separate alerts for all environments/teams/conditions and just add static labels.</p>
<p>Something along the lines of</p>
<pre class="lang-yaml prettyprint-override"><code>  - alert: many_restarts_data
    expr: increase(kube_pod_container_status_restarts_total{job=&quot;kube-state-metrics&quot;,namespace=~&quot;.*&quot;,pod!~&quot;app-test-.*&quot;, container=~&quot;.*test.*&quot;}[30m]) &gt; 2
    labels:
      team: data
    
  - alert: many_restarts_data
    expr: increase(kube_pod_container_status_restarts_total{job=&quot;kube-state-metrics&quot;,namespace=~&quot;.*&quot;,pod!~&quot;app-test-.*&quot;, container=~&quot;.*prod.*&quot;}[30m]) &gt; 2
    labels:
      team: sre
</code></pre>
<p>But it will require multiplying number of alerts by number of teams.</p>
<p>I would argue way easier solution is to use routing capabilities of alertmanager (or PagerDuty if it provides similar functionality). This way you write criteria which alerts with which labels should be routed to which teams, at alertmanager configuration, and it works independently from alerts creation part.</p>
<pre class="lang-yaml prettyprint-override"><code>    routes:
    - matchers:
        - container =~ &quot;.*test.*&quot;
        - severity =~ &quot;.*test.*&quot;
        - alertname =~ &quot;my_alert_1|my_alert_2&quot;
      receiver: team-data

    - matchers:
        - container =~ &quot;.*prod.*&quot;
        - severity =~ &quot;.*prod.*&quot;
        - alertname =~ &quot;my_alert_1|my_alert_2&quot;
      receiver: team-sre
</code></pre>
