Before you begin
Anyone who can use the command line to edit files and install software should be able to follow along. However, if you want to create an extension for production use, you should have:- Familiarity with both Go (Golang) and JavaScript, and their tooling
- Understanding of how the Go-to-JavaScript bridge works within k6
Prerequisites
Install the required tools:Install Go
Download and install Go (version 1.19 or higher).
Install Git
Install Git for version control.
Avoid unneeded work
Before creating a new extension:- Check existing extensions - Review the Extensions catalog and the
xk6topic on GitHub - Prefer generic solutions - If you can test with a generic protocol like MQTT, use xk6-mqtt over building a custom protocol extension
- Consider pure JavaScript - A JavaScript library will be better supported, more straightforward, and reusable than an extension
JavaScript extensions
JavaScript extensions extend the JavaScript APIs available to your test scripts. They can add support for new network protocols, improve performance, or add features.Create a simple extension
k6 extensions must have the
k6/x/ prefix, and the short name must be unique among all extensions built in the same k6 binary.Advanced module API
For extensions that need access to internal k6 objects (VU state, runtime), implement the advanced module API:Output extensions
Output extensions send metrics to a custom file format or service. They can add custom processing and dispatching methods.Create an output extension
Secret source extensions
Secret source extensions provide secrets to your k6 script at runtime from external sources like HashiCorp Vault or AWS Secrets Manager.Subcommand extensions
Subcommand extensions add custom CLI commands under thek6 x namespace for setup, validation, and integration tools.
Best practices
Performance
Code in the default function executes many times. Ensure operations are performant and thread-safe.
Initialization
Do heavy initialization in the init context, not in the default function.
Custom metrics
Use
Registry.NewMetric() to create custom metrics and emit them with metrics.PushIfNotDone().Templates
Use the xk6-example or xk6-output-example templates.
Quick start with templates
The fastest way to get started is using GitHub templates:- Create a repository from grafana/xk6-example for JavaScript extensions
- Or use grafana/xk6-output-example for output extensions
- Open in GitHub Codespaces for instant development environment
- Run
xk6 run script.jsto test immediately
Registry requirements
If you maintain a public xk6 repository and wish to have your extension listed in the registry, review the requirements in the k6 documentation.Next steps
Bundle Extensions
Build custom k6 binaries with xk6
Explore Extensions
See what others have built
Community Forum
Ask questions and share your extensions
GitHub Template
Start with the official template