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

如何为Artillery metrics-by-endpoint插件动态命名POST请求?

问题:Artillery中loop内请求name无法插值$loopElement导致metrics-by-endpoint指标命名异常

我希望使用Artillery、payload及metrics-by-endpoint插件跟踪每个payload的指标,但场景中loop的name字段无法对$loopElement进行插值。当前生成的指标摘要中仍保留{{ $loopElement.sample_file }}占位符,无法按预期以payload中的sample_file值(如input.1.json)为每个POST请求命名并生成独立的指标统计。

实际生成的指标摘要

plugins.metrics-by-endpoint.response_time.{{ $loopElement.sample_file }}:
  min: ......................................................................... 38
  max: ......................................................................... 40
  mean: ........................................................................ 39
  median: ...................................................................... 37.7
  p95: ......................................................................... 37.7
  p99: ......................................................................... 37.7
plugins.metrics-by-endpoint.{{ $loopElement.sample_file }}.codes.200: .......... 2

期望的指标摘要

http.responses: ................................................................ 2
plugins.metrics-by-endpoint.input.1.json.......................................  1
plugins.metrics-by-endpoint.input.2.json.......................................  1
plugins.metrics-by-endpoint.response_time.input.1.json:
  min: ......................................................................... 46
  max: ......................................................................... 46
  mean: ........................................................................ 46
  median: ...................................................................... 46.1
  p95: ......................................................................... 46.1
  p99: ......................................................................... 46.1
plugins.metrics-by-endpoint.response_time.input.2.json:
  min: ......................................................................... 42
  max: ......................................................................... 42
  mean: ........................................................................ 42
  median: ...................................................................... 41.7
  p95: ......................................................................... 41.7
  p99: ......................................................................... 41.7

相关配置文件

artillery.yaml

config:
  environments:
    ci:
      target: https://URL.host
    local:
      target: http://localhost:8080
  payload:
  - fields:
    - sample_file
    - json_body
    loadAll: true
    name: json_bodies
    path: payload.csv
  plugins:
    expect:
      reportFailuresAsErrors: true
    metrics-by-endpoint:
      useOnlyRequestNames: true
    publish-metrics:
    - apiKey: '{{ $env.DATADOG_API_KEY }}'
      appKey: '{{ $env.DATADOG_APP_KEY }}'
      event:
        priority: normal
        tags: &id001
        - service:artilleryio
        - env:ci
        - version:2.0.26
        title: Plugin integration test
      tags: *id001
      type: datadog
scenarios:
- flow:
  - loop:
    - post:
        body: '{{ $loopElement.json_body }}'
        headers:
          Content-Type: application/json
        name: '{{ $loopElement.sample_file }}'
        url: /api/decision
    over: json_bodies

payload.csv

input.1.json,"{""key"":""value1""}"
input.2.json,"{""key"":""value2""}"

解决方案

Artillery的loop块内直接在name字段使用$loopElement插值存在时机问题:插件在解析配置阶段会读取name的原始字符串,而$loopElement的插值是在测试运行时才执行的,导致插件无法识别动态生成的请求名称。

方法1:通过set步骤中转变量

先将$loopElement.sample_file存入一个临时变量,再在请求的name中引用该变量,确保插值在插件读取名称前完成:

scenarios:
- flow:
  - loop:
    - set:
        variable: current_sample
        value: '{{ $loopElement.sample_file }}'
    - post:
        body: '{{ $loopElement.json_body }}'
        headers:
          Content-Type: application/json
        name: '{{ current_sample }}'
        url: /api/decision
    over: json_bodies

方法2:启用插件的运行时插值支持

针对Artillery v2及以上版本的metrics-by-endpoint插件,可直接在配置中添加enableNameInterpolation: true,让插件支持运行时解析请求名称的插值:

plugins:
  # 保留其他插件配置
  metrics-by-endpoint:
    useOnlyRequestNames: true
    enableNameInterpolation: true # 新增配置项

两种方法都能让指标按每个payload的sample_file值生成独立统计,匹配你期望的格式。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 03:08:13