如何修改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记录规则为指标添加单位标签
- 创建一个规则文件(比如
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等
- 修改Prometheus配置文件,启用该规则文件:
rule_files: - "k6_metrics_units.yml"
- 重启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
相关产品推荐
相关产品推荐

