You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add macro similar to $rateColumns for counter like metrics #80
Similarly to request in #78, please consider adding support for computing $perSecond for multiple series (hosts).
Currently, the $rateColumns computes something like:
SELECT t, arrayMap(a -> (a.1, a.2/runningDifference( t/1000 )), groupArr)
FROM (
SELECT t, groupArray((host, v)) as groupArr
....
Which does not produce correct results for metrics which only grows over time, as one needs to compute difference between two consequitive values v as well (not just time).
Unfortunatelly, doing runningDifference(a.2)/runningDifference(t/1000) does not work, as a.2 gets materialized to different values v between different columns (host).
I have been scratching my head for a while and I am not sure how to write such query in Clickhouse at all. I hope you know better :)
Here we have a Type column and counter Packets. It is obvious, that tcp grows by 1000 per minute (1000/60=16.6 per second) and udp by 2000 per minute (2000/60=33.33 per second).
I think the macros should implement the following query:
SELECT
t,
arrayMap(a -> (a.1, a.2 / runningDifference(t)), groupArr) AS am
FROM
(
SELECT
t,
groupArray((Type, runningDifference(c))) AS groupArr
FROM
(
SELECT
intDiv(toUInt32(Time), 120) * 120 AS t,
Type,
max(Packets) AS c
FROM issue_78
GROUP BY
t,
Type
ORDER BY
Type ASC,
t ASC
)
GROUP BY t
ORDER BY t ASC
)
┌──────────t─┬─am──────────────────────────────────────────────────────┐
│ 1535712600 │ [('tcp',nan),('udp',-inf)] │
│ 1535712720 │ [('tcp',16.666666666666668),('udp',33.333333333333336)] │
│ 1535712840 │ [('tcp',16.666666666666668),('udp',33.333333333333336)] │
│ 1535712960 │ [('tcp',16.666666666666668),('udp',33.333333333333336)] │
│ 1535713080 │ [('tcp',16.666666666666668),('udp',33.333333333333336)] │
└────────────┴─────────────────────────────────────────────────────────┘
5 rows in set. Elapsed: 0.009 sec.
Please, note some details:
Selecting max value for period instead of counting, since we're dealing with counter:
max(Packets)
The reverted sort in inner query:
ORDER BY
Type ASC,
t ASC
The runningDifference comparison which is possibly because of reverse sorting:
groupArray((Type, runningDifference(c))) AS groupArr
Please, try this construction with your data. If it fits your need I'll add a new macros into plugin.
Thank you!
Hi @hagen1778 ,
Thanks a lot for your reply, it's very useful. I'm from @JiriHorky team and I have been testing your query for a while and I think we have a problem when there are missing rows in ClickHouse table. I will try to explain:
This query I want to create should produce a graph of CPU usage of the particular hosts and we want to be able to see the metric for several hosts. After a small adjustment I made it work with Grafana's variable $host, where we want to select multiple values (hosts).
I adjusted the query like this:
SELECT
t,
arrayMap(a -> (a.1, 100 - a.2 / runningDifference(t) * 1000), ga) AS am
FROM
(
SELECT
t,
groupArray((host, runningDifference(v))) AS ga
FROM
(
SELECT
$timeSeries AS t,
host,
max(value) AS v
FROM $table
WHERE $timeFilter and host in ($host)
GROUP BY
t,
host
ORDER BY
host ASC,
t ASC
)
GROUP BY t
ORDER BY t ASC
)
Where $table is set to cpu_idle. And as for $host I will select two values S182V06001924 and S182V06001957 for this example .
In ClickHouse we have table cpu_idle and here is an example of data from the two hosts:
You may notice that we did not receive data from host S182V06001957 at 12:10, 12:15 and 12:20.
And here comes the issue:
If I try to apply the query on this data and simulate multiselect by WHERE (host IN ('S182V06001957', 'S182V06001924')):
SELECT
toDateTime(t),
t,
arrayMap(a -> (a.1, 100 - (a.2 / runningDifference(t))), groupArr) AS am
FROM
(
SELECT
t,
groupArray((host, runningDifference(v))) AS groupArr
FROM
(
SELECT
toUInt32(time) AS t,
host,
max(value) AS v
FROM cpu_idle
WHERE (host IN ('S182V06001957', 'S182V06001924')) AND (time >= '2018-10-29 12:00:00') AND (time <= '2018-10-29 12:25:00')
GROUP BY
t,
host
ORDER BY
host ASC,
t ASC
)
GROUP BY t
ORDER BY t ASC
)
As you can see in the last row the value we get for S182V06001957 (and thus the graph) are erroneous in case we use multiselect and some data are missing. In other words, the curves in the graph change depending on what host(-s) I select.
Could you please help me with that? Have you got any idea how to get rid of this? I tried to adjust the query for a while but unsuccessfully.
In case you need any further info, please ask.
Thanks a lot.
We see a little time gap for udp type between last two rows. To avoid false results I suggest to use following query:
SELECT
t * 1000 AS t,
groupArray((Type, c)) AS groupArr
FROM
(
SELECT
t,
Type,
runningDifference(m) AS diff,
if(diff < 0, nan, diff / runningDifference(t)) AS c
FROM
(
SELECT
intDiv(toUInt32(Time), 120) * 120 AS t,
Type,
max(Packets) AS m
FROM issue_78
GROUP BY
t,
Type
ORDER BY
Type ASC,
t ASC
)
)
GROUP BY t
ORDER BY t ASC
┌─────────────t─┬─groupArr────────────────────────────────────────────────┐
│ 1535712600000 │ [('tcp',nan),('udp',nan)] │
│ 1535712720000 │ [('tcp',16.666666666666668),('udp',33.333333333333336)] │
│ 1535712840000 │ [('tcp',16.666666666666668),('udp',33.333333333333336)] │
│ 1535712960000 │ [('tcp',16.666666666666668),('udp',33.333333333333336)] │
│ 1535713080000 │ [('tcp',16.666666666666668),('udp',33.333333333333336)] │
│ 1535713200000 │ [('tcp',16.666666666666668)] │
│ 1535713320000 │ [('tcp',16.666666666666668)] │
│ 1535713440000 │ [('tcp',16.666666666666668),('udp',33.333333333333336)] │
│ 1535713560000 │ [('tcp',16.666666666666668),('udp',33.333333333333336)] │
└───────────────┴─────────────────────────────────────────────────────────┘
The following operations:
SELECT
runningDifference(m) AS diff,
if(diff < 0, nan, diff / runningDifference(t)) AS c
gives us opportunity to compare values of the same type considering the time gaps. Also, the condition if(diff < 0 avoids false results of comparing the values for different types.
The result of the query could be easily displayed in Grafana, since plugin knows how to behave with null values. Anyway, you still have a possibility to manage the way of displaying the null values on panel:
This works perfectly, thank you very much. :) My final version of the query look like this:
SELECT
t,
groupArray((host, c)) AS groupArr
FROM
(
SELECT
t,
host,
runningDifference(v) AS diff,
if(diff < 0, nan, 100 - diff / runningDifference(t) * 1000) AS c
FROM
(
SELECT
$timeSeries AS t,
host,
max(value) AS v
FROM $table
WHERE $timeFilter and host in ($host)
GROUP BY
t,
host
ORDER BY
host ASC,
t ASC
)
)
GROUP BY t
ORDER BY t ASC
Are going to create the macro for this? If so, I would wait for it, because I want to implement similar queries in some 20+ other graphs and the macro would make everything easier and faster.
Hi @MichalNedbalek
Why there is an additional sub operation in if(diff < 0, nan, 100 - diff / runningDifference(t) * 1000) AS c? I mean the following 100 - diff
Hi @hagen1778
Because in this particular case we get cpu_idle metric from the hosts. So when I want to calculate percentage of CPU usage I need to subtract the value from 100.
Currently I don't see an easy way to make customizable macros where you will be able to do 100 - diff. The macros is following: $perSecondColumns(type, total) FROM requests where type in ('udp', 'tcp')
Which results into:
SELECT
t,
groupArray((Type, max_0_Rate)) AS groupArr
FROM
(
SELECT
t,
type,
if(runningDifference(max_0) < 0, nan, runningDifference(max_0) / runningDifference(t / 1000)) AS max_0_Rate
FROM
(
SELECT
$timeSeries AS t,
type,
max(total) AS max_0
FROM $table
WHERE $timeFilter
AND (Type IN ('udp', 'tcp'))
GROUP BY
t,
type
ORDER BY
type ASC,
t ASC
)
)
GROUP BY t
ORDER BY t ASC
In the inner query we need to get max value for column (so it should be just it's name) and in second query apply runningDifference. I'm wondering how should I support passing extra operations here...
The both $perSecond and $perSecondColumns are in the master right now. Could you test your queries with that update?
Today we will try to install it to our testing Grafana and test it there.
As for the 100 - diff queries, I think it is not necessary to be concerned with it for now. I will use the query as it is now and later we will change the metrics we get from the hosts and the query will get changed anyway.
Sorry for the delay and thank you for the fix. I finally got to the testing of your new macros. It looks ok from my point of view and it is perfectly usable (except for the special case higher where I'm gonna use the slightly complicated query because of the 100 - diff).
Activity
hagen1778 commentedon Oct 18, 2018
Hi @JiriHorky
Back to #78
Let's introduce new data:
Here we have a
Typecolumn and counterPackets. It is obvious, thattcpgrows by 1000 per minute (1000/60=16.6 per second) andudpby 2000 per minute (2000/60=33.33 per second).I think the macros should implement the following query:
Please, note some details:
maxvalue for period instead of counting, since we're dealing with counter:Please, try this construction with your data. If it fits your need I'll add a new macros into plugin.
Thank you!
MichalNedbalek commentedon Oct 29, 2018
Hi @hagen1778 ,
Thanks a lot for your reply, it's very useful. I'm from @JiriHorky team and I have been testing your query for a while and I think we have a problem when there are missing rows in ClickHouse table. I will try to explain:
This query I want to create should produce a graph of CPU usage of the particular hosts and we want to be able to see the metric for several hosts. After a small adjustment I made it work with Grafana's variable
$host, where we want to select multiple values (hosts).I adjusted the query like this:
Where
$tableis set tocpu_idle. And as for$hostI will select two valuesS182V06001924andS182V06001957for this example .In ClickHouse we have table
cpu_idleand here is an example of data from the two hosts:You may notice that we did not receive data from host
S182V06001957at 12:10, 12:15 and 12:20.And here comes the issue:
If I try to apply the query on this data and simulate multiselect by
WHERE (host IN ('S182V06001957', 'S182V06001924')):I will get this:
But if I select just
S182V06001957I will get this:As you can see in the last row the value we get for
S182V06001957(and thus the graph) are erroneous in case we use multiselect and some data are missing. In other words, the curves in the graph change depending on what host(-s) I select.Could you please help me with that? Have you got any idea how to get rid of this? I tried to adjust the query for a while but unsuccessfully.
In case you need any further info, please ask.
Thanks a lot.
hagen1778 commentedon Oct 29, 2018
Hi @MichalNedbalek,
I guess I got the idea. Let's introduce updated data:
We see a little time gap for
udptype between last two rows. To avoid false results I suggest to use following query:The following operations:
gives us opportunity to compare values of the same type considering the time gaps. Also, the condition
if(diff < 0avoids false results of comparing the values for different types.The result of the query could be easily displayed in Grafana, since plugin knows how to behave with null values. Anyway, you still have a possibility to manage the way of displaying the null values on panel:

Hope that will help!
MichalNedbalek commentedon Oct 30, 2018
Hi @hagen1778,
This works perfectly, thank you very much. :) My final version of the query look like this:
Are going to create the macro for this? If so, I would wait for it, because I want to implement similar queries in some 20+ other graphs and the macro would make everything easier and faster.
Thanks a lot.
hagen1778 commentedon Oct 30, 2018
Yes, I will implement it. New release will published this week
MichalNedbalek commentedon Oct 30, 2018
Thanks for info
hagen1778 commentedon Oct 30, 2018
Hi @MichalNedbalek
Why there is an additional sub operation in
if(diff < 0, nan, 100 - diff / runningDifference(t) * 1000) AS c? I mean the following100 - diffMichalNedbalek commentedon Oct 31, 2018
Hi @hagen1778
Because in this particular case we get
cpu_idlemetric from the hosts. So when I want to calculate percentage of CPU usage I need to subtract the value from 100.hagen1778 commentedon Oct 31, 2018
Currently I don't see an easy way to make customizable macros where you will be able to do
100 - diff. The macros is following:$perSecondColumns(type, total) FROM requests where type in ('udp', 'tcp')Which results into:
In the inner query we need to get max value for column (so it should be just it's name) and in second query apply runningDifference. I'm wondering how should I support passing extra operations here...
The both $perSecond and $perSecondColumns are in the master right now. Could you test your queries with that update?
MichalNedbalek commentedon Oct 31, 2018
I have to wait for a colleague who will update our Grafana and I'm going to test it tomorrow. Thanks for the macros.
MichalNedbalek commentedon Nov 1, 2018
Hi @hagen1778 ,
We tried the new version and we ended up with all graphs not working with error
query is undefinedand stacktrace like this:Today we will try to install it to our testing Grafana and test it there.
As for the
100 - diffqueries, I think it is not necessary to be concerned with it for now. I will use the query as it is now and later we will change the metrics we get from the hosts and the query will get changed anyway.Thanks
hagen1778 commentedon Nov 1, 2018
Yep, my fault. Should be fixed now. Could you try it again?
hagen1778 commentedon Nov 5, 2018
@MichalNedbalek ping
MichalNedbalek commentedon Nov 7, 2018
Hi @hagen1778,
Sorry for the delay and thank you for the fix. I finally got to the testing of your new macros. It looks ok from my point of view and it is perfectly usable (except for the special case higher where I'm gonna use the slightly complicated query because of the
100 - diff).So what are the next steps now?
Thanks
hagen1778 commentedon Nov 7, 2018
Please, see https://github.com/Vertamedia/clickhouse-grafana/releases/tag/1.8.0