As a software engineer here at Grafana Labs, I’ve learned there are two questions that commonly come up when someone begins setting up a new Loki installation: “How many logs can I ingest into my cluster?” followed by, “How fast can I query these logs?”
There are two ways to find out the answers.
You could configure your existing application infrastructure to push logs and see what happens — or you can find out the smart way and load test the system with Grafana k6.
Grafana k6 is a modern load-testing tool. Its clean and approachable scripting API works locally or in the cloud. Thankfully, there is also a k6 extension that allows you to push logs to and query logs from Loki. It acts as a Loki client, simulating real-world load to test the scalability, reliability, and performance of your Loki installation.
In this post, I’m going to explain how to do that and show you the basic concepts for write and query path testing, as well as some more insights on how to use configuration options to tune your tests. In my explanation, I will assume you have a basic understanding of how to use k6. (If you’re new to it, head over the documentation — it’s great!) Also, I’m not going to explain how to build and install the k6 Loki extension, but it’s not as hard as you might think. You can find instructions here.
A very basic test.js load test file looks like this:
And can be executed like so:
First, let’s look at how to randomize push requests to simulate write path load. As shown in the example above, the Client object has a method push(). This generates a single push request with a payload containing five streams with random label values and a total uncompressed size of log files between 800Kb and 1Mb.
Need to adjust the request parameters? The client also has a pushParameterized(streams, minSize, maxSize) method that lets you do exactly that. In the following example, the push request generates a random amount between two and eight streams, and a total log size between 1 and 2MB.
If you want a constant batch size, set the second and third argument to the same value. The size for each individual stream is the total size divided by the number of streams.
In terms of the actual log lines, xk6-loki generates log lines using the flog library, which generates fake log lines in various common log formats, such as apache_common, syslog rfc5424, or json. The log line format is defined by the format label of the stream.\
Labels and streams
So far, we’ve controlled the format and size of the actual logs, but did not have a look at the log metadata, also called labels. A set of labels with unique key-value-pairs is called a stream, and a stream contains many log lines. When using the pushParameterized(n, minSize, maxSize) function to push logs to Loki, xk6-loki creates n streams with random label values.
A stream always contains three predefined labels — instance, os, and format — as well as optional labels namespace, app, pod, language, and word. The predefined label instance is set to the VU and hostname; os is one of windows, linux, or darwin; and format is one of the supported log formats of flog.
Optional labels are used when the Config object is instantiated with a “label cardinality” map. The map defines how many different label values for the given label name should be used. It allows you to control how many unique streams the load test can possibly generate, which iis just the cartesian product of the possible values.
The following example shows how to configure the cardinality of optional labels.
Assuming the test is run from a single machine with 10 VUs, the maximum amount of unique streams is:
At the time of writing, there is no way to define custom label names.
As with pushing logs, the Client object also provides functions for querying logs and metadata from Loki. Just like Loki’s query API endpoints, these methods are:
- instantQuery(query, limit)
- rangeQuery(query, duration, limit)
- labelValuesQuery(label, duration)
- seriesQuery(matchers, duration)
(They do very much what their names suggest.)
The following example uses the labels from the Config object to generate randomized LogQL queries. Since the pool of labels is generated already when the Config object is instantiated — and because it is done with a fixed seed — label names and values are generated in a predictable manner. This is useful when you want to run a write and a read load test separately.
When performing query tests, it’s important to always check the response of the API call, as it gives further insights into whether queries were successful or not.
Single user vs multi-tenancy
Loki can be run in single and multi-tenant mode. When run in single-tenant mode, all logs are stored under the same tenant: “fake.” In multi-tenant mode, the client can specify the X-Scope-OrgID header to identify as a tenant. Logs stored by a specific tenant can only be retrieved by the same tenant. xk6-loki also supports both modi operandi.
If Loki is configured in multi-tenant mode (authentication enabled), and the loki.Config method does not receive a username as part of the URL, xk6-loki will use a different X-Scope-OrgID value (in the format xk6-tenant-$VU) for each virtual user (VU). That means that running a test with n VUs will ingest roughly 1/n of the total logs for each VU. The same applies for queries, so a query for a tenant only processes 1/n of the total data ingested.
If Loki is configured in multi-tenant mode but you want to use xk6-loki in single user mode, you can specify the username, and optionally also the password, in the user info part of the URL. It would look like this:
This way, every request, both for pushing and querying logs, is done using the same X-Scope-OrgID header across all VUs on the load test.
Unlike single user mode, which optionally accepts a password, multi-tenant mode does not allow authorization (passwords) of individual tenants.
Monitoring your load test
Last but not least, what would a load test be without gathering hard data that you can analyze and compare? In addition to the build-in metrics of k6, the extension collects additional custom metrics — both for push and query requests — and they are printed in the end-of-test summary. Here’s an example of what one looks like:
For query requests, these metrics are exposed:
- loki_bytes_processed_total and loki_lines_processed_total are counters for bytes and lines respectively.
loki_bytes_processed_per_second and loki_lines_processed_per_second are throughput rates for bytes and lines respectively.
All four metrics are derived from the query response statistics sent by the Loki server.
And for push requests, you’ll want to use:
loki_client_lines is a counter that counts the total number of lines that have been pushed to Loki during the time of the load test. loki_client_uncompressed_bytes counts the total number of bytes of the pushed log lines.
These metrics provide insights in how much data was transferred to and processed by Loki.
Now you should be able to set up and run load tests against your Loki installation.
If you’d like to learn more, there is plenty of more information and a full reference documentation of the xk6-loki extension on Github.
Want to learn more about Grafana k6? Join us for our upcoming webinar: Shift left performance testing with Grafana k6 Cloud (July 7).