# Dynamic Labels For Alerting

**URL:** <https://community.grafana.com/t/dynamic-labels-for-alerting/86181>\
**Category:** Alerting\
**Created:** [April 20, 2023, 4:53pm UTC](https://community.grafana.com/t/dynamic-labels-for-alerting/86181 "2023-04-20T16:53:01Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![jasonmallory](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/jasonmallory/32/36686_2.png) [@jasonmallory](https://community.grafana.com/u/jasonmallory)\
**Post date:** [April 20, 2023, 4:53pm UTC](https://community.grafana.com/t/dynamic-labels-for-alerting/86181/1 "2023-04-20T16:53:01Z")

</div>

I have been trying to figure this out for months on alerting. I have even discussed doing enterprise licensing for Grafana but I need a problem figured out. I have a multitude of servers, services and other miscellaneous monitoring going on and need dynamic alerting. If I have 100 servers and they all have different services and logs being monitored, I should be able to dynamically get the hostname, service or whatever other issue we have that’s going into a database. The alerts know that there’s a variable there for a column:

 ![image](https://us1.discourse-cdn.com/grafana/original/3X/c/9/c95f6acafce0921646849a5c97ae91ae14624637.png)

But that’s what exactly shows up in the email:

 ![image](https://us1.discourse-cdn.com/grafana/original/3X/0/5/05bdc175964f3606d119bed887a6939f3d53aac3.png)

These columns show up in Explore:

 ![image](https://us1.discourse-cdn.com/grafana/original/3X/6/3/63060d95725ba06bbd18f01ee529b7b2622d162c.png)

 ![image](https://us1.discourse-cdn.com/grafana/original/3X/3/f/3f39f46751cefa7c7c019c4fc0e94de0bc844ac0.png)

But not for alerting. I also can’t do a SELECT in the alert for the text based columns so I can’t get the info that way either.

I find it hard to believe that the mark was missed on this. Am I missing something? (calling it by label, tag, field, column, whatever) or is it true that alerting is not dynamic?

---

<div class="post-metadata">

**Author:** ![georgerobinson](https://avatars.discourse-cdn.com/v4/letter/g/ad7895/32.png) [@georgerobinson](https://community.grafana.com/u/georgerobinson)\
**Post date:** [April 21, 2023, 10:45am UTC](https://community.grafana.com/t/dynamic-labels-for-alerting/86181/2 "2023-04-21T10:45:10Z")

</div>

Hi!

Yes this is supported 🙂 There is no variable called `$column`, but can use `$labels` in custom annotations to reference some or all of the labels returned by the datasource query. You can find the docs on this [here](https://grafana.com/docs/grafana/latest/alerting/fundamentals/annotation-label/variables-label-annotation/).

If you are using PostgreSQL you can do something like this:

```auto
SELECT 1, id::text, domain, chassis, model, oper_state_code FROM servers WHERE oper_state_code = 'degraded';

```

 ![Screenshot 2023-04-21 at 11.43.18](https://us1.discourse-cdn.com/grafana/original/3X/0/6/060042823ed2ef0fab30dc8b080be63523c5b023.png)

![Screenshot 2023-04-21 at 11.43.31](https://us1.discourse-cdn.com/grafana/original/3X/a/b/abe3013ff1a5ff78ba082d9aef31cceea8d5db38.png)

This is how it looks:

 ![Screenshot 2023-04-21 at 11.44.01](https://us1.discourse-cdn.com/grafana/original/3X/5/c/5cf70a9a9a6ea7fd844a3098d1031fccedb6939e.png)

 ![Screenshot 2023-04-21 at 11.44.13](https://us1.discourse-cdn.com/grafana/original/3X/d/a/da599b0c689c09a228339c3667147bb5bb99f30a.png)

---

<div class="post-metadata">

**Author:** ![jasonmallory](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/jasonmallory/32/36686_2.png) [@jasonmallory](https://community.grafana.com/u/jasonmallory)\
**Post date:** [April 21, 2023, 4:13pm UTC](https://community.grafana.com/t/dynamic-labels-for-alerting/86181/3 "2023-04-21T16:13:02Z")

</div>

Okay this is my query on InfluxDB:

select domain, chassis, id, model, last(oper\_state\_code) from Servers where oper\_state = ‘degraded’

So would I put ::text infornt of each column I want recognized as a label? I tried using label before

---

<div class="post-metadata">

**Author:** ![jasonmallory](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/jasonmallory/32/36686_2.png) [@jasonmallory](https://community.grafana.com/u/jasonmallory)\
**Post date:** [April 21, 2023, 4:18pm UTC](https://community.grafana.com/t/dynamic-labels-for-alerting/86181/4 "2023-04-21T16:18:20Z")

</div>

This is what I get:

 ![image](https://us1.discourse-cdn.com/grafana/original/3X/c/d/cd74ebb58e4de0760a5c1a742dcc4b26ecd36921.png)

 ![image](https://us1.discourse-cdn.com/grafana/original/3X/2/5/2550e5f968b1cabbc91b1d2504de55ad27c70cb2.png)

---

<div class="post-metadata">

**Author:** ![georgerobinson](https://avatars.discourse-cdn.com/v4/letter/g/ad7895/32.png) [@georgerobinson](https://community.grafana.com/u/georgerobinson)\
**Post date:** [April 21, 2023, 4:19pm UTC](https://community.grafana.com/t/dynamic-labels-for-alerting/86181/5 "2023-04-21T16:19:42Z")

</div>

For InfluxDB no because `::text` is a PostgreSQL feature. I’m afraid I don’t use InfluxDB so I’m not 100% sure how it should look in InfluxQL, but you can take the example which is for PostgreSQL and translate it to InfluxQL and it should just work.

Looking at the InfluxQL you’ve already shared I think you want a number (I used `1`) and also need to make sure that the labels are strings. You should be able to find out how to do this with the InfluxDB documentation on the InfluxDB website.

---

<div class="post-metadata">

**Author:** ![jasonmallory](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/jasonmallory/32/36686_2.png) [@jasonmallory](https://community.grafana.com/u/jasonmallory)\
**Post date:** [April 21, 2023, 4:22pm UTC](https://community.grafana.com/t/dynamic-labels-for-alerting/86181/6 "2023-04-21T16:22:37Z")

</div>

Any example for MySQL?

---

<div class="post-metadata">

**Author:** ![jasonmallory](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/jasonmallory/32/36686_2.png) [@jasonmallory](https://community.grafana.com/u/jasonmallory)\
**Post date:** [April 21, 2023, 4:35pm UTC](https://community.grafana.com/t/dynamic-labels-for-alerting/86181/7 "2023-04-21T16:35:56Z")

</div>

And I don’t see anything about strings for tag keys:

## tagKey

chassis  
domain  
host  
id  
location  
service\_profile  
type

---

<div class="post-metadata">

**Author:** ![jasonmallory](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/jasonmallory/32/36686_2.png) [@jasonmallory](https://community.grafana.com/u/jasonmallory)\
**Post date:** [April 21, 2023, 5:33pm UTC](https://community.grafana.com/t/dynamic-labels-for-alerting/86181/8 "2023-04-21T17:33:42Z")

</div>

This seems to be the problem. There is no examples for $labels being used with different datasources. After talking to Grafana they said it’s not possible. So I’m not sure what to do. I am a BIG proponent of Grafana and would like to use it and replace Nagios. But with alerting not being dynamic or not properly documented/supported we can’t use it

---

<div class="post-metadata">

**Author:** ![jasonmallory](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/jasonmallory/32/36686_2.png) [@jasonmallory](https://community.grafana.com/u/jasonmallory)\
**Post date:** [April 21, 2023, 6:29pm UTC](https://community.grafana.com/t/dynamic-labels-for-alerting/86181/9 "2023-04-21T18:29:09Z")

</div>

This is the query and the data returned. We are not getting the other key tags in the query:

 ![image](https://us1.discourse-cdn.com/grafana/original/3X/0/b/0b975529a05c42fed096f971586892cc5d95f5b5.png)

---

<div class="post-metadata">

**Author:** ![georgerobinson](https://avatars.discourse-cdn.com/v4/letter/g/ad7895/32.png) [@georgerobinson](https://community.grafana.com/u/georgerobinson)\
**Post date:** [April 21, 2023, 9:48pm UTC](https://community.grafana.com/t/dynamic-labels-for-alerting/86181/10 "2023-04-21T21:48:55Z")

</div>

In MySQL the query would be `SELECT 1, cast(id as char(11)) AS id, domain, chassis, model, oper_state_code FROM servers WHERE oper_state_code = 'degraded';`

---

<div class="post-metadata">

**Author:** ![georgerobinson](https://avatars.discourse-cdn.com/v4/letter/g/ad7895/32.png) [@georgerobinson](https://community.grafana.com/u/georgerobinson)\
**Post date:** [April 21, 2023, 9:49pm UTC](https://community.grafana.com/t/dynamic-labels-for-alerting/86181/11 "2023-04-21T21:49:34Z")

</div>

Looking at the InfluxDB docs tags are strings, so no need to make `id` a string if using InfluxDB.

---

<div class="post-metadata">

**Author:** ![jasonmallory](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/jasonmallory/32/36686_2.png) [@jasonmallory](https://community.grafana.com/u/jasonmallory)\
**Post date:** [April 28, 2023, 5:49pm UTC](https://community.grafana.com/t/dynamic-labels-for-alerting/86181/12 "2023-04-28T17:49:21Z")

</div>

What we’re seeing when we run the InfluxDB query, not all the columns are returned in the Alerts query. Could that have something to do as why we can’t get labels?
