Menu
Grafana Cloud

Metrics Values REST API

Use these endpoints to query metrics values from test runs.

Query range values

GET /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

ParameterTypeDescription
querystringRequired: query expression for selecting metrics. Possible values depend on the metric type. See below for detailed list and examples.
metricstringRequired: name of the metric to query, with optional labels selector.
Example: `http_reqs{expected_response=“true”}
stepintOptional: The step size in seconds for the query range.
startiso8601Optional: The start timestamp for the query range. Default is test run start. Example: 2022-11-23T10:03:00Z
endiso8601Optional: The end timestamp for the query range. Default is test run end. Example: 2022-11-23T10:13:00Z

Example request:

http
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: Token 56c166885f9a7fc1e588a1b3cb66f6dd

Example response

http
HTTP/1.1 200
Content-Type: application/json

{
  "status": "success",
  "data": {
    "resultType": "matrix",
    "result": [
      {
        "metric": {
          "__name__": "http_reqs",
          "name": "login page",
          "status": "200",
          "test_run_id": "152779"
        },
        "values": [
          [
            1684949415,
            845
          ],
          [
            1684949420,
            1034
          ]
        ]
      },
      {
        "metric": {
          "__name__": "http_reqs",
          "name": "http://test.k6.io/",
          "status": "200",
          "test_run_id": "152779"
        },
        "values": [
          [
            1684949415,
            16035
          ],
          [
            1684949420,
            54535
          ],
        ]
      }
    ]
  }
}

Example request with a time filter:

http
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: Token 56c166885f9a7fc1e588a1b3cb66f6dd

Query aggregate values

GET /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

ParameterTypeDescription
querystringRequired: query expression for selecting metrics. Possible values depend on the metric type. See below for detailed list and examples.
metricstringRequired: name of the metric to query, with optional labels selector.
Example: `http_reqs{expected_response=“true”}
startiso8601The start timestamp for the query range. Default is test run start. Example: 2022-01-02T10:03:00Z
endiso8601The end timestamp for the query range. Default is test run end. Example: 2022-01-02T10:13:00Z

Example request:

http
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: Token 56c166885f9a7fc1e588a1b3cb66f6dd

Example response

http
HTTP/1.1 200
Content-Type: application/json

{
  "status": "success",
  "data": {
    "resultType": "vector",
    "result": [
      {
        "metric": {
          "__name__": "http_req_duration",
          "name": "http://test.k6.io/?q=2",
          "status": "200",
          "test_run_id": "152779"
        },
        "values": [
          [
            1684950639,
            14207
          ]
        ]
      },
      {
        "metric": {
          "__name__": "http_req_duration",
          "name": "http://test.k6.io/?q=1",
          "status": "500",
          "test_run_id": "152779"
        },
        "values": [
          [
            1684950639,
            28031
          ]
        ]
      }
    ]
  }
}

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

MethodDescriptionPromQL equivalent
increaseTotal increase of counter’s value in the time span. For a metric like http_reqs this will be the number of requests.increase(metric_name[$__interval])
rateAverage increase per second. Equals to increase divided by time span size in seconds.rate(metric_name[$__interval])
valueGrowing counter value.metric_name
cumrateCumulative rate from test run start. Equals to value divided by time elapsed since test run start.
max_rate(<interval>)Get rate for each <interval> seconds within time span and return the max value. Used e.g. for “peak RPS” value in test run stats.
<interval> must be an integer.
max_over_time(rate(metric_name[<interval>])[$__interval:<interval>])

Gauge methods

MethodDescriptionPromQL equivalent
lastLast observed value.metric_name
minMin observed value.min_over_time(metric_name[$__interval])
maxMax observed value.max_over_time(metric_name[$__interval])
avgAverage of observed values.avg_over_time(metric_name[$__interval])
last_timeTimestamp of last observed value.timestamp(metric_name)

Rate methods

MethodDescriptionPromQL equivalent
increase_totalTotal increase of all observations counter in the time span.increase(metric_name[$__interval])
rate_totalAverage increase per second of all observations counter.rate(metric_name[$__interval])
value_totalGrowing value of all observations counter.metric_name
increase_nzTotal increase of nonzero-value observations counter in the time span.increase(metric_name{nonzero="true"}[$__interval])
rate_nzAverage increase per second of nonzero-value observations counter.rate(metric_name{nonzero="true"}[$__interval])
value_nzGrowing value of nonzero-value observations counter.metric_name{nonzero="true"}
increase_zTotal increase of zero-value observations counter in the time span.increase(metric_name{nonzero="false"}[$__interval])
rate_zAverage increase per second of zero-value observations counter.rate(metric_name{nonzero="false"}[$__interval])
value_zGrowing value of zero-value observations counter.metric_name{nonzero="false"}
ratioRatio (fraction) of nonzero-value observations count increase to total increase.increase(metric_name{nonzero="false"}[$__interval]) / increase(metric_name[$__interval])

Trend methods

MethodDescriptionPromQL equivalent
histogram_maxMax observed value.
histogram_minMin observed value.
histogram_avgAverage of observed values.
histogram_stddevStandard deviation.
histogram_quantile(<quantile>)Quantile of histogram observations.
<quantile> parameter must be a number between 0 and 1.
histogram_quantile(<quantile>, rate(hist_metric_name_bucket[$__interval]))
histogram_count_increaseIncrease of observations counter.increase(hist_metric_name_count[$__interval])
histogram_count_valueGrowing value of observations counter.hist_metric_name

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)