Hi folks, In this blog, we will discuss and explore more about tags and groups in our favorite performance testing tool, Grafana K6. However, you may be asking what the circumstances are and why we use them. So let’s start with some core fundamentals of tags and groups.
A load test is frequently performed on a service that has multiple subsystems and resources. This makes it difficult to determine the problems that are causing poor performance. So, to avoid this type of problem, k6 introduces tags and groups in performance testing.
Tags categorize your checks, thresholds, custom metrics, and requests for in-depth filtering, where as Groups let you apply tags to the script’s functions.
Tags in k6
They are a simple and effective way to categorize k6 objects for subsequent filtering of results.
k6 provides two types of tags:
- User-defined =>
the ones you added when writing the script, and
2. System =>
automatically assigned by k6.
System tags in k6
The list of automatically created tags includes these:
- Proto tag
proto | The protocol used (for example, HTTP/1.1) |
- Subproto tag
subproto | The subprotocol (used by web sockets) |
- Status tag
status | The HTTP status code (for example, 200, 404, etc.) |
- Method tag
method | the HTTP method name (e.g. GET, POST, etc.) or the RPC method for gRPC |
- URL tag
url | URL of the HTTP request |
- Name tag
name | HTTP request name |
- Group tag
group | the full group path |
- Check tag
check | the Check name |
- Error tag
error | a string with an error message other than HTTP (for example, a network or DNS error) |
- Error_code tag
error_code | a number that is unique for different error types |
- Tls_version tag
tls_version | the TLS version |
- Scenario tag
scenario | the name of the scenario in which the metric was emitted |
- Service tag
service | name of the RPC service for gRPC |
User-defined tags
Custom tags allow you to classify K6 objects based on logic. You can tag the following entities:
- Requests
- Checks
- Thresholds
- Custom metrics
const my_Trend_type = new Trend("trend_metrices");
export let options = {
thresholds: {
"http_req_duration": ["p(90)<1000"]
},
stages: [
{ duration: '2s', target: 20 }, // simulate ramp-up of traffic from 1
to 20 users over 2 seconds.
{ duration: '5s', target: 20 }, // stay at 20 users for 5 seconds.
{ duration: '2s', target: 0 }, // ramp-down to 0 users.
]
};
export default function () {
// Add tag to request metric data
const res = http.get("http://httpbin.org/", {
tags: {
my_tag: "I'm a tag",
},
});
// Add tag to check
check(res, { 'status is 200': (r) => r.status === 200 }, { my_tag: "I'm a tag" });
// Add tag to custom metric
my_Trend_type.add(res.timings.connecting, { my_tag: "I'm a tag"});
}

Groups in k6
Groups are used to organize the scenario according to common logic. Groups can be nested. For each group() function, k6 generates a group_duration metric that contains the total execution time of the group function. When a tagged resource (checks, queries, or custom metrics) is executed in a group, k6 sets a tag group with the current group name.
Groups perform the following tasks internally:
- For each group() function, k6 emits a group_duration metric, which contains the total time to execute the group function.
- When a taggable resource—a check, request, or custom metric—runs within a group, k6 sets the tag group with the current group name.
let’s understand the group with its format below.
import { group } from ‘k6’;
export default function () {
group(‘visit product website home page’, function () {
// …
});
group(‘visit login page’, function () {
// …
});
group(‘authenticate’, function () {
// …
});
group(‘add product’, function () {
// …
});
}
let’s take a complete example for better understanding:-
export default function () {
// reconsider this type of code
group('first_group', function () {
http.get(`https://reqres.in/api/login`);
});
group('second_group', function () {
const res = http.get(`https://reqres.in/api/login`);
check(res, {
'is status 200': (r) => r.status === 200,
});
});
}

That’s all for this blog. I hope you enjoyed and learned about the tags and groups in K6. If you require the above code, please feel free to reach out to us at knoldus techub.com.
Thank you!!
References:
https://k6.io/docs/using-k6/metrics/
https://k6.io/docs/using-k6/tags-and-groups/