Custom Metrics
The built-in metrics tell you about requests. They do not know your business. A custom metric lets you count or time the thing that matters to you, such as a failed login or the time of one step.
5 min read5 মিনিটে পড়া যায় · 6 sections6টি সেকশন · 8 sources8টি সোর্স · updated Nov 2026আপডেট: Nov 2026
Four metrics, four shapesচারটা মেট্রিক, চার রকম আকার
Feed the same stream of requests into Counter, Gauge, Rate and Trend and see what each reports.একই অনুরোধের স্রোত Counter, Gauge, Rate আর Trend-এ দিন আর দেখুন প্রতিটা কী জানায়।
You’ll learnআপনি শিখবেন
- What Counter, Gauge, Rate and Trend reportCounter, Gauge, Rate আর Trend কী জানায়
- That a Rate goes from 0.00 to 1.00Rate যে 0.00 থেকে 1.00 পর্যন্ত যায়
- Which threshold variable fits each typeকোন ধরনের জন্য কোন threshold variable
- Why one failed threshold fails the testএকটা threshold ব্যর্থ হলে কেন টেস্ট ব্যর্থ
Why you need custom metrics
k6 has built-in metrics for HTTP, such as http_req_duration, which gives the time of one request Source 7সোর্স 7. A custom metric measures something that you define. The module k6/metrics creates custom metrics of several types Source 6সোর্স 6.
Here are two examples:
- The time of one business step, such as "log in, then open the pizza list". One step can use several requests. The built-in
group_durationtimes a group Source 8সোর্স 8 anditeration_durationtimes a full iteration Source 7সোর্স 7. In the tutorial's own view, a customTrendis simpler when you want one named number for one step. - The count of failed logins. A login can return a status that the test treats as a failure, and you want a number for it.
This is the tutorial's own view: add a custom metric when you want a threshold or a chart for a number that the built-in metrics do not give.
সহজ English
k6 has built-in metrics for HTTP, such as http_req_duration, which gives the time of one request Source 7সোর্স 7. A custom metric measures something that you choose. The module k6/metrics makes custom metrics Source 6সোর্স 6.
Here are two examples:
- The time of one business step, such as "log in, then open the pizza list". One step can use several requests.
- The count of failed logins.
This is the tutorial's own view: add a custom metric when you want a threshold or a chart for a number that the built-in metrics do not give.
বাংলা ব্যাখ্যা
k6-এ HTTP-র জন্য built-in metric আছে, যেমন http_req_duration, যা একটা request-এর সময় দেয় Source 7সোর্স 7। Custom metric এমন কিছু মাপে যা আপনি ঠিক করেন। k6/metrics module custom metric বানায় Source 6সোর্স 6।
দুটো উদাহরণ:
- একটা business step-এর সময়, যেমন "login করে pizza list খোলা"। একটা step-এ কয়েকটা request থাকতে পারে। built-in
group_durationএকটা group-এর সময় মাপে Source 8সোর্স 8, আরiteration_durationপুরো একটা iteration-এর সময় মাপে Source 7সোর্স 7। টিউটোরিয়ালের নিজের মত: একটা step-এর জন্য একটা নামওয়ালা সংখ্যা চাইলে customTrendসহজ। - ফেইল হওয়া login-এর সংখ্যা।
এটা টিউটোরিয়ালের নিজের মত: built-in metric যে সংখ্যা দেয় না, তার জন্য threshold বা chart চাইলে custom metric যোগ করুন।
The four metric types
k6 has four custom metric types Source 6সোর্স 6:
| Type | What it does | Threshold variable |
|---|---|---|
Counter | Adds up the values Source 6সোর্স 6. | count or rate Source 2সোর্স 2 |
Gauge | Keeps the latest value. The summary also shows the min and max Source 3সোর্স 3 Source 6সোর্স 6. | value Source 3সোর্স 3 |
Rate | Gives the share of values that are not zero, from 0.00 to 1.00 Source 4সোর্স 4. | rate Source 4সোর্স 4 |
Trend | Gives the min, max, average, median, and percentiles Source 5সোর্স 5. | avg, min, max, med, p(N) Source 5সোর্স 5 |
All four types have the method add(value, [tags]) Source 2সোর্স 2 Source 3সোর্স 3 Source 4সোর্স 4 Source 5সোর্স 5. Gauge and Trend also take a second constructor argument, isTime. If it is true, k6 treats the values as time values. If it is false or missing, the values have no type Source 3সোর্স 3 Source 5সোর্স 5.
A Rate takes true, false, 1, or 0. Any non-zero value counts as a hit Source 4সোর্স 4.
সহজ English
k6 has four custom metric types Source 6সোর্স 6:
| Type | What it does | Threshold variable |
|---|---|---|
Counter | Adds up the values Source 6সোর্স 6. | count or rate Source 2সোর্স 2 |
Gauge | Keeps the latest value. The summary also shows the min and max Source 3সোর্স 3 Source 6সোর্স 6. | value Source 3সোর্স 3 |
Rate | Gives the share of values that are not zero, from 0.00 to 1.00 Source 4সোর্স 4. | rate Source 4সোর্স 4 |
Trend | Gives the min, max, average, median, and percentiles Source 5সোর্স 5. | avg, min, max, med, p(N) Source 5সোর্স 5 |
All four types have the method add(value, [tags]) Source 2সোর্স 2 Source 3সোর্স 3 Source 4সোর্স 4 Source 5সোর্স 5. Gauge and Trend also take a second argument, isTime. If it is true, k6 treats the values as time values. If it is false or missing, the values have no type Source 3সোর্স 3 Source 5সোর্স 5.
A Rate takes true, false, 1, or 0. Any value that is not zero counts as a hit Source 4সোর্স 4.
বাংলা ব্যাখ্যা
k6-এ চার ধরনের custom metric আছে Source 6সোর্স 6:
| Type | কী করে | Threshold variable |
|---|---|---|
Counter | value যোগ করে যায় Source 6সোর্স 6। | count বা rate Source 2সোর্স 2 |
Gauge | সবশেষ value রাখে। summary-তে min আর max-ও দেখায় Source 3সোর্স 3 Source 6সোর্স 6। | value Source 3সোর্স 3 |
Rate | শূন্য নয় এমন value-র অনুপাত দেয়, ০.০০ থেকে ১.০০ Source 4সোর্স 4। | rate Source 4সোর্স 4 |
Trend | min, max, average, median আর percentile দেয় Source 5সোর্স 5। | avg, min, max, med, p(N) Source 5সোর্স 5 |
চারটা type-এরই add(value, [tags]) method আছে Source 2সোর্স 2 Source 3সোর্স 3 Source 4সোর্স 4 Source 5সোর্স 5। Gauge আর Trend দ্বিতীয় আর্গুমেন্ট হিসেবে isTime নেয়। এটা true হলে k6 value-গুলোকে সময় ধরে। false হলে বা না দিলে value-র কোনো type থাকে না Source 3সোর্স 3 Source 5সোর্স 5।
Rate নেয় true, false, 1 বা 0। শূন্য নয় এমন যেকোনো value একটা hit গোনা হয় Source 4সোর্স 4।
A script with all four
Create each metric in the init context, outside default Source 1সোর্স 1. Then call add in the VU code to take a measurement Source 1সোর্স 1. This script targets the public demo app quickpizza.grafana.com. It runs 2 VUs for 5 s.
import http from 'k6/http';
import { sleep } from 'k6';
import { Counter, Gauge, Rate, Trend } from 'k6/metrics';
// Create every custom metric in the init context.
const badStatus = new Counter('bad_status_count');
const bodySize = new Gauge('home_body_bytes');
const failedRate = new Rate('failed_requests');
const waiting = new Trend('home_waiting_time', true);
export const options = {
vus: 2,
duration: '5s',
thresholds: {
bad_status_count: ['count==0'],
home_body_bytes: ['value>0'],
failed_requests: ['rate<0.01'],
home_waiting_time: ['p(95)<1000'],
},
};
export default function () {
const res = http.get('https://quickpizza.grafana.com/');
const failed = res.status >= 400;
if (failed) {
badStatus.add(1, { page: 'home' });
}
bodySize.add(res.body.length);
failedRate.add(failed);
waiting.add(res.timings.waiting);
sleep(1);
}The { page: 'home' } object is a tag. You can add tags to any value. You can use them to filter results Source 1সোর্স 1. A tagged value also creates a sub-metric, and a threshold can name it, as in bad_status_count{page:home} Source 2সোর্স 2.
সহজ English
Create each metric at the top level of the script, outside default Source 1সোর্স 1. Then call add in the VU code to record a value Source 1সোর্স 1. This script tests the public demo app quickpizza.grafana.com. It runs 2 VUs for 5 s.
import http from 'k6/http';
import { sleep } from 'k6';
import { Counter, Gauge, Rate, Trend } from 'k6/metrics';
// Create every custom metric in the init context.
const badStatus = new Counter('bad_status_count');
const bodySize = new Gauge('home_body_bytes');
const failedRate = new Rate('failed_requests');
const waiting = new Trend('home_waiting_time', true);
export const options = {
vus: 2,
duration: '5s',
thresholds: {
bad_status_count: ['count==0'],
home_body_bytes: ['value>0'],
failed_requests: ['rate<0.01'],
home_waiting_time: ['p(95)<1000'],
},
};
export default function () {
const res = http.get('https://quickpizza.grafana.com/');
const failed = res.status >= 400;
if (failed) {
badStatus.add(1, { page: 'home' });
}
bodySize.add(res.body.length);
failedRate.add(failed);
waiting.add(res.timings.waiting);
sleep(1);
}The { page: 'home' } part is a tag. You can add tags to any value.
You can use tags to filter results Source 1সোর্স 1.
A tagged value also makes its own smaller metric. A threshold can name it, as in bad_status_count{page:home} Source 2সোর্স 2.
বাংলা ব্যাখ্যা
প্রতিটা metric init context-এ, default-এর বাইরে বানান Source 1সোর্স 1। এরপর VU code-এ add কল করে একটা measurement নিন Source 1সোর্স 1। এই script public demo app quickpizza.grafana.com-কে target করে। এটা ২টা VU নিয়ে ৫ s চলে।
import http from 'k6/http';
import { sleep } from 'k6';
import { Counter, Gauge, Rate, Trend } from 'k6/metrics';
// Create every custom metric in the init context.
const badStatus = new Counter('bad_status_count');
const bodySize = new Gauge('home_body_bytes');
const failedRate = new Rate('failed_requests');
const waiting = new Trend('home_waiting_time', true);
export const options = {
vus: 2,
duration: '5s',
thresholds: {
bad_status_count: ['count==0'],
home_body_bytes: ['value>0'],
failed_requests: ['rate<0.01'],
home_waiting_time: ['p(95)<1000'],
},
};
export default function () {
const res = http.get('https://quickpizza.grafana.com/');
const failed = res.status >= 400;
if (failed) {
badStatus.add(1, { page: 'home' });
}
bodySize.add(res.body.length);
failedRate.add(failed);
waiting.add(res.timings.waiting);
sleep(1);
}{ page: 'home' } একটা tag। যেকোনো value-তে tag যোগ করা যায়। tag দিয়ে result filter করা যায় Source 1সোর্স 1। tag দেওয়া value একটা sub-metric-ও বানায়। threshold তার নাম দিতে পারে, যেমন bad_status_count{page:home} Source 2সোর্স 2।
Thresholds on a custom metric
A threshold on a custom metric works like any other threshold. The key is the metric name. The expression uses the variable of the metric type Source 2সোর্স 2 Source 3সোর্স 3 Source 4সোর্স 4 Source 5সোর্স 5. The script above has one threshold for each type. See Thresholds for the full syntax.
Keep these points in mind:
- Trend values are in milliseconds, so
p(95)<1000means 1 s Source 5সোর্স 5. Passtrueas the second argument when the values are times Source 5সোর্স 5. - A
Rategoes from 0.00 to 1.00, sorate<0.01means less than 1% Source 4সোর্স 4. - Use percentiles for a
Trend. The k6 docs do not recommendminandmaxin thresholds, because they show outliers Source 5সোর্স 5. - k6 needs to know the metric before the test starts. This is one reason why you create metrics in the init context Source 1সোর্স 1.
If a threshold fails, the test fails, as with any threshold.
সহজ English
A threshold on a custom metric works like any other threshold. The key is the metric name. The expression uses the variable of the metric type Source 2সোর্স 2 Source 3সোর্স 3 Source 4সোর্স 4 Source 5সোর্স 5. The script above has one threshold for each type. See Thresholds for the full syntax.
Keep these points in mind:
- Trend values are in milliseconds, so
p(95)<1000means 1 s Source 5সোর্স 5. Passtrueas the second argument when the values are times Source 5সোর্স 5. - A
Rategoes from 0.00 to 1.00, sorate<0.01means less than 1% Source 4সোর্স 4. - Use percentiles for a
Trend. The k6 docs do not recommendminandmaxin thresholds, because they show outliers Source 5সোর্স 5. - k6 must know the metric before the test starts. This is one reason why you create metrics in the init context Source 1সোর্স 1.
If a threshold fails, the test fails, as with any threshold.
বাংলা ব্যাখ্যা
Custom metric-এর threshold অন্য threshold-এর মতোই কাজ করে। key হলো metric-এর নাম। expression-এ metric type-এর variable লাগে Source 2সোর্স 2 Source 3সোর্স 3 Source 4সোর্স 4 Source 5সোর্স 5। উপরের script-এ প্রতিটা type-এর জন্য একটা threshold আছে। পুরো syntax-এর জন্য Thresholds দেখুন।
কয়েকটা কথা মনে রাখুন:
- Trend-এর value millisecond-এ থাকে, তাই
p(95)<1000মানে ১ s Source 5সোর্স 5। value সময় হলে দ্বিতীয় আর্গুমেন্টেtrueদিন Source 5সোর্স 5। Rate০.০০ থেকে ১.০০ পর্যন্ত যায়, তাইrate<0.01মানে ১%-এর কম Source 4সোর্স 4।Trend-এ percentile ব্যবহার করুন। k6 docs threshold-এminআরmaxব্যবহার করতে বলে না, কারণ এগুলো outlier দেখায় Source 5সোর্স 5।- টেস্ট শুরুর আগে k6-কে metric জানতে হয়। তাই init context-এ metric বানাতে হয় Source 1সোর্স 1।
threshold fail করলে অন্য threshold-এর মতো টেস্টও fail করে।
Custom metrics in the summary
k6 prints custom metrics in the summary at the end of a test. They are also in the raw data points Source 1সোর্স 1. This is the output of one local run of the script above. The numbers change on each run.
█ TOTAL RESULTS
CUSTOM
bad_status_count...............: 0 0/s
failed_requests................: 0.00% 0 out of 8
home_body_bytes................: 2847 min=2847 max=2847
home_waiting_time..............: avg=353.85ms min=308.57ms med=322.38ms max=605.78ms p(90)=411.26ms p(95)=508.52msEach type has its own format in the summary. The Counter shows a total and a rate per second. The Rate shows a percentage and the counts. The Gauge shows the latest value with its min and max. The Trend shows its statistics, and ms appears because of isTime. All 4 thresholds passed, so k6 printed a check mark for each and exited with code 0. See Reading Results for how to read the full summary.
k6 collects custom metrics from the VUs only at the end of an iteration. In a long script, a custom metric can appear only after the test has run for a while Source 1সোর্স 1.
সহজ English
k6 prints custom metrics in the summary at the end of a test. They are also in the raw data points Source 1সোর্স 1. This is the output of one local run of the script above. The numbers change on each run.
█ TOTAL RESULTS
CUSTOM
bad_status_count...............: 0 0/s
failed_requests................: 0.00% 0 out of 8
home_body_bytes................: 2847 min=2847 max=2847
home_waiting_time..............: avg=353.85ms min=308.57ms med=322.38ms max=605.78ms p(90)=411.26ms p(95)=508.52msEach type has its own format in the summary. The Counter shows a total and a rate per second. The Rate shows a percentage and the counts. The Gauge shows the latest value with its min and max. The Trend shows its statistics, and ms appears because of isTime. All 4 thresholds passed, so k6 printed a check mark for each and exited with code 0. See Reading Results for how to read the full summary.
k6 collects custom metrics from the VUs only at the end of an iteration. In a long script, a custom metric can appear only after the test has run for a while Source 1সোর্স 1.
বাংলা ব্যাখ্যা
Custom metric end-of-test summary-তে আর granular data point-এ দেখা যায় Source 1সোর্স 1। নিচের output উপরের script-এর একটা local run-এর। প্রতিবার সংখ্যা বদলায়।
█ TOTAL RESULTS
CUSTOM
bad_status_count...............: 0 0/s
failed_requests................: 0.00% 0 out of 8
home_body_bytes................: 2847 min=2847 max=2847
home_waiting_time..............: avg=353.85ms min=308.57ms med=322.38ms max=605.78ms p(90)=411.26ms p(95)=508.52mssummary-তে প্রতিটা type-এর নিজের format আছে। Counter মোট সংখ্যা আর প্রতি সেকেন্ডের rate দেখায়। Rate শতাংশ আর গণনা দেখায়। Gauge সবশেষ value আর তার min-max দেখায়। Trend তার statistic দেখায়, আর isTime-এর কারণে ms দেখা যায়। ৪টা threshold-ই pass করেছে, তাই k6 প্রতিটার পাশে check mark দেখিয়েছে আর exit code 0 দিয়ে শেষ হয়েছে। পুরো summary কীভাবে পড়বেন তার জন্য Reading Results দেখুন।
k6 শুধু iteration শেষে VU থেকে custom metric সংগ্রহ করে। লম্বা script-এ custom metric কিছুক্ষণ পরে দেখা দিতে পারে Source 1সোর্স 1।
Key words
| Term | Meaning |
|---|---|
| Custom metric | A metric that you create with k6/metrics. |
Counter | A metric that adds up the values. |
Gauge | A metric that keeps the latest value. |
Rate | A metric for the share of non-zero values. |
Trend | A metric for statistics such as average and percentiles. |
add(value, [tags]) | The method that records one value. |
isTime | An option of Gauge and Trend: the values are times. |
| Init context | The code outside default, where you create metrics. |
| Sub-metric | A metric for one tag value, such as bad_status_count{page:home}. |