count_distinct aggFn on metric tiles ignores valueExpression, always counts distinct metric Values

未关闭 适合新手
#2,311 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@Harshul1484 已经在做这个了。

开始于 2026年9月28日。

  • #3237 来自 @Harshul1484 —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
72/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
冷清
技术栈
clickhouse, sql, typescript

调研方向

从 packages/common-utils/src/core/renderChartConfig.ts 中的 translateMetricChartConfig 开始,检查在生成 SQL 之前如何翻译 gauge 选择项。复现 issue 中的 count_distinct 指标配置,保留其属性 valueExpression,并验证生成的查询统计的是不同的属性值,而不是 LastValue。

由索引模型根据 Issue 内容生成。

描述

Bug Description

When using count_distinct as the aggFn on a metric tile, the valueExpression is ignored. The generated SQL always counts distinct values of the metric's float Value column rather than the specified attribute expression.

Steps to Reproduce

  1. Create a dashboard tile with displayType: "number" and a metric select using count_distinct with a valueExpression pointing to an attribute:
    {
      "displayType": "number",
      "source": "Metrics",
      "select": [
        {
          "aggFn": "count_distinct",
          "valueExpression": "Attributes['name']",
          "metricName": "argocd_app_info",
          "metricType": "gauge"
        }
      ]
    }
    
  2. Observe the number displayed — for an info metric (where Value is always 1.0), the tile shows 1 or 2 instead of the actual count of distinct attribute values.

Expected Behavior

The tile should display the count of distinct values of Attributes['name'] across all time series for the metric — i.e., the number of distinct applications reported by argocd_app_info.

Direct ClickHouse query gives the correct answer:

SELECT uniqExact(Attributes['name'])
FROM default.otel_metrics_gauge
WHERE MetricName = 'argocd_app_info'
  AND TimeUnix >= now() - INTERVAL 1 HOUR
-- returns 53

Actual Behavior

The generated SQL uses count(DISTINCT LastValue) where LastValue = last_value(Value) — the gauge's numeric value — not the attribute expression:

-- Generated by HyperDX
...
Bucketed AS (
  SELECT
    ...,
    last_value(Value) AS LastValue,   -- <-- always the float metric value
    any(Attributes) AS Attributes
  FROM Source
  GROUP BY AttributesHash, __hdx_time_bucket2
)
SELECT count(DISTINCT LastValue)      -- <-- counts distinct 1.0, 0.0, etc.
FROM Bucketed

Result: 2 (distinct float values) instead of 53 (distinct app names).

Root Cause

In packages/common-utils/src/core/renderChartConfig.ts, translateMetricChartConfig unconditionally overwrites valueExpression with 'LastValue' for gauge metrics before passing the select config to SQL generation:

select: [
  {
    ..._select,
    valueExpression: 'LastValue',  // overwrites user-provided Attributes['name']
    aggCondition: '',
  },
]

The original valueExpression from the user config is never used in metric query translation.

Additional Context

  • The zod schema (externalDashboardSelectItemSchema in packages/api/src/utils/zod.ts) accepts and even requires valueExpression for non-count aggFns on metrics — so the intent to support this exists, but the query translator doesn't honour it.
  • The UI currently has no input for valueExpression on metric tiles, which makes sense given it's ignored — but fixing the translator would enable useful aggregations like counting distinct label values.
  • Confirmed present in v2.27.0. No prior issue or fix found.

Suggested Fix

For count_distinct (and potentially other aggFns where valueExpression refers to an attribute), use the user-provided expression instead of hardcoding 'LastValue':

// In translateMetricChartConfig for gauge metrics:
const effectiveValueExpression =
  _select.aggFn === 'count_distinct' && _select.valueExpression
    ? _select.valueExpression
    : 'LastValue';

select: [
  {
    ..._select,
    valueExpression: effectiveValueExpression,
    aggCondition: '',
  },
]
主要语言
TypeScript
星标
10.1k
派生
527
平均合并
1 天 20 小时
30 天内合并 PR
186

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

ClickHouse/ClickStack 的其他 Issue

查看 ClickHouse/ClickStack 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。