如何在对 Promscale 的聚合查询中对标签进行分组

How to group on labels in aggregate query to Promscale

我正在创建 SQL 从 Grafana 到 Promscale 的查询。有指标和标签。我无法获得按某些标签分组的正确方法。我试过了:

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_percent{host=~"$host_prom"}) by(path))

您可以使用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某些情况下。