This seems like it was a script error that just aborted some iterations prematurely. This will not cause k6 to return a non-zero exit code, only crossed thresholds will. So I suggest wrapping your code in a try / catch block and if there’s an exception. Something like this:
import { Counter } from 'k6/metrics'
let failures = new Counter('failed_iterations');
export let options = {
thresholds: {
failed_iterations: ['count==0'],
}
};
export default function () {
try {
if (Math.random() < 0.5) {
console.log('throwing an error');
throw new Error('random script error');
}
} catch (e) {
failures.add(1);
}
};