k6 extension for publishing test-run metrics to Elasticsearch.
Go 1.20 or better (verify with go version
).
Install xk6:
# Install xk6
go install go.k6.io/xk6/cmd/xk6@latest
# Build the xk6 binary locally
git clone git@github.com:elastic/xk6-output-elasticsearch.git
# Build k6 locally with the Elasticsearch output extension
make
You will have a k6
binary in the current directory.
This Dockerfile builds a docker image with the k6 binary.
Note
The examples below use Elastic Cloud, which allows to connect via K6_ELASTICSEARCH_CLOUD_ID
. Alternatively you can also specify the connection URL with K6_ELASTICSEARCH_URL
.
You can run the new k6 binary against a Cloud cluster with:
export K6_ELASTICSEARCH_CLOUD_ID=your-cloud-id-here
export K6_ELASTICSEARCH_USER=elastic
export K6_ELASTICSEARCH_PASSWORD=your-password-here
./k6 run ./examples/script.js -o output-elasticsearch
or alternatively via an API key (see also how to create an API key in Kibana):
export K6_ELASTICSEARCH_CLOUD_ID=your-cloud-id-here
export K6_ELASTICSEARCH_API_KEY=your-base64-encoded-api-key-here
./k6 run ./examples/script.js -o output-elasticsearch
or a service account token:
export K6_ELASTICSEARCH_CLOUD_ID=your-cloud-id-here
export K6_ELASTICSEARCH_SERVICE_ACCOUNT_TOKEN=your-service-account-token-here
./k6 run ./examples/script.js -o output-elasticsearch
or with client certificate authentication
export K6_ELASTICSEARCH_CLIENT_CERT_FILE=cert.pem
export K6_ELASTICSEARCH_CLIENT_KEY_FILE=key.pem
./k6 run ./examples/script.js -o output-elasticsearch
Alternatively, you can send metrics to a local (unsecured) cluster:
export K6_ELASTICSEARCH_URL=http://localhost:9200
./k6 run ./examples/script.js -o output-elasticsearch
If running locally with TLS (with a self-signed certificate), set K6_ELASTICSEARCH_INSECURE_SKIP_VERIFY
to true
(defaults to false
):
export K6_ELASTICSEARCH_URL=https://localhost:9200
export K6_ELASTICSEARCH_INSECURE_SKIP_VERIFY=true
./k6 run ./examples/script.js -o output-elasticsearch
The metrics are stored in the index k6-metrics
by default which will be automatically created by this extension. See the mapping for details. The index name can be customized with the environment variable K6_ELASTICSEARCH_INDEX_NAME
.
This repo includes a docker-compose.yml file based on the documentation, that starts Elasticsearch and Kibana. It also adds a custom build of k6 having the xk6-output-elasticsearch
extension. This is just a quick way to showcase the usage, not meant for production usage.
Note that some variables (Stack version, elastic and Kibana users' passwords) are defined in the .env
file, where you can change them.
Clone the repo to get started and follow these steps:
-
Put your
k6
scripts in theexamples
directory or use thescript.js
example. -
Start the
docker-compose
environment.docker-compose up -d
-
Use the k6 Docker image to run the k6 script and send metrics to the Elasticsearch cluster started in the previous step.
docker-compose run --rm -T k6 run -<examples/script.js --tag testid=<SOME-ID>
Note that the docker-compose command to run k6 tests might differ depending your OS.
-
Visit http://localhost:5601/ to view results in Kibana (default credentials are
elastic
/changeme
).
When done, to clean up run docker-compose down -v
.