Grafana基于MySQL绘制折线图

2022-05-14 08:55:47 浏览数 (1)

写个笔记备忘下,防止后面再用到。

直接配置下即可

对应的SQL如下:

代码语言:javascript复制
SELECT
  $__timeGroupAlias(ts,$__interval),
  avg(metrics) AS "thread running"
FROM t_thread_running
WHERE
  $__timeFilter(ts)
GROUP BY 1
ORDER BY $__timeGroup(ts,$__interval)

t_thread_running表的建表语句如下:

代码语言:javascript复制
CREATE TABLE `t_thread_running` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `app` varchar(32) NOT NULL DEFAULT '',
  `metrics` bigint unsigned NOT NULL,
  `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2445 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

最关键的点就是表里面要有个时间列,这样Grafana上才能基于时间线来绘图


里面数据大致这样子:
    id  app                    metrics                   ts  
------  ---------------------  -------  ---------------------
     1  打码打码_MySQL                  5    2022-05-11 17:20:00
     2  打码打码_MySQL                  4    2022-05-11 17:21:00
     3  打码打码_MySQL                  6    2022-05-11 17:22:00
     4  打码打码_MySQL                  5    2022-05-11 22:50:00
     5  打码打码_MySQL                  4    2022-05-11 22:51:00
     6  打码打码_MySQL                  3    2022-05-11 22:52:00
     7  打码打码_MySQL                  5    2022-05-11 22:43:00

0 人点赞