Skip to content

ClickHouse Metrics

Lorenzo Mangani edited this page Jan 13, 2021 · 28 revisions

ClickHouse Metrics

cLoki provides a simplified query model for generating metrics and tags dynamically out of clickhouse tables.

This approach requires no preparation and poses no discrimination as of how data is inserted into clickhouse. Since Grafana handles cLoki endpoints as native loki datasources, no additional plugins are required to access data.

Query Options

The clickhouse cLoki macro accepts the following parameters:

parameter description
db clickhouse database name
table clickhouse table name
tag column(s) for tags, comma separated
metric function for metric values
where where condition (optional)
interval interval in seconds (optional)

Example

In this example, we'll query database my_database and table my_table to extract the average value of column mos as metric, and source_ip as one of many possible grouping tags. We can optionally specify a WHERE clause and specify the interval for our response resolution.

clickhouse({ 
  db="my_database", 
  table="my_table", 
  tag="source_ip", 
  metric="avg(mos)", 
  where="mos > 0", 
  interval="60" 
})
Clickhouse Query

The cloki subset query is translated into a native clickhouse query using the relative parameters and time boundaries.

SELECT source_ip, groupArray((t, c)) AS groupArr 
FROM (
  SELECT (intDiv(toUInt32(record_datetime), 60) * 60) * 1000 AS t, source_ip, avg(mos) c 
  FROM my_database.my_table 
  PREWHERE record_datetime BETWEEN 1610533076 AND 1610536677 AND mos > 0 
  GROUP BY t, source_ip ORDER BY t, source_ip
) 
GROUP BY source_ip ORDER BY source_ip

The results are formatted emulating the loki matrix format for tagged timeseries:

cloki

Clone this wiki locally