[grafana] 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 (k6 Load Testing Results | Grafana Labs) 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

2 Likes

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

2 Likes

This works for me & I agree it needs to be updated in the public dashboard. Thanks very much!

For anyone trying this in 2022.
I found the best solution, if you’re looking for https errors, is to edit the grafana panel to read from the http_req_failed measurement instead.
You can make the query this:

select sum(value) from "http_req_failed" WHERE $timeFilter GROUP BY time($__interval), "status" fill(none)

Also updated the Alias by to $tag_status.

This way you will see the different error codes you’re getting and you don’t need to make a new measurement. Only downside is the 200 status code is always displayed as 0.
Hope this saves someone else a ton of time!

Example:

1 Like

@Inshaal93

As I remember the http_req_failed measurement logs HTTP status code 200 is also. It’s better to filter the results with status codes 4xx and 5xx for your solution.