How can I capture only the responses that returned an error and import them into a json file?

In the example below the results variable in handlesummary is always as [ ]

import http from 'k6/http';
import { check } from 'k6';
import { sleep } from 'k6';
import { randomItem } from 'https://jslib.k6.io/k6-utils/1.2.0/index.js';
import { Trend } from 'k6/metrics';

const headers = {
  'Content-Type': 'application/json',
};

let email = ["YW5hLmxvcGVzQGptZWxsb3NhdWRlLnB0", "YW5hLmxvcGVzQGptZWxsb3NhdWRlLnB0", "YW5hLmxvcGVzQGptZWxsb3NhdWRlLnB0","4", "YW5hLmxvcGVzQGptZWxsb3NhdWRlLnB0","YW5hLmxvcGVzQGptZWxsb3NhdWRlLnB0"]
const randomEmail = randomItem(email);

const query = `
query{
  collaboratorByEmail(email: "${randomEmail}") { collaborator { email },  jwt }}`;

let results = {}

export default function () {
  const res = http.post("http://localhost:5000/V1",
  JSON.stringify({ query }),
  { headers },);

  if(res.status !==200)
    results =  { request: res.headers, status: res.status };
    
  check(res, { 'status is 200': (r) => r.status === 200 });

  sleep(1); // Adicionei um sleep para não sobrecarregar o servidor
}


export function handleSummary(data) {
  console.log(data) 
  return {
    './results.html': htmlReport(data, { testName: 'My Test Suite' }),
    './results.xml': textSummary(data, { testName: 'My Test Suite', indent: ' ', enableColors: true }),
    './summary.json': JSON.stringify(data),
    stdout: textSummary(data, { indent: ' ', enableColors: true })
  };
}

Hi @JoaoPC !

Welcome to the community forums! :wave:

Unfortunately, out of the box, there is no way of doing that in k6. We know that such a need exists, and we even have an open issue on our GitHub `--http-debug=error` to log only failures · Issue #2163 · grafana/k6 · GitHub (which you could upvote if you want :sweat_smile: ).

However, for sure, some workarounds are possible. You could combine the logs and file output that later filter outside the k6 with grep util, ’ jq`, or whatever you chose.

Let’s say we have a script:

import http from 'k6/http';

const urls = [
   'https://httpbin.test.k6.io/status/200',
   'https://httpbin.test.k6.io/status/418', // this should respond with status 418
]

export default function () {
   console.log("just a normal log message");

   for (let i = 0; i < urls.length; i++) {
      let res = http.get(urls[i]);

      if (res.status > 300) {
         console.error("HTTP REQUEST FAILED: ", JSON.stringify({ url: urls[i], status: res.status}));
      }
   }   
}

So running it with the following flags:

$ k6 run --log-format=json --log-output=file=all.logs script.js

Will produce the all.logs file

And using something external (like grep/jq/whatever), you could filter that:

$ grep "HTTP REQUEST FAILED" all.logs
{"level":"error","msg":"HTTP REQUEST FAILED:  {\"url\":\"https://httpbin.test.k6.io/status/418\",\"status\":418}","source":"console","time":"2023-03-23T17:17:17+01:00"}

I hope that helps,
Cheers!

1 Like

HI! @olegbespalov!
I saw this solution in another theme in the community, and I followed up with this idea, but one doubt, can you tell me if there is any way to do it like we do with handleSummary?

handleSummary serves other needs its data includes information about the test run’s time and all built-in and custom metrics (including checks).

And yeah, there is no similar to it way, at least for now :frowning_face: