k6 scripted check
k6 scripted checks can execute k6 tests in Synthetic Monitoring. Use k6 scripted checks to monitor transactions and user flows by implementing workflows, custom logic, and validations in JavaScript.
With k6 scripted checks, you can:
- Define custom user behavior and complex request logic.
- Leverage the flexibility of a scripting language.
- Test HTTP/S and WebSocket services.
- Utilize the k6 API and other JavaScript libraries.
- Enable OpenTelemetry traces for server-side correlation.
- Reuse the same k6 script for distinct testing goals.
How it works
A k6 check runs one iteration of a k6 test at short, frequent intervals to proactively monitor applications and services.
Synthetic Monitoring results are stored as Prometheus metrics and Loki logs, which allow you to set up Grafana alerts for custom notifications and incident management.
The k6 scripts in Synthetic Monitoring are broadly compatible with other k6 products, like the k6 CLI and Grafana Cloud Performance Testing. This allows you to reuse the same k6 script for various use cases, enhancing testing productivity and coverage.
Get started
You can write and execute a k6 script from the code editor in the Synthetics UI.
- On the left-side menu, select Testing & synthetics and then Synthetics.
- Click Create new check or Add new check.
- Choose k6 scripted as your check type.
- Set the value for the required options.
- Edit or copy your k6 script under Script.
- Schedule or run the check.
- Click Save to schedule the check.
- Click Test to run the k6 script.
For development and debugging, you can write and execute the k6 script from your local machine using the k6 CLI and your IDE. After completing the k6 script, create a new scripted check or update the script of an existing one.
The Synthetics script editor includes a few scripts with basic examples. To get started implementing your first k6 scripts, refer to:
Options
The list of common options to all check types:
Additionally, k6 scripted checks have the following options:
Supported k6 Options
k6 options can be used to configure a wide range of features when running the k6 script. In Synthetic Monitoring, k6 options can only be set in the script options
object:
export const options = {
tags: { foo: 'bar' },
userAgent: 'MyK6UserAgentString/1.0',
};
Several k6 options don’t apply in the context of Synthetic Monitoring. Here is the list of supported options:
Metrics
Synthetic checks store their results as Prometheus metrics, including the list of common metrics to all check types:
k6 metrics
Scripted checks also collect the metrics produced by k6 and store them as Prometheus metrics.
k6 has two types of metrics:
- Built-in metrics: These are metrics collected by every k6 test, such as data received and total number of requests.
- Custom metrics: These are metrics that you can create in your test script to measure anything that’s specific to your system or business logic. In Prometheus, they’re renamed to
probe_K6_METRIC_NAME
and mapped to Prometheus gauges.
k6 built-in metrics are transformed in Synthetic Monitoring as follows:
Visualization
You can query all the produced check metrics with Grafana Explore
, where you can create custom panels and add them to your dashboards.
Additionally, each check includes a dashboard displaying the results of the most relevant metrics. To learn more about the various visualization options, refer to Analyze results.
Compatibility
There are some limitations when running k6 scripts in Grafana Cloud Synthetic Monitoring, compared to running them locally or using Grafana Cloud k6:
Workload options
A k6 check runs only one iteration of the k6 test. Options such as
vus
,duration
,stages
, anditerations
are omitted.Timeout
k6 checks have a maximum execution time, configured by the
Timeout
option in the UI.k6 CLI
The k6 CLI can’t run or upload k6 scripts in Grafana Cloud Synthetic Monitoring.
Load files
Probes can’t load local files for security reasons. k6 APIs like
open
,fs
, andgrpc.load
(non-reflection) are unsupported.Importing a local module or local library isn’t possible from the UI. Alternatively, you can use a bundler to build the k6 script with its dependencies locally and copy it into the script option.
Other k6 APIs
Thresholds aren’t supported.
Labels
Custom labels defined in the UI aren’t included in k6 metrics. The non-k6 metrics don’t include the labels defined in the k6
tags
option.