How to Gate CI on API Response Times
Tagging requests by endpoint and scoping thresholds to those tags gives a per-endpoint latency gate instead of one blended number.
Tag each request with a name, then declare a threshold scoped to that tag such as http_req_duration{name:orders}: p(95)<300. Each endpoint gates independently.
Steps
- Tag requests with a stable
nameper endpoint. - Declare thresholds scoped to each tag.
- A breach on any one endpoint fails the run.
Scoped thresholds
k6-script.js
import http from 'k6/http';
export const options = {
thresholds: {
'http_req_duration{name:health}': ['p(95)<150'],
'http_req_duration{name:orders}': ['p(95)<300'],
},
};
export default function () {
http.get(`${__ENV.BASE_URL}/health`, { tags: { name: 'health' } });
http.get(`${__ENV.BASE_URL}/api/orders`, { tags: { name: 'orders' } });
}Gotchas
- Use a static
nametag; a URL with an id creates a new metric series per request and no gate works. - Group similar endpoints so the threshold count stays manageable.
Related guides
How to Configure k6 Thresholds as a CI GateConfigure k6 thresholds so specific metrics like p95 latency and error rate abort the run and fail CI, with a…
How to Enforce a Performance Budget in CIEnforce a performance budget in CI by failing the job when p95 latency exceeds a limit, expressed as a thresh…