influxdata / kapacitor

Open source framework for processing, monitoring, and alerting on time series data
MIT License
2.31k stars 492 forks source link

Allow Templating in .email field in SMTP handler. #2437

Open AndreyVChernykh opened 3 years ago

AndreyVChernykh commented 3 years ago

Hello, i'm trying tick script

var every = 20s var period = 5h var total = batch |query('select "Mails" from "DB"."RB"."MailsMeasurement" ORDER BY DESC LIMIT 1') .every(every) .period(period)
var mail = '{{ index .Fields "Mails" }}' total |log() |alert() .crit(lambda: TRUE) .message(mail) .email(mail)

Where Mails: Can be array of mails (but for tests i put in field one mail, i know that for arrays i can use '.to()')

When i watch logs of script, i see that message resolves and "event_message=MyMail@ForTest.com" appears, but i recieve nothing. [smtp] section configured, when i specify mail in TICK like " .email(MyMail@ForTest.com)" i recieve it

What am I missing?

Thanks a lot!

docmerlin commented 3 years ago

the .email(mail) field doesn't support templating, unfortunately. We should add this feature. @AndreyVChernykh

AndreyVChernykh commented 3 years ago

@docmerlin Hi! When i specify value 'mail' like this: var mail =MyMail@ForTest.com and pass it in .email(mail) it works! Maybe there is a type mismatch?

docmerlin commented 3 years ago

It just means it can't template out the string, but must have the fully realized string to work. I'll have to add templating to it to do what you want.

DeagleGross commented 3 years ago

@docmerlin Hello and thanks for Your replies! Want to share my thoughts and experiments on this functionality.

I've succeeded in substituting emails to array and then passing them to .emails() of batch-query. Watch this script:

var emails_group= ['email_1@email.com', 'email_2@email.com']
var data = batch
    |query(...)
    |alert(...)
        .email(emails_group)

If You are calling this as a template, then it's already working. Sorry for misunderstanding if I am not right.

The real reason this issue is created is to get emails from InfluxDb during select-query. Imagine we have a table with tag-column Product and field-column ManagerEmail. I want to query data about product from another table, group by data by Product and email alerts to ManagerEmail. And it is not working at all. I am guessing type of batch.field-variable is not the same as if I've created same variable as string-array. Here is a sample of script I want to create:

var managerData = batch
    |query(...)

var productData = batch
    |query(...)
        .groupby('Product')
    |join(managerData) // here joining is done by 'product'
    |alert(...)
        .email(managerData.ManagerEmail) // substituting email-array taken from db

Also if You can, please, share any thoughts on date when this functionality could be implemented and released Thanks in advance