使用 Grafana 和 MySQL 在同一时间序列上绘制多个序列

Plot more than one series on the same time series using Grafana and MySQL

我有多个服务器报告一个值(温度或可靠性分数)。我想在同一时间序列上绘制它们。每个指标随时间变化的值。

table 看起来像这样

Total Reliability
Time                | value | metric
2021-12-07 08:24:20 | 0.994 | 2787
2021-12-07 08:25:18 | 0.996 | 3129
2021-12-07 08:25:34 | 0.994 | 2787
2021-12-07 08:26:31 | 0.996 | 3129
2021-12-07 08:26:48 | 0.994 | 2787
2021-12-07 08:27:44 | 0.996 | 3129

我阅读了一些类似的问题,但由于某种原因他们的解决方案不起作用

SELECT
time AS “time”,
reliability as value,
machine_id as metric
FROM machine
WHERE
$__unixEpochFilter(time)
GROUP BY machine_id,time
ORDER BY time asc

如果我删除分组以绘制两列

使用 Grafana v8.2.5 (b57a137acd) 和 MySQL 服务器 8.0.27

指标应为字符串。在你的情况下它看起来像一个整数,所以 hack machine_id as metric:

CONCAT(machine_id , '') AS metric

使用 Grafana MySQL macros 进行时间聚合。然后最终 Grafana SQL 5m 平均聚合查询应该是:

SELECT
  $__timeGroup(time,'5m'),
  AVG(reliability) AS value,
  CONCAT(machine_id, '') AS metric
FROM machine
WHERE $__unixEpochFilter(time)
GROUP BY machine_id, time
ORDER BY time ASC

它可能还需要一些小的调整。

我用这个

SELECT
  time AS "time",
  reliability AS value,
  CONCAT(machine_id, '') AS metric
FROM machine
WHERE
  $__unixEpochFilter(time)
ORDER BY time