grafana / clickhouse-datasource

Grafana Plugin for ClickHouse
Apache License 2.0
127 stars 58 forks source link

grafana's $__to and $__from are not supported in alerts #887

Closed vbichov closed 2 weeks ago

vbichov commented 2 months ago

What happened:

I tried using grafana's $from and $to variables

in alert condition but got the following exception when testing alert condition:

request handler response error {error querying the database: code: 62, message: Syntax error: failed at position 861 ('$') (line 27, col 68): ${__from} / 1000))) and t < toStartOfMinute(toDateTime(toInt32(${__to} / 1000)))
    -- where t + interval 1 hour > (toDateTime(toInt32(${__from} / 1000))) and . Expected one of: ALL, DISTINCT, token, Comma, ClosingRoundBracket, CAST operator, NOT, INTERVAL, CASE, DATE, TIMESTAMP, tuple, collection of literals, array, number, literal, NULL, NULL, Bool, TRUE, FALSE, string literal, asterisk, qualified asterisk, compound identifier, list of elements, identifier, COLUMNS matcher, COLUMNS, qualified COLUMNS matcher, substitution, MySQL-style global variable  A <nil> [] [] 0xc003e27170}"

What you expected to happen:

alert evaluation should have been successful

How to reproduce it (as minimally and precisely as possible):

  1. run grafana 10.3.3 with legacy alerting enabled

  2. create a graph pannel with query that uses $from an $to in it:

    select $__timeInterval(date_created) as t, count() from your_table
    where date_created < toDateTime(${__to} / 1000) and date_created > toDateTime(${__from} / 1000)
    group by t
    order by t
  3. make sure the graph works

  4. go to legacy alerts page - try to press Test rule

Screenshots

Environment:

SpencerTorres commented 1 month ago

Have you tried different syntax for accessing these variables? It seems like it's not replacing the ${xxx} syntax. Either that or the variable isn't defined in alerts mode. Do these variables work with other datasources? Can you show the HTTP request payload? Usually the request itself will define the time range, so we can see whether it's included or not.

Usually I see these written as $__from and $__to. You could also try one of the macros which can replace the time for you.

bossinc commented 2 weeks ago

@vbichov I am closing this issue. Please reopen if you still need help. 🙂