promql-cli — Prometheus Query CLI Skill
INLINECODE0 (github.com/nalbury/promql-cli) is a Go CLI for querying, analyzing, and visualizing Prometheus metrics, plus PromQL fundamentals.
Reference Files
Read the relevant reference file(s) before executing tasks:
| File | When to read |
|---|
| INLINECODE1 | User needs to install promql-cli or set up configuration (hosts, auth, token, password, multi-host) |
| INLINECODE2 |
User wants to discover metrics/exporters/labels, run queries, or choose output formats |
|
references/graphing.md | User wants to visualize Prometheus data as an ASCII chart in the terminal |
|
references/debugging.md | User is investigating a performance issue, latency, errors, or saturation |
|
references/promql-reference.md | User needs help writing PromQL, understanding metric types, functions, or aggregations |
For most tasks, read references/usage.md. For PromQL help, read references/promql-reference.md. When debugging, read both references/debugging.md and references/promql-reference.md.
Setup Check
Before running any query, verify that a host is configured:
CODEBLOCK0
Recognize these errors as a configuration/auth problem and refer to references/installation.md:
| Error | Cause |
|---|
| INLINECODE11 | No host running at the configured address |
| INLINECODE12 |
Hostname not resolved — wrong host in config |
|
error querying prometheus: ...401... | Bearer token missing or invalid |
|
error querying prometheus: ...403... | Token valid but insufficient permissions |
|
please specify an authentication type | Auth flags partially set — use config file instead |
If any of these appear, do not create config files on behalf of the user — config files may contain credentials (tokens, passwords) that must never pass through an LLM. Instead, guide the user to set it up themselves:
"Please create ~/.promql-cli.yaml manually with your Prometheus host (and credentials if needed). See references/installation.md for the exact format. Let me know once it's ready."
Only after the user confirms the config is in place should you proceed with queries.
Quick Command Reference
CODEBLOCK1
Key Principles
- 1. Use
rate() on counters, never raw values — raw counters only ever increase; the absolute value is meaningless. rate() gives the per-second change rate, which is what you actually care about. - When debugging, isolate a single instance — aggregating across replicas masks per-instance anomalies. A single overloaded pod hidden behind healthy peers won't show up in averages.
- Filter early with label matchers in the innermost selector — Prometheus evaluates selectors before functions, so filtering late means scanning all time series. Early filters reduce data scanned and query latency.
- For histograms, keep
le in the by clause before histogram_quantile() — the function needs all le buckets to interpolate percentiles; dropping le early produces NaN or wrong results. - Prefer
--output graph for range queries — ASCII sparklines convey trend direction (rising, falling, spiking) in a compact format that LLMs parse well; raw timestamp tables require mental modeling. - Store credentials in
~/.promql-cli.yaml and ~/.promql_token, chmod 600 — passing tokens as CLI args exposes them in shell history and process listings.
This skill is not exhaustive. Please refer to the official promql-cli documentation and examples for up-to-date information. Context7 can help as a discoverability platform.
If you encounter a bug or unexpected behavior in promql-cli itself, open an issue at https://github.com/nalbury/promql-cli/issues.
promql-cli — Prometheus 查询 CLI 技能
promql-cli (github.com/nalbury/promql-cli) 是一个用于查询、分析和可视化 Prometheus 指标的 Go 语言 CLI 工具,同时涵盖 PromQL 基础知识。
参考文件
在执行任务前,请阅读相关参考文件:
| 文件 | 何时阅读 |
|---|
| references/installation.md | 用户需要安装 promql-cli 或配置(主机、认证、令牌、密码、多主机) |
| references/usage.md |
用户想要发现指标/导出器/标签、运行查询或选择输出格式 |
| references/graphing.md | 用户希望在终端中以 ASCII 图表形式可视化 Prometheus 数据 |
| references/debugging.md | 用户正在排查性能问题、延迟、错误或饱和情况 |
| references/promql-reference.md | 用户需要编写 PromQL、理解指标类型、函数或聚合的帮助 |
对于大多数任务,请阅读 references/usage.md。如需 PromQL 帮助,请阅读 references/promql-reference.md。排查问题时,请同时阅读 references/debugging.md 和 references/promql-reference.md。
设置检查
在运行任何查询前,请验证主机是否已配置:
bash
promql up # 如果主机可达则成功;未配置则因连接错误失败
或
promql --host xxx up
将以下错误识别为配置/认证问题,并参考 references/installation.md:
| 错误 | 原因 |
|---|
| dial tcp ... connection refused | 配置地址处没有运行的主机 |
| dial tcp ... no such host |
主机名无法解析——配置中的主机错误 |
| error querying prometheus: ...401... | Bearer 令牌缺失或无效 |
| error querying prometheus: ...403... | 令牌有效但权限不足 |
| please specify an authentication type | 认证标志部分设置——请改用配置文件 |
如果出现以上任何错误,不要代表用户创建配置文件——配置文件可能包含凭证(令牌、密码),这些信息绝不能通过 LLM 传递。相反,引导用户自行设置:
请手动创建 ~/.promql-cli.yaml,填入您的 Prometheus 主机(以及必要的凭证)。具体格式请参见 references/installation.md。配置完成后请告知我。
只有在用户确认配置文件就绪后,您才能继续执行查询。
快速命令参考
bash
promql up # 即时查询
promql rate(httprequeststotal[5m]) --start 1h # 范围查询(ASCII 图表)
promql up --output csv # CSV 输出
promql up --output json # JSON 输出
promql metrics # 列出所有指标名称
promql labels # 列出指标的标签
promql meta # 显示指标类型和帮助信息
promql --config ~/.promql-cli-prod.yaml up # 针对特定主机
关键原则
- 1. 对计数器使用 rate(),切勿使用原始值——原始计数器只会增加;绝对值毫无意义。rate() 提供每秒变化率,这才是您真正关心的。
- 排查问题时,隔离单个实例——跨副本聚合会掩盖单个实例的异常。隐藏在健康对等体后面的单个过载 Pod 不会在平均值中显现。
- 在最内层的选择器中尽早使用标签匹配器进行过滤——Prometheus 在函数之前评估选择器,因此延迟过滤意味着扫描所有时间序列。早期过滤器可减少扫描的数据量和查询延迟。
- 对于直方图,在 histogramquantile() 之前将 le 保留在 by 子句中——该函数需要所有 le 桶来插值百分位数;过早丢弃 le 会产生 NaN 或错误结果。
- 范围查询优先使用 --output graph——ASCII 迷你趋势线以紧凑格式传达趋势方向(上升、下降、尖峰),LLM 能很好地解析;原始时间戳表需要心智建模。
- 将凭证存储在 ~/.promql-cli.yaml 和 ~/.promqltoken 中,权限设为 600——将令牌作为 CLI 参数传递会将其暴露在 shell 历史和进程列表中。
本技能并非详尽无遗。请参考官方 promql-cli 文档和示例获取最新信息。Context7 可作为发现平台提供帮助。
如果您在 promql-cli 本身中遇到错误或意外行为,请在 https://github.com/nalbury/promql-cli/issues 提交问题。