在发送给 Datadog 的 k6 指标中包含测试运行 ID

问题描述

我在本地机器上使用 k6 执行负载测试,并使用 Datadog agent 在 Datadog 中可视化指标。

我想在 Datadog 中过滤 k6 指标,因为测试无法区分。

此时 $test_run_id显示 *(请参阅下面的屏幕截图):

enter image description here

我按照 this the official doc 建议在 k6 配置中将 include_test_run_id 标志设置为 true,但我没有成功。

这是我目前使用的 k6 配置(<YOUR_DATADOG_API_KEY> 替换为实际的 Datadog API 密钥):

export const options = {
  vus: 5,duration: "10s",noConnectionReuse: true,ext: {
    loadimpact: {
      apm: [
        {
          provider: "datadog",api_key: "<YOUR_DATADOG_API_KEY>",include_test_run_id: true
        }
    ]
    }
  }
};

解决方法

您将 DataDog 配置用于商业 k6 Cloud service (k6 cloud),而不是本地运行的 k6 测试 (k6 run)。 test_run_id 是云服务中的一个概念,尽管它也很容易在本地进行模拟以区分测试运行。

对于本地测试,您应该通过使用 k6 run --out datadog script.js 运行 k6 来启用 D​​ataDog 输出。我假设您这样做了,否则您将不会在 DataDog 中看到任何指标。

然后,您可以使用 tags option 为特定 k6 运行生成的所有指标注入唯一的额外标签,以便您可以在 DataDog 中区分它们。例如:

k6 run --out datadog --tag test_run_id=1 script.js
k6 run --out datadog --tag test_run_id=2 script.js
k6 run --out datadog --tag test_run_id=3 script.js
...

当然,您可以选择任何 key=value 组合,您不限于 test_run_id