You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Prometheus查询中使用Grafana变量遇解析错误

Fixing the "unexpected character: '$'" Error in PromQL Variable Usage

Ah, I see the issue here—let's break this down and fix it! The root problem is that PromQL itself doesn't natively support raw variable references at the start of a query. The $ character isn't part of standard PromQL syntax, so the Prometheus parser throws an error when it encounters that leading $. Variable substitution isn't handled by PromQL itself—it's managed by the tool you're using to run the query (like Grafana, Alertmanager, or a custom client) before the query gets sent to Prometheus.

Here's how to resolve this:

  • Leverage your tool's built-in variable system
    Most tools that interact with Prometheus (like Grafana) let you define variables that get replaced with actual values before the query is executed. For example:

    1. In your tool, create a variable (say, named target_metric) and set its value to totalaccuracy_total.
    2. In your PromQL query, reference the variable using the syntax your tool supports. For Grafana, this would be either $target_metric or ${target_metric}. So your query would look like:
      $target_metric
      
      Or if you want to run an aggregation:
      sum($target_metric) by (instance)
      
      Your tool will replace $target_metric with totalaccuracy_total before sending the query to Prometheus, so Prometheus receives a fully valid PromQL statement.
  • Keep in mind: Prometheus Web UI doesn't support custom variables
    If you're testing directly in the Prometheus Web UI's query editor, you can't use custom $ variables here. You'll need to write the full metric name directly, like totalaccuracy_total.

  • Double-check variable naming rules
    Make sure your variable name doesn't include characters that your tool considers invalid. While underscores are generally fine, some tools might restrict variable names to lowercase letters, numbers, and underscores—avoid spaces or special characters in variable names to prevent unexpected issues.


内容的提问来源于stack exchange,提问作者HandsDown

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:53:46