K6 Counters
You set up k6 counters by defining a custom metric in the test init stage. Then you add values to that metric inside your virtual user loop. Teams use these metrics to count cumulative events like failed logins or cart checkouts. When test runners have broad permissions on target systems, high load can cause real damage. Unchecked scripts might flood databases with junk rows or trigger expensive third-party API calls. How k6 counters work in a load test A counter is a built-in metric type tha
Counters: the complete guide
Architecture patterns, implementation strategies, and security best practices. Delivered to your inbox.
You set up k6 counters by defining a custom metric in the test init stage. Then you add values to that metric inside your virtual user loop. Teams use these metrics to count cumulative events like failed logins or cart checkouts. When test runners have broad permissions on target systems, high load can cause real damage. Unchecked scripts might flood databases with junk rows or trigger expensive third-party API calls.
How k6 counters work in a load test
A counter is a built-in metric type that stores a running sum of values. You create the counter object once before the virtual users start their test runs. During the main test phase, each virtual user adds numbers to this shared metric. The tool aggregates these additions across all threads and reports the final total.
You can also attach metadata tags to each counter update during the run. Tags let your team split one counter into smaller groups for deeper analysis. For example, a single error counter can track failures by region or payment type. After the run finishes, the summary output displays both the total count and the rate.
Teams often pair these custom counters with pass or fail threshold rules. A threshold checks the final sum against a hard limit that your team sets. If the error counter climbs too high, the tool marks the whole build as failed. This setup stops bad code from moving forward in your deployment pipeline.
What native metrics solve and where gaps remain
Built-in and custom counters give clear insight into how an app handles heavy traffic. They prove whether specific code paths run as expected under stress. They also help engineers spot subtle logic bugs that standard HTTP status codes miss. A request might return a success status while still failing a business rule inside.
Still, test metrics only measure what happens on the client side of the request. The load tool needs credentials to log in and send traffic to your backend services. Native test setups decide who can start a run, not what individual requests do.
Counters: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
When a load test runs against staging or production, static keys create big risks. A script bug could send destructive commands or pull back live customer data. Masking rewrites matched fields like emails, phone numbers, card numbers, and national IDs in results. Without strict controls over each command in flight, load tests can leak sensitive records.
Best practices for tracking custom load events
Keep your counter names clear and consistent so the whole team can read them. Avoid putting unique IDs or timestamps inside metric tags during a large test run. High tag counts create too many unique time series and slow down your metric storage. Stick to small sets of known values like status categories or endpoint names.
Always test your counter logic with a single virtual user before scaling up the load. Check the end summary log to confirm the sums match your manual count. Clean up any test data written during the run to keep target systems fast.
Govern statements and responses across databases and APIs
Good config and scoped roles are needed setup. They do not check single queries in flight or mask sensitive fields in returned rows. Across Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes APIs, SSH, and internal HTTP endpoints, hoop.dev runs beside each supported service as an open-source proxy, a sidecar for your database or API.
First, hoop.dev guardrails block banned queries before they run. Then hoop.dev data masking rewrites matched sensitive values in responses in memory. The audit trail records every statement and verdict per resource. Furthermore, hoop.dev holds SOC 2 Type II and is open source on GitHub.
Frequently asked questions
Can a k6 counter value decrease during a test run?
No, a counter only adds positive numbers to the running total over time. If you need to track values that go up and down, use a gauge metric.
Where should I initialize k6 counters in my script?
You must set up all custom counters in the init context outside the main function. Creating metrics inside the virtual user loop causes an error and stops the test.
How do thresholds work with custom counter metrics?
Thresholds check the final count or per-second rate against a rule you define. If the metric breaks that rule, the tool exits with a non-zero failure code.
Related guides
In this post