Metrics REST API
Use these endpoints to access metrics metadata and metrics values from test runs.
Note
The metrics endpoints use the
v5
version of the Grafana Cloud k6 API. Make sure to use the correct version when defining the URL in your scripts or applications to avoid any errors.
List metrics
GET /cloud/v5/test_runs/:testRunId/metrics
Example request:
GET https://api.k6.io/cloud/v5/test_runs/152779/metrics
Accept: application/json
Authorization: Bearer 56c166885f9a7fc1e588a1b3cb66f6dd
Example response
List time series
Note
This API output is similar to Prometheus series API
GET /cloud/v5/test_runs/:testRunId/series
Example request:
GET https://api.k6.io/cloud/v5/test_runs/152777/series?match[]=http_req_duration{expected_response="true"}
Accept: application/json
Authorization: Bearer 56c166885f9a7fc1e588a1b3cb66f6dd
Example response
Query label names
Note
This API output is similar to Prometheus labels API
GET /cloud/v5/test_runs/:testRunId/labels
Example request:
GET https://api.k6.io/cloud/v5/test_runs/152777/labels?match[]=http_req_duration
Accept: application/json
Authorization: Bearer 56c166885f9a7fc1e588a1b3cb66f6dd
Example response
HTTP/1.1 200
Content-Type: application/json
{
"status": "success"
"data": [
"__name__",
"expected_response",
"group",
"load_zone",
"method",
"name",
"proto",
"scenario",
"status",
"test_run_id",
"url"
],
}
Query label values
Note
This API output is similar to Prometheus label values API
GET /test_runs/:testRunId/label/:labelName/values
Example request:
GET https://api.k6.io/cloud/v5/test_runs/152777/label/load_zone/values?match[]=http_req_duration
Accept: application/json
Authorization: Bearer 56c166885f9a7fc1e588a1b3cb66f6dd
Example response
HTTP/1.1 200
Content-Type: application/json
{
"status": "success"
"data": [
"amazon:us:columbus",
"amazon:eu:dublin",
],
}
Query range values
GET /cloud/v5/test_runs/:testRunId/query_range_k6(:parameters)
Range query returns multiple sample values for a given metric over a time range.
Parameters are a comma-separated list in the format of key=value
. String values should be surrounded by single quotes '
.
Note
This API output is similar to Prometheus range query API
Request Parameters
Example request:
GET https://api.k6.io/cloud/v5/test_runs/152779/query_range_k6(query='increase by (name,status)',metric='http_reqs{status!="0"}',step=5)
Accept: application/json
Authorization: Bearer 56c166885f9a7fc1e588a1b3cb66f6dd
Example response
Example request with a time filter:
GET https://api.k6.io/cloud/v5/test_runs/152779/query_range_k6(query='increase by (name,status)',metric='http_reqs{method="GET",status="2\d\d"}',step=5,start=2022-11-23T10:03:00Z,end=2022-11-23T10:13:00Z)
Accept: application/json
Authorization: Bearer 56c166885f9a7fc1e588a1b3cb66f6dd
Cheat sheet
Get a chart of simultaneous VUs during the test run:
/cloud/v5/test_runs/:testRunId/query_range_k6(metric='vus',query='sum(last by (instance_id))')
Get a chart of requests per second:
/cloud/v5/test_runs/:testRunId/query_range_k6(metric='http_reqs',query='rate')
Get a chart of requests per second, excluding network errors (where status
equals to "0"
), separated by URL and HTTP method:
/cloud/v5/test_runs/:testRunId/query_range_k6(metric='http_reqs{status!="0"}',query='rate by (name,method)')
Get a chart of the 95th percentile of HTTP request response times, separated by URL, HTTP method, and HTTP status code, with 10 second resolution, and filtered for a specific time range:
/test_runs/:testRunId/query_range_k6(metric='http_req_duration',query='histogram_quantile(0.95) by (name,method,status)',step=10,start=2021-08-09T12:01:10Z,end=2021-08-09T12:09:13Z)
Query aggregate values
GET /cloud/v5/test_runs/:testRunId/query_aggregate_k6(:parameters)
Aggregate query returns a single sample value for a given metric over the duration of the test run.
Parameters are a comma-separated list in the format of key=value
. String values should be surrounded by single quotes '
.
Note
This API output is similar to Prometheus range query API
Request Parameters
Example request:
GET https://api.k6.io/cloud/v5/test_runs/152779/query_aggregate_k6(query='histogram_quantile(0.95) by (name,status)',metric='http_req_duration')
Accept: application/json
Authorization: Bearer 56c166885f9a7fc1e588a1b3cb66f6dd
Example response
Cheat sheet
Get the total number of HTTP requests made during the test run:
/cloud/v5/test_runs/:testRunId/query_aggregate_k6(metric='http_reqs',query='increase')
Get the number of HTTP requests made within a specific time period:
/cloud/v5/test_runs/:testRunId/query_aggregate_k6(metric='http_reqs',query='increase',start=2021-08-09T12:01:10Z,end=2021-08-09T12:09:13Z)
Get the 95th percentile of HTTP request response times, separated by URL, HTTP method, and HTTP status code:
/cloud/v5/test_runs/:testRunId/query_aggregate_k6(metric='http_req_duration',query='historgam_quantile(0.95) by (name,method,status)')
Get the peak requests per second during the test run with 5 second precision, for requests with statuses 2xx and 3xx, separated by URL:
/cloud/v5/test_runs/:testRunId/query_aggregate_k6(metric='http_reqs{status~="[23][0-9]{2}"}',query='max_rate(5) by (name)')
Get peak CPU usage of each instance during test run, averaged between instances:
/cloud/v5/test_runs/:testRunId/query_aggregate_k6(metric='load_generator_cpu_percent',query='avg(max by (instance))')
Get peak simultaneous VUs during the test run:
/cloud/v5/test_runs/:testRunId/query_aggregate_k6(metric='vus',query='sum(max by (instance_id))')
Specifying metric name
The metric
parameter can be used to specify the metric name and optional label filter.
The syntax is similar to that of Prometheus selectors: metric name, e.g. http_reqs
, followed by optional label filters in curly braces,
e.g. {status="200"}
.
The following operators are supported for label matching:
=
: Equal to the provided string.!=
: Not equal to the provided string.=~
: Regex-match the provided string.!~
: Do not regex-match the provided string.
Examples:
http_req_duration
http_req_duration{status="200"}
http_req_duration{status~="(2|3)\d\d"}
http_req_duration{status!="0"}
http_req_duration{status!="0",name="login page"}
Specifying query expression
The query
parameter can be used to specify the function applied to the metric to produce values.
Basic methods
A method
is a basic function that gives a numerical value when applied to some time span of a
metric.
For range queries (like the ones that produce graphs over time) the timespans are windows between each returned datapoint. For aggregate queries (like getting total number of requests in the test run) the timespan is the whole test run.
k6 has 4 metric types: Counter, Gauge, Rate and Trend. Each of these offer a distinct set of methods for operating that data type.
Counter methods
Gauge methods
Rate methods
Trend methods
Returning multiple type series
Using a method as-is to query a metric produces a single time series, aggregating all values.
To preserve some labels and get multiple time series, use the following syntax:
<method> by (<label list>)
Examples:
# 95th-percentiles or "http_req_duration" metric (Trend), separated by URL ("name" label) and HTTP status ("status" label)
histogram_quantile(0.95) by (name, status)
# Peak CPU usage (Gauge) for each load generator instance
max by (instance_id)
Aggregating over labels dimension
You can further aggregate time series produced with queries using by
syntax.
Use aggregators to aggregate values for each time bucket across labels dimensions.
Supported aggregators are min
, max
, sum
and avg
.
They work similarly to Prometheus aggregation operators
Syntax:
<aggregator>(<method-expr>) [by (<label list>)]
Examples:
# Active VUs count on "vus" metric
sum(last by (instance_id))
# Average request rate on "http_reqs" metric across URLs, separated by HTTP method
avg(rate by (name, method)) by (method)