<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>k6/metrics on Grafana Labs</title><link>https://grafana.com/docs/k6/v2.3.x/javascript-api/k6-metrics/</link><description>Recent content in k6/metrics on Grafana Labs</description><generator>Hugo -- gohugo.io</generator><language>en</language><atom:link href="/docs/k6/v2.3.x/javascript-api/k6-metrics/index.xml" rel="self" type="application/rss+xml"/><item><title>Counter</title><link>https://grafana.com/docs/k6/v2.3.x/javascript-api/k6-metrics/counter/</link><pubDate>Mon, 21 Sep 2026 15:16:28 +0000</pubDate><guid>https://grafana.com/docs/k6/v2.3.x/javascript-api/k6-metrics/counter/</guid><content><![CDATA[&lt;h1 id=&#34;counter&#34;&gt;Counter&lt;/h1&gt;
&lt;p&gt;&lt;em&gt;Counter&lt;/em&gt; is an object for representing a custom cumulative counter metric. It&amp;rsquo;s one of the four 
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-metrics/&#34;&gt;metric types&lt;/a&gt;.&lt;/p&gt;
&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Parameter&lt;/th&gt;
              &lt;th&gt;Type&lt;/th&gt;
              &lt;th&gt;Description&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;name&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;string&lt;/td&gt;
              &lt;td&gt;The name of the custom metric.&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Method&lt;/th&gt;
              &lt;th&gt;Description&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-metrics/counter/counter-add/&#34;&gt;Counter.add(value, [tags])&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Add a value to the counter metric.&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;h2 id=&#34;counter-usage-in-thresholds&#34;&gt;Counter usage in Thresholds&lt;/h2&gt;
&lt;p&gt;When &lt;code&gt;Counter&lt;/code&gt; is used in a threshold expression, the variable must be called &lt;code&gt;count&lt;/code&gt; or &lt;code&gt;rate&lt;/code&gt; (lower case).
For example:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;count &amp;gt;= 200&lt;/code&gt; // value of the counter must be larger or equal to 200&lt;/li&gt;
&lt;li&gt;&lt;code&gt;count &amp;lt; 10&lt;/code&gt; // less than 10.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;examples&#34;&gt;Examples&lt;/h3&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;JavaScript&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-javascript&#34;&gt;import { Counter } from &amp;#39;k6/metrics&amp;#39;;

const myCounter = new Counter(&amp;#39;my_counter&amp;#39;);

export default function () {
  myCounter.add(1);
  myCounter.add(2, { tag1: &amp;#39;myValue&amp;#39;, tag2: &amp;#39;myValue2&amp;#39; });
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;JavaScript&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-javascript&#34;&gt;import http from &amp;#39;k6/http&amp;#39;;
import { Counter } from &amp;#39;k6/metrics&amp;#39;;

const CounterErrors = new Counter(&amp;#39;Errors&amp;#39;);

export const options = { thresholds: { Errors: [&amp;#39;count&amp;lt;100&amp;#39;] } };

export default function () {
  const res = http.get(&amp;#39;https://quickpizza.grafana.com/api/json?name=Bert&amp;#39;);
  const contentOK = res.json(&amp;#39;name&amp;#39;) === &amp;#39;Bert&amp;#39;;
  CounterErrors.add(!contentOK);
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;JavaScript&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-javascript&#34;&gt;import { Counter } from &amp;#39;k6/metrics&amp;#39;;
import { sleep } from &amp;#39;k6&amp;#39;;
import http from &amp;#39;k6/http&amp;#39;;

const allErrors = new Counter(&amp;#39;error_counter&amp;#39;);

export const options = {
  vus: 1,
  duration: &amp;#39;1m&amp;#39;,
  thresholds: {
    &amp;#39;error_counter&amp;#39;: [
      &amp;#39;count &amp;lt; 10&amp;#39;, // 10 or fewer total errors are tolerated
    ],
    &amp;#39;error_counter{errorType:authError}&amp;#39;: [
      // Threshold on a sub-metric (tagged values)
      &amp;#39;count &amp;lt;= 2&amp;#39;, // max 2 authentication errors are tolerated
    ],
  },
};

export default function () {
  const auth_resp = http.post(&amp;#39;https://quickpizza.grafana.com/api/users/token/login&amp;#39;, {
    username: &amp;#39;default&amp;#39;,
    password: &amp;#39;supersecure&amp;#39;,
  });

  if (auth_resp.status &amp;gt;= 400) {
    allErrors.add(1, { errorType: &amp;#39;authError&amp;#39; }); // tagged value creates submetric (useful for making thresholds specific)
  }

  const other_resp = http.get(&amp;#39;https://quickpizza.grafana.com/api/json&amp;#39;);
  if (other_resp.status &amp;gt;= 400) {
    allErrors.add(1); // untagged value
  }

  sleep(1);
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
]]></content><description>&lt;h1 id="counter">Counter&lt;/h1>
&lt;p>&lt;em>Counter&lt;/em> is an object for representing a custom cumulative counter metric. It&amp;rsquo;s one of the four
&lt;a href="/docs/k6/v2.3.x/javascript-api/k6-metrics/">metric types&lt;/a>.&lt;/p>
&lt;section class="expand-table-wrapper">&lt;div class="button-div">
&lt;button class="expand-table-btn">Expand table&lt;/button>
&lt;/div>&lt;div class="responsive-table-wrapper">
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Parameter&lt;/th>
&lt;th>Type&lt;/th>
&lt;th>Description&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>&lt;code>name&lt;/code>&lt;/td>
&lt;td>string&lt;/td>
&lt;td>The name of the custom metric.&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;/div>
&lt;/section>&lt;section class="expand-table-wrapper">&lt;div class="button-div">
&lt;button class="expand-table-btn">Expand table&lt;/button>
&lt;/div>&lt;div class="responsive-table-wrapper">
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Method&lt;/th>
&lt;th>Description&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>
&lt;a href="/docs/k6/v2.3.x/javascript-api/k6-metrics/counter/counter-add/">Counter.add(value, [tags])&lt;/a>&lt;/td>
&lt;td>Add a value to the counter metric.&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;/div>
&lt;/section>&lt;h2 id="counter-usage-in-thresholds">Counter usage in Thresholds&lt;/h2>
&lt;p>When &lt;code>Counter&lt;/code> is used in a threshold expression, the variable must be called &lt;code>count&lt;/code> or &lt;code>rate&lt;/code> (lower case).
For example:&lt;/p></description></item><item><title>Gauge</title><link>https://grafana.com/docs/k6/v2.3.x/javascript-api/k6-metrics/gauge/</link><pubDate>Mon, 21 Sep 2026 15:16:28 +0000</pubDate><guid>https://grafana.com/docs/k6/v2.3.x/javascript-api/k6-metrics/gauge/</guid><content><![CDATA[&lt;h1 id=&#34;gauge&#34;&gt;Gauge&lt;/h1&gt;
&lt;p&gt;&lt;em&gt;Gauge&lt;/em&gt; is an object for representing a custom metric holding only the latest value added. It&amp;rsquo;s one of the four 
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-metrics/&#34;&gt;metric types&lt;/a&gt;.&lt;/p&gt;
&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Parameter&lt;/th&gt;
              &lt;th&gt;Type&lt;/th&gt;
              &lt;th&gt;Description&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;name&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;string&lt;/td&gt;
              &lt;td&gt;The name of the custom metric.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;isTime&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;boolean&lt;/td&gt;
              &lt;td&gt;A boolean indicating whether the values added to the metric are time values or just untyped values.&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Method&lt;/th&gt;
              &lt;th&gt;Description&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-metrics/gauge/gauge-add/&#34;&gt;Gauge.add(value, [tags])&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Add a value to the gauge metric. Only the latest value added will be kept.&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;h2 id=&#34;gauge-usage-in-thresholds&#34;&gt;Gauge usage in Thresholds&lt;/h2&gt;
&lt;p&gt;When gauge is used in a threshold expression, the variable must be called &lt;code&gt;value&lt;/code&gt; (lower case).
For example:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;value &amp;lt; 200&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;value &amp;gt; 1&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;examples&#34;&gt;Examples&lt;/h3&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;JavaScript&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-javascript&#34;&gt;import { Gauge } from &amp;#39;k6/metrics&amp;#39;;

const myGauge = new Gauge(&amp;#39;my_gauge&amp;#39;);

export default function () {
  myGauge.add(3);
  myGauge.add(1);
  myGauge.add(2, { tag1: &amp;#39;value&amp;#39;, tag2: &amp;#39;value2&amp;#39; });
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;JavaScript&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-javascript&#34;&gt;import http from &amp;#39;k6/http&amp;#39;;
import { sleep } from &amp;#39;k6&amp;#39;;
import { Gauge } from &amp;#39;k6/metrics&amp;#39;;

const GaugeContentSize = new Gauge(&amp;#39;ContentSize&amp;#39;);

export const options = {
  thresholds: {
    ContentSize: [&amp;#39;value&amp;lt;4000&amp;#39;],
  },
};

export default function () {
  const res = http.get(&amp;#39;https://quickpizza.grafana.com&amp;#39;);
  GaugeContentSize.add(res.body.length);
  sleep(1);
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
]]></content><description>&lt;h1 id="gauge">Gauge&lt;/h1>
&lt;p>&lt;em>Gauge&lt;/em> is an object for representing a custom metric holding only the latest value added. It&amp;rsquo;s one of the four
&lt;a href="/docs/k6/v2.3.x/javascript-api/k6-metrics/">metric types&lt;/a>.&lt;/p>
&lt;section class="expand-table-wrapper">&lt;div class="button-div">
&lt;button class="expand-table-btn">Expand table&lt;/button>
&lt;/div>&lt;div class="responsive-table-wrapper">
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Parameter&lt;/th>
&lt;th>Type&lt;/th>
&lt;th>Description&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>&lt;code>name&lt;/code>&lt;/td>
&lt;td>string&lt;/td>
&lt;td>The name of the custom metric.&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>isTime&lt;/code>&lt;/td>
&lt;td>boolean&lt;/td>
&lt;td>A boolean indicating whether the values added to the metric are time values or just untyped values.&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;/div>
&lt;/section>&lt;section class="expand-table-wrapper">&lt;div class="button-div">
&lt;button class="expand-table-btn">Expand table&lt;/button>
&lt;/div>&lt;div class="responsive-table-wrapper">
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Method&lt;/th>
&lt;th>Description&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>
&lt;a href="/docs/k6/v2.3.x/javascript-api/k6-metrics/gauge/gauge-add/">Gauge.add(value, [tags])&lt;/a>&lt;/td>
&lt;td>Add a value to the gauge metric. Only the latest value added will be kept.&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;/div>
&lt;/section>&lt;h2 id="gauge-usage-in-thresholds">Gauge usage in Thresholds&lt;/h2>
&lt;p>When gauge is used in a threshold expression, the variable must be called &lt;code>value&lt;/code> (lower case).
For example:&lt;/p></description></item><item><title>Rate</title><link>https://grafana.com/docs/k6/v2.3.x/javascript-api/k6-metrics/rate/</link><pubDate>Mon, 21 Sep 2026 15:16:28 +0000</pubDate><guid>https://grafana.com/docs/k6/v2.3.x/javascript-api/k6-metrics/rate/</guid><content><![CDATA[&lt;h1 id=&#34;rate&#34;&gt;Rate&lt;/h1&gt;
&lt;p&gt;&lt;em&gt;Rate&lt;/em&gt; is an object for representing a custom metric keeping track of the percentage of added values that are non-zero. It&amp;rsquo;s one of the four 
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-metrics/&#34;&gt;metric types&lt;/a&gt;.&lt;/p&gt;
&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Parameter&lt;/th&gt;
              &lt;th&gt;Type&lt;/th&gt;
              &lt;th&gt;Description&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;name&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;string&lt;/td&gt;
              &lt;td&gt;The name of the custom metric.&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Method&lt;/th&gt;
              &lt;th&gt;Description&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-metrics/rate/rate-add/&#34;&gt;Rate.add(value, [tags])&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Add a value to the rate metric.&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;h2 id=&#34;rate-usage-in-thresholds&#34;&gt;Rate usage in Thresholds&lt;/h2&gt;
&lt;p&gt;When &lt;code&gt;Rate&lt;/code&gt; is used in a threshold expression, the variable must be called &lt;code&gt;rate&lt;/code&gt; (lower case).
For example:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;rate &amp;lt; 0.1&lt;/code&gt; // less than 10%&lt;/li&gt;
&lt;li&gt;&lt;code&gt;rate &amp;gt;= 0.9&lt;/code&gt; // more or equal to 90%&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The value of the &lt;code&gt;rate&lt;/code&gt; variable ranges between &lt;code&gt;0.00&lt;/code&gt; and &lt;code&gt;1.00&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&#34;examples&#34;&gt;Examples&lt;/h3&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;JavaScript&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-javascript&#34;&gt;import { Rate } from &amp;#39;k6/metrics&amp;#39;;

const myRate = new Rate(&amp;#39;my_rate&amp;#39;);

export default function () {
  myRate.add(true);
  myRate.add(false);
  myRate.add(1);
  myRate.add(0, { tag1: &amp;#39;value&amp;#39;, tag2: &amp;#39;value2&amp;#39; });
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;JavaScript&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-javascript&#34;&gt;import { Rate } from &amp;#39;k6/metrics&amp;#39;;
import { sleep } from &amp;#39;k6&amp;#39;;
import http from &amp;#39;k6/http&amp;#39;;

const errorRate = new Rate(&amp;#39;errorRate&amp;#39;);

export const options = {
  vus: 1,
  duration: &amp;#39;5m&amp;#39;,
  thresholds: {
    errorRate: [
      // more than 10% of errors will abort the test
      { threshold: &amp;#39;rate &amp;lt; 0.1&amp;#39;, abortOnFail: true, delayAbortEval: &amp;#39;1m&amp;#39; },
    ],
  },
};

export default function () {
  const resp = http.get(&amp;#39;https://quickpizza.grafana.com&amp;#39;);

  errorRate.add(resp.status &amp;gt;= 400);

  sleep(1);
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
]]></content><description>&lt;h1 id="rate">Rate&lt;/h1>
&lt;p>&lt;em>Rate&lt;/em> is an object for representing a custom metric keeping track of the percentage of added values that are non-zero. It&amp;rsquo;s one of the four
&lt;a href="/docs/k6/v2.3.x/javascript-api/k6-metrics/">metric types&lt;/a>.&lt;/p></description></item><item><title>Trend</title><link>https://grafana.com/docs/k6/v2.3.x/javascript-api/k6-metrics/trend/</link><pubDate>Mon, 21 Sep 2026 15:16:28 +0000</pubDate><guid>https://grafana.com/docs/k6/v2.3.x/javascript-api/k6-metrics/trend/</guid><content><![CDATA[&lt;h1 id=&#34;trend&#34;&gt;Trend&lt;/h1&gt;
&lt;p&gt;&lt;em&gt;Trend&lt;/em&gt; is an object for representing a custom metric that allows for calculating different statistics on the added values (min, max, average or percentiles). It&amp;rsquo;s one of the four 
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-metrics/&#34;&gt;metric types&lt;/a&gt;.&lt;/p&gt;
&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Parameter&lt;/th&gt;
              &lt;th&gt;Type&lt;/th&gt;
              &lt;th&gt;Description&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;name&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;string&lt;/td&gt;
              &lt;td&gt;The name of the custom metric.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;isTime&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;boolean&lt;/td&gt;
              &lt;td&gt;A boolean indicating whether the values added to the metric are time values or just untyped values.&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Method&lt;/th&gt;
              &lt;th&gt;Description&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-metrics/trend/trend-add/&#34;&gt;Trend.add(value, [tags])&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Add a value to the trend metric.&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;h2 id=&#34;trend-usage-in-thresholds&#34;&gt;Trend usage in Thresholds&lt;/h2&gt;
&lt;p&gt;When &lt;code&gt;Trend&lt;/code&gt; is used in a 
    &lt;a href=&#34;/docs/k6/v2.3.x/using-k6/thresholds/&#34;&gt;threshold expression&lt;/a&gt;, there are a range of variables that can be used.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;avg&lt;/code&gt; for average&lt;/li&gt;
&lt;li&gt;&lt;code&gt;min&lt;/code&gt; for minimum&lt;/li&gt;
&lt;li&gt;&lt;code&gt;max&lt;/code&gt; for maximum&lt;/li&gt;
&lt;li&gt;&lt;code&gt;med&lt;/code&gt; for median&lt;/li&gt;
&lt;li&gt;&lt;code&gt;p(N)&lt;/code&gt; for specific percentile. &lt;code&gt;N&lt;/code&gt; is a number between &lt;code&gt;0.0&lt;/code&gt; and &lt;code&gt;100.0&lt;/code&gt; meaning the percentile value to look at, e.g. &lt;code&gt;p(99.99)&lt;/code&gt; means the 99.99th percentile.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The unit of these variables and functions are all in milliseconds.&lt;/p&gt;
&lt;h3 id=&#34;example-threshold-expressions&#34;&gt;Example threshold expressions:&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;p(95) &amp;lt; 400&lt;/code&gt; // 95% of requests must finish below 400ms&lt;/li&gt;
&lt;li&gt;&lt;code&gt;p(99) &amp;lt; 1000&lt;/code&gt; // 99% of requests must finish within 1s.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;p(50) &amp;lt; 200&lt;/code&gt; // half of requests must finish within 200ms.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;max &amp;lt; 3000&lt;/code&gt; // the slowest request must finish within 3s.&lt;/li&gt;
&lt;/ul&gt;
&lt;blockquote&gt;
&lt;h4 id=&#34;-dont-use-min-and-max-in-thresholds&#34;&gt;⚠️ Don&amp;rsquo;t use &lt;code&gt;min&lt;/code&gt; and &lt;code&gt;max&lt;/code&gt; in thresholds&lt;/h4&gt;
&lt;p&gt;We don&amp;rsquo;t recommend using &lt;code&gt;min&lt;/code&gt; and &lt;code&gt;max&lt;/code&gt; for specifying thresholds because these
values represent outliers. Use percentiles instead.&lt;/p&gt;&lt;/blockquote&gt;
&lt;h3 id=&#34;examples&#34;&gt;Examples&lt;/h3&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;JavaScript&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-javascript&#34;&gt;import { Trend } from &amp;#39;k6/metrics&amp;#39;;

const myTrend = new Trend(&amp;#39;my_trend&amp;#39;);

export default function () {
  myTrend.add(1);
  myTrend.add(2, { tag1: &amp;#39;value&amp;#39;, tag2: &amp;#39;value2&amp;#39; });
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;!-- md-k6:nothresholds --&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;JavaScript&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-javascript&#34;&gt;import { Trend } from &amp;#39;k6/metrics&amp;#39;;
import { sleep } from &amp;#39;k6&amp;#39;;
import http from &amp;#39;k6/http&amp;#39;;

const serverWaitingTimeOnLogin = new Trend(&amp;#39;serverWaitingTimeOnLogin&amp;#39;, true);

export const options = {
  vus: 1,
  duration: &amp;#39;1m&amp;#39;,
  thresholds: {
    serverWaitingTimeOnLogin: [&amp;#39;p(95) &amp;lt; 200&amp;#39;],
  },
};

export default function () {
  const resp = http.post(&amp;#39;https://quickpizza.grafana.com/api/users/token/login&amp;#39;, {
    username: &amp;#39;default&amp;#39;,
    password: &amp;#39;supersecure&amp;#39;,
  });

  serverWaitingTimeOnLogin.add(resp.timings.waiting);
  sleep(1);
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
]]></content><description>&lt;h1 id="trend">Trend&lt;/h1>
&lt;p>&lt;em>Trend&lt;/em> is an object for representing a custom metric that allows for calculating different statistics on the added values (min, max, average or percentiles). It&amp;rsquo;s one of the four
&lt;a href="/docs/k6/v2.3.x/javascript-api/k6-metrics/">metric types&lt;/a>.&lt;/p></description></item></channel></rss>