Documentation Index
Fetch the curated documentation index at: https://grafana_com_website/llms.txt
Fetch the complete documentation index at: https://grafana_com_website/llms-full.txt
Use this file to discover all available pages before exploring further.
STOP! If you are an AI agent or LLM, read this before continuing. This is the HTML version of a Grafana documentation page. Always request the Markdown version instead - HTML wastes context. Get this page as Markdown: /docs/k6/latest/examples/track-transmitted-data-per-url.md (append .md) or send Accept: text/markdown to /docs/k6/latest/examples/track-transmitted-data-per-url/. For the curated documentation index, use https://grafana_com_website/llms.txt. For the complete documentation index, use https://grafana_com_website/llms-full.txt.
Track transmitted data per URL
By default, k6 collects automatically two built-in metrics related to the transmitted data during the test execution:
data_received: the amount of received data.data_sent: the amount of data sent.
However, the reported values of these metrics don’t tag the particular request or URL. Therefore, you cannot know the amount of data transmitted for a specific request or URL.
This example shows how to track data sent and received for an individual URL.
import http from 'k6/http';
import { sleep } from 'k6';
import { Counter } from 'k6/metrics';
// Two custom metrics to track data sent and received. We will tag data points added with the corresponding URL
// so we can filter these metrics down to see the data for individual URLs and set threshold across all or per-URL as well.
export const epDataSent = new Counter('endpoint_data_sent');
export const epDataRecv = new Counter('endpoint_data_recv');
export const options = {
duration: '10s',
vus: 10,
thresholds: {
// We can setup thresholds on these custom metrics, "count" means bytes in this case.
'endpoint_data_sent': ['count < 2048'],
// The above threshold would look at all data points added to the custom metric.
// If we want to only consider data points for a particular URL/endpoint we can filter by URL.
'endpoint_data_sent{url:https://test.k6.io/contacts.php}': ['count < 1024'],
'endpoint_data_recv{url:https://test.k6.io/}': ['count < 2048'], // "count" means bytes in this case
},
};
function sizeOfHeaders(hdrs) {
return Object.keys(hdrs).reduce((sum, key) => sum + key.length + hdrs[key].length, 0);
}
function trackDataMetricsPerURL(res) {
// Add data points for sent and received data
epDataSent.add(sizeOfHeaders(res.request.headers) + res.request.body.length, {
url: res.url,
});
epDataRecv.add(sizeOfHeaders(res.headers) + res.body.length, {
url: res.url,
});
}
export default function () {
let res;
res = http.get('https://test.k6.io/');
trackDataMetricsPerURL(res);
res = http.get('https://test.k6.io/contacts.php');
trackDataMetricsPerURL(res);
sleep(1);
}Was this page helpful?
Related resources from Grafana Labs

