如何在 Promscale 的聚合查询中对标签进行分组
Posted
技术标签:
【中文标题】如何在 Promscale 的聚合查询中对标签进行分组【英文标题】:How to group on labels in aggregate query to Promscale 【发布时间】:2021-07-24 03:03:57 【问题描述】:我正在创建从 Grafana 到 Promscale 的 SQL 查询。有指标和标签。我无法获得按某些标签进行分组的正确方法。我试过了:
SELECT time_bucket('$__interval', "time") AS "time",
AVG("value") AS "used"
FROM "disk_used_percent"
WHERE $__timeFilter("time") AND
"labels" ? ('host' == '$host_pg')
GROUP BY 1, "labels" --> 'path'
ORDER BY 1;
还有:
SELECT time_bucket('$__interval', "time") AS "time",
AVG("value") AS "used"
FROM "disk_used_percent"
WHERE $__timeFilter("time") AND
"labels" ? ('host' == '$host_pg')
GROUP BY 1, "path_id"
ORDER BY 1;
但分组似乎没有按预期工作。怎么了?相应的 PromQL 查询将是:
avg(disk_used_percenthost=~"$host_prom") by(path))
【问题讨论】:
【参考方案1】:您可以使用VAL("<label>_id")
进行分组:
SELECT time_bucket('$__interval', "time") AS "time",
VAL("path_id") AS "path",
AVG("value") AS "used"
FROM "disk_used_percent"
WHERE $__timeFilter("time") AND
"labels" ? ('host' == '$host_pg')
GROUP BY 1, 2
ORDER BY 1;
旁注:还要避免在 Grafana 中使用 $__timeFilter("time")
模板宏,因为它会生成以下谓词:
"time" BETWEEN 'time range begin' AND 'time range end'
这可能是 problematic 在某些情况下。
【讨论】:
以上是关于如何在 Promscale 的聚合查询中对标签进行分组的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Sequelize 中对 belongsToMany 关联进行***查询?