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

如何修改Prometheus Y轴单位以显示k6生成的毫秒指标

让Prometheus可视化界面Y轴显示k6指标的毫秒单位方法

当前配置与执行命令

Prometheus配置文件

global:
  scrape_interval: 3s # Set the scrape interval to every 3 seconds. Default is every 1 minute.
  evaluation_interval: 5s # Evaluate rules every 5 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
    - static_configs:
        - targets:
          # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: "prometheus"

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ["localhost:9090"]

Prometheus启动命令

prometheus --config.file=/etc/prometheus/prometheus.yml --web.enable-remote-write-receiver --enable-feature=native-histograms

K6性能测试命令

k6 run -o experimental-prometheus-rw  dist/get-all-node-requests-test.js

具体修改方法

方法1:在可视化工具中直接设置Y轴单位(最快捷)

如果用Grafana等工具展示指标,在面板的Y轴设置项里,直接选择单位为Milliseconds (ms),即可强制Y轴显示毫秒单位,无需修改Prometheus配置或指标。

方法2:通过Prometheus记录规则为指标添加单位标签

  1. 创建一个规则文件(比如k6_metrics_units.yml),内容如下:
groups:
- name: k6_metrics_units
  rules:
  - record: http_req_duration_ms
    expr: http_req_duration{job="k6"}
    labels:
      unit: "ms"
  # 可根据需要添加其他k6指标的规则,如http_req_blocked等
  1. 修改Prometheus配置文件,启用该规则文件:
rule_files:
  - "k6_metrics_units.yml"
  1. 重启Prometheus生效,之后查询http_req_duration_ms时,指标会带有unit="ms"标签,可视化工具可识别该标签并自动显示单位。

方法3:在PromQL中显式标注单位(适用于Prometheus自带UI)

在Prometheus表达式浏览器中,使用别名的方式标注单位,例如:

http_req_duration{job="k6"} as "请求耗时 (ms)"

或者添加注释说明:

http_req_duration{job="k6"} /* 单位:ms */

这样查询结果的图表Y轴会显示你设置的单位信息。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 04:52:18