lysov
lysov

Reputation: 160

Include test run id in k6 metrics sent to Datadog

I use k6 on my local machine to perform load-testing as well as a Datadog agent to visualize the metrics in Datadog.

I'd like to filter k6 metrics in Datadog as the tests aren't distinguishable.

At this point the $test_run_id only shows * (refer to the screenshot below): enter image description here

I followed this the official doc that suggests to set include_test_run_id flag to true in k6 config, but I was unsuccessful.

Here's a k6 config I currently use (<YOUR_DATADOG_API_KEY> is replaced with an actual Datadog API key):

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
        }
    ]
    }
  }
};

Upvotes: 2

Views: 2699

Answers (2)

Tacio Degrazia
Tacio Degrazia

Reputation: 65

One way to use the test_run_id inside the script is by adding it as tags inside the options. Ex:

export const options = {
    
  tags: {
    test_run_id: 'my-test-run-id',
  },
};

Upvotes: 0

na--
na--

Reputation: 1106

You are using the DataDog configuration for the commercial k6 Cloud service (k6 cloud), not locally run k6 tests (k6 run). test_run_id is a concept in the cloud service, though it's also easy to emulate locally as a way to distinguish between test runs.

For local tests, you should enable the DataDog output by running k6 with k6 run --out datadog script.js. I assume you did that, otherwise you wouldn't see any metrics in DataDog.

Then, you can use the tags option to inject a unique extra tag for all metrics generated by a particular k6 run, so you can differentiate them in DataDog. For example:

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
...

Of course, you can choose any key=value combination, you are not restricted to test_run_id.

Upvotes: 6

Related Questions