Extracting value from an Alert fails

Hi, all! I´m new to InfluxDB and to time series in general. In the past weeks I´ve read articles, tutorials and a lot of documentation, so now I decided to practice. I´m stuck with a problem since a few days which I can´t figure out, so I would like to reach out to you.

I´m trying to extract the value of an existing alert, in order to send it to a telegram channel via the telegram package. The procedure is described very well in this tutorial. My problem is, that I can´t figure out how to properly extract the _value of the alert. When I try to include it in the telegram message, the message won´t be send out. I´ve been trying to solve this for several hour now and I´m really stuck.

My code is as follows:

import "contrib/sranka/telegram"

data = from(bucket: "_monitoring")
  |> range(start: -48h)
  |> filter(fn: (r) => r["_measurement"] == "statuses")
  |> filter(fn: (r) => r["_check_id"] == "07abe20dbb031000")
  |> filter(fn: (r) => r["_check_name"] == "Water_Consumption")
  |> filter(fn: (r) => r["_field"] == "_message")
  |> filter(fn: (r) => r["_level"] == "ok")
  |> last()
  |> findRecord(fn: (key) => true, idx: 0)

telegram.message(
	url: "https://api.telegram.org/bot",
	token: "token",
	channel: "channel",
	text: "Alert text: ${data._value}"
)

Telegram is working fine, I´m receiving the message as soon as I delete the ${data._value} placeholder. I think the problem lies in the findRecord function and I´ve tried playing with the group fields, but I really couldn´t find out how to make it work. Would be really nice of someone could give me an advice. Thank you!

UPDATE: I´ve found out that the extraction of other columns, like _measurement and _type works! So the only column which I can´t extract is the _value column. My message template is

Check: ${ r._check_name } is ${ r._level }, value is ${string(v: r._anomaly_index)}

which results in the string storend in the _value column of the alert to be like “Check: Water_Consumption is ok, value is 0.2536085255631998”. Is this string too long or what could the problem be? Thanks again in advance!

Hello @Pollo,
Thank you for doing due diligence and taking a look at that tutorial?
Can you please share your full script? The fact that you have:

Check: ${ r._check_name } is ${ r._level }, value is ${string(v: r._anomaly_index)}

Indicates to me that you might be making a custom check based from a UI generated check? Have you used the fieldsAsCol function? If so you’d be able to include the message field value in your telegram message with r._message.
Alternatively, you can use the monitor.from() function instead which includes a fieldsAsCol() function:

 text: "check: ${ r._check_name }: ${ r._message }", 

I would also consider using the monitor.notify() function so that you can map across the values in your check and not have to use the findRecord() function. But of course you can bypass the notifications system in InfluxDB.

That being said, I don’t see anything wrong with your query and it is odd that you can return “_measurement” values but not the “_value” value.
Can you please try:

import "array"
array.from(rows: [{_time: now(), _value: data._value}])

To check whether you’re able to get the value you expect? Thank you

EDIT

Try:

Converting to string first: string(v: data._value)

This is a bug check out the issue here:

Hi Anaisdg, and thank you for the exhaustive answer! The check which I´m using is standard. This code snippet:

Check: ${ r._check_name } is ${ r._level }, value is ${string(v: r._anomaly_index)}

is the status message template which I´m using as a message. What I was trying to do was to get the last check and send the message via telegram bot. I´ve notidced that monitor. enables complex functions, but I wanted to start slowly with a simple task and to just report the message (stored in the data._value column of the query above).

In the meanwhile I´ve found out a possible solution. Changing the parseMode to HTML solves the problem, so I guess the problem lies in the fact that I´m trying to report a float (0.25…). Floats contain dots, which, if I´m right, are special characters that need to be escaped in the default parseMode MarkdownV2. I hadn´t the time to test this, but the problem starts as soon as s dot gets in the text message. I hope this helps someone :slight_smile:

Thanks again for your support!

1 Like