How can I make a dropped_iterations_percentage metric?

I would like to make a dropped_iterations_percentage metric. This metric should be a Rate, and it should be a percentage of dropped iterations over the total number of iterations.

The reasoning for this is we run a suite of k6 tests in a pipeline, and very occasionally tests will drop some iterations for tests. The tests have a wide variety of VU counts, total iterations, etc. Therefore, we want to define a threshold for all tests so that if only a small percentage of iterations is dropped the test will not fail.

The only way I could think of doing this is in the handleSummary method since it is only there that the iterations and dropped_iterations metrics are available in code. I could manually calculate the percentage and manually add the metric to the summary output, however if the threshold is exceeded the test still exits with a 0 code. I wasn’t able to find a way to make it exit without a 0.

Is there any way to go about making this metric? Thank you in advance.

Hello @sr120av, welcome to the community forum!

As I see, you can’t do this out of the box, because:

  • you cant exit with errorcode from handleSummary(), because k6 catches all the errors, and just logs it.
  • thresholds are already evaluated by the time of calling handleSummary()
  • you can’t access the values of the metrics before handleSummary()

But you can write an extension like this ( , and you can workaround the problem with it

1 Like