Advanced Thresholds and Custom Metrics

Thresholds and custom metrics are k6’s way of turning raw measurements into enforceable quality gates. Advanced usage lets you define thresholds on specific tags or custom metrics that represent business-level conditions.

Thresholds on Built-In and Tag-Filtered Metrics

You can define thresholds not only on global metrics like http_req_duration but also on metrics filtered by tags, such as a particular endpoint or scenario. This enables fine-grained gates, like requiring faster responses on checkout than on less critical pages.

export const options = {
  thresholds: {
    http_req_duration: ['p(95)<800'],
    'http_req_duration{journey:checkout}': ['p(95)<600'],
    checks: ['rate>0.99'],
  },
};
Note: Threshold names support tag selectors in braces, letting you target subsets of traffic.

Custom Metrics and Business-Level Thresholds

k6 supports custom counters, gauges, trends and rates that you can update from scripts to represent domain-specific events. You can then define thresholds on these custom metrics, such as the rate of failed logins or the average time for a composite transaction.

import { Trend } from 'k6/metrics';

const checkoutTime = new Trend('checkout_time');

export const options = {
  thresholds: {
    checkout_time: ['p(95)<1200'],
  },
};

export function checkoutFlow() {
  const start = Date.now();
  // ... perform multiple HTTP calls ...
  const elapsed = Date.now() - start;
  checkoutTime.add(elapsed);
}
Tip: Design custom metrics around things your stakeholders care about, such as β€œtime to place order” or β€œsuccessful payment rate.”
Warning: Defining too many custom metrics with fine-grained tags can increase storage and processing costs in your monitoring backend.

Advanced thresholds and custom metrics make performance gates more expressive and aligned with business goals.

Common Mistakes

Mistake 1 β€” Using only a single global threshold

This hides critical hotspots.

❌ Wrong: Treating all endpoints as equally important.

βœ… Correct: Apply stricter thresholds on high-impact journeys.

Mistake 2 β€” Creating custom metrics with no clear consumer

This adds noise.

❌ Wrong: Tracking everything just because it is possible.

βœ… Correct: Define metrics that support specific questions or decisions.

🧠 Test Yourself

How do advanced thresholds and custom metrics improve k6 test suites?