-
Notifications
You must be signed in to change notification settings - Fork 0
35 Grafana使用入门
Jinxin Chen edited this page Dec 11, 2019
·
1 revision
本文介绍如何使用Grafana展示时序图
一个时序图包含两个基本要素:时间、数值,下面的sql获取了一系列以1小时分组的数据:
SELECT
(extract(epoch from "Created")/3600)::bigint*3600 AS time,
count(*) as value
FROM
"Table"
GROUP BY time
ORDER BY time
将这个Query输入到Grafana的graph中,则会展示出一张基本的时序图
在Grafana中,图形的展示是基于一定的时间区间,即“最近一个月”、“今天”等时间选项。
一般情况下,我们仅需要获取当前时间区间的时间即可,这时候可以用到宏:
SELECT
(extract(epoch from "Created")/3600)::bigint*3600 AS time,
count(*) as value
FROM
"Table"
WHERE $__timeFilter("Created")
GROUP BY time
ORDER BY time
也可以利用这个函数来轻松定义数据的时间间隔:
以1小时为单位获取数据:
SELECT
$__timeGroup("Created", '1h'),
count(*) as value
FROM
"Table"
WHERE $__timeFilter("Created")
GROUP BY time
ORDER BY time
也可以让系统自动根据当前图表大小来确定时间间隔:
SELECT
$__timeGroup("Created", $__interval),
count(*) as value
FROM
"Table"
WHERE $__timeFilter("Created")
GROUP BY "time"
ORDER BY "time"
实际使用过程中,$__interval计算出来的间隔会非常小,不一定能满足一些需求,比如监控访问量,此时可以手动创建inteval类型的变量解决:
- Create variable of type Interval called $autoInterval
- Values: 1m,10m,30m,1h,6h,12h,1d,7d,14d,30d,90d
- Auto Option: On
- Step Count: 20
- Hide: Variable
- Change $__timeGroup("Created", $__interval) to $__timeGroup("Created", $autoInternal, 0)