0Pricing
Load Testing & Performance Benchmarking (JMeter & k6) · Lesson

Custom Metrics and Trends in k6

Define your own Counter, Gauge, Rate, and Trend metrics in k6 to measure exactly what matters for your advanced scenarios.

Custom Metrics and Trends in k6 is a free Load Testing & Performance Benchmarking (JMeter & k6) lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Load Testing & Performance Benchmarking (JMeter & k6) learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Beyond Built-in Metrics

k6 ships with built-in metrics like http_req_duration, but advanced tests often need to measure domain-specific values: items in a cart, business transaction time, or a custom error rate. k6 lets you create custom metrics.

The Four Metric Types

k6 offers four custom metric types from k6/metrics:

  • Counter — cumulative sum.
  • Gauge — last recorded value.
  • Rate — percentage of non-zero/true values.
  • Trend — statistics like min, max, avg, p95.

Importing Metric Constructors

Import the constructors you need and create the metric objects in module scope so they are shared across iterations.

import { Counter, Trend, Rate, Gauge } from 'k6/metrics';

A Counter Example

A Counter accumulates a value over the whole test. Here we count how many orders were created.

const ordersCreated = new Counter('orders_created');

export default function () {
  ordersCreated.add(1);
}

A Trend for Timing

A Trend collects a series of numbers and reports statistics. It is ideal for timing custom logic.

import http from 'k6/http';
const loginTime = new Trend('login_time');

export default function () {
  const res = http.post('https://test.k6.io/login');
  loginTime.add(res.timings.duration);
}

A Rate for Success Ratio

A Rate tracks how often a condition is true. Add true or false and k6 reports the percentage of trues.

const successRate = new Rate('success_rate');
successRate.add(res.status === 200);

A Gauge for Last Value

A Gauge keeps only the most recent value you add. Use it for things like the current queue length reported by an endpoint.

const queueDepth = new Gauge('queue_depth');
queueDepth.add(42);

Tagging Custom Metrics

Just like built-in metrics, you can attach tags when adding a value. This lets you slice your custom metric by endpoint or feature.

loginTime.add(res.timings.duration, { region: 'eu' });

Thresholds on Custom Metrics

Custom metrics integrate fully with thresholds. You can fail a run if your business metric crosses a limit.

export const options = {
  thresholds: {
    login_time: ['p(95)<500'],
    success_rate: ['rate>0.98'],
  },
};

Reading Custom Metrics in Summary

At the end of a run, custom metrics appear in the end-of-test summary alongside built-in ones, showing the appropriate statistics for their type.

k6 run scenario.js

Choosing the Right Type

Picking the correct metric type matters: use a Trend for durations, a Rate for success ratios, a Counter for totals, and a Gauge for snapshot values. The wrong type gives misleading summaries.

Quick Check

Pick the right metric type for the job.

Recap

You can now extend k6 with custom metrics.

  • Counter sums, Gauge keeps the last value, Rate tracks ratios, Trend reports stats.
  • Add tags and thresholds to custom metrics for targeted SLOs.
  • They appear in the summary and any output backend.

Frequently asked questions

Is the “Custom Metrics and Trends in k6” lesson free?

Yes — the full text of “Custom Metrics and Trends in k6” is free to read here on the web, and the Load Testing & Performance Benchmarking (JMeter & k6) course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Load Testing & Performance Benchmarking (JMeter & k6) course, upgrade to CoddyKit PRO.

What will I learn in “Custom Metrics and Trends in k6”?

Define your own Counter, Gauge, Rate, and Trend metrics in k6 to measure exactly what matters for your advanced scenarios. You practise Load Testing & Performance Benchmarking (JMeter & k6) with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Load Testing & Performance Benchmarking (JMeter & k6)?

No prior experience is required. Load Testing & Performance Benchmarking (JMeter & k6) on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Custom Metrics and Trends in k6” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Load Testing & Performance Benchmarking (JMeter & k6) lesson?

Yes. Every Load Testing & Performance Benchmarking (JMeter & k6) lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Virtual User Scenarios (VUs)
  2. Data Parameterization in k6
  3. Cloud Execution with k6
  4. Custom Metrics and Trends in k6
← Back to Load Testing & Performance Benchmarking (JMeter & k6)