k6

How to display Error per second data on Grafana with k6?

I have run k6 with InfluxDB and Grafana , but I haven’t seen Error per second graph been changed. How can I do to push data into this graph? Please help me

Hi,

the “Errors Per Second” graph in the “k6 Load Testing Results” dashboard (https://grafana.com/grafana/dashboards/2587) is configured to read from the “errors” metric. See the screenshot:

In order to emit this metric you can create a custom one in your JS script to track what you want, like HTTP response errors. For example:

import http from "k6/http";
import { check } from "k6";
import { Rate } from "k6/metrics";

export let options = {
    vus: 5,
    iterations: 100,
};

export let errorRate = new Rate("errors");

export default function() {
    let res = http.get("https://httpbin.org/status/200,400")
    let success = check(res, {"is status 200": (r) => r.status === 200});
    errorRate.add(!success);
};

If you change the “errors” name (e.g. new Rate("my-errors")), then you’ll need to update the default graph source or create a new graph to read from it.

Hope this helps,

Ivan

1 Like

@imiric , Hi Ivan
I have tried your solution but I have a problem with this : check(res, { "200": (r) => r.status == 200 }); sleep(1); let error_value=check(res,{ "400": (r) => r.status == 400, }) errorRate.add(error_value)
When I have configs this , in error_rate this display both 200 and 400 not only 400. How can I configs only 400 rate.
Thank Ivan

Hi @ginglove,

my apologies, I think my original example was incorrect. I updated it by changing to:

    errorRate.add(!success);

But still, there’s a problem with the InfluxDB query used by the “Errors Per Second” graph in that Grafana dashboard, as it hasn’t been updated in 2 years, and I think k6 stopped using the “errors” metric internally since then, so the intended functionality might have changed.

In order to display “Errors Per Second” correctly with the above example to exclude non-200 responses, click on “Edit” in the graph dropdown (on title hover), and there you need to change the query used to the following:

SELECT count("value") FROM "errors" WHERE "value"=1 and $timeFilter GROUP BY time($__interval) fill(none)

I’m not sure how to input this in the GUI query builder, so click on the hamburger menu on the right of the query and then on “Toggle Edit Mode”, where you can copy/paste the full query.

The change adds an additional "value"=1 filter to the WHERE clause.

Let me know if this works for you. If so, I’ll see if we can fix this in the public dashboard.

Alternatively, you can avoid changing the graph, and instead only conditionally add values to the “errors” rate. For example:

if (!success) {
    errorRate.add(1)
}

This would fix the graph issue, but it goes against the documented way of using checks and custom rates, so the fix should be done on the dashboard instead. And keep in mind that you can use custom metric names and graphs, so you can change your dashboards to suit your scripts.

1 Like