How to get your k6 performance test metrics into Splunk?
K6 is really a great product and very easy to setup. The question is, what do you do with the results? If you bought k6 as part of your Grafana Cloud subscription it's easy but if you want to use the open source product in your CI/CD pipelines or just run at your own schedule? You need to have the data somewhere for analysis.
Wouldn't it be great if you have it in Splunk Enterprise together with your system metrics and all relevant logfiles? I think that would be great! Let's go!
The setup
The setup is quite simple.
I've build a simple Flask app which can be called using HTTP requests
A k6 container contains a loadtest script which runs against the Flask app
Loadtest results are streamed in a statsd (metrics) format to Splunk Enterprise
For this guide, everything has been build using docker compose.
Flask app
The Flask app consists of app.py, wsgi.py and a requirements.txt. It's glued together in a Dockerfile.
Service definition in docker-compose.yml
k6
k6 is a loadtest project based on JavaScript files. It was started as an open source project and later aqcuired by Grafana to be part of the Grafana Cloud solution. The open source project is still available and updated but may lack certain features. For us that is not a problem because we don't need them.
k6 is super easy to setup.
Run a k6 container with the correct environment variables
Put the loadtest script in the container
Run when needed (I run it manually but it can be easily integrated into a CI/CD pipeline)
Below you will find the docker-compose service definition. The important part is the volume (to get the script in the container) and the environment variables. For statsd output there are multiple options for which you can find an explanation on the k6 website. Here all I need is the K6_OUT var set to statsd and the K6_STATSD_ADDR set to the TCP input I'll define in my Splunk Enterprise container.
💡
Note. The built-in k6 statsd output will be deprecated and replaced with a xk6 extension in the future. More info HERE!
The beauty of a k6 loadtest script is that it contains the actions it needs to execute, the testscenario that needs to be exectued and the thresholds it needs to monitor. For a full list of opportunities visit their website.
Now we need something to run the k6 script when I want to. It's nothing more than a docker-compose run command which, if need be, can be wrapped in a shell script.