Search
Varnish Cache Plus

Stat (Prometheus)

Description

The stat vmod provides access to Varnish counters via backends, to expose them through HTTP and cache, compress and manipulate the responses as any cached content.

Please note the prometheus output of this vmod is still subject to change. It is expected that the API of this vmod will not be changed.

Example

Here’s a VCL example to expose prometheus metrics, only showing uptime and hit counters:

import stat;

sub vcl_recv {
    if (req.url == "/metrics") {
        return (pass);
    }
}

sub vcl_backend_fetch {
    if (bereq.url == "/metrics") {
        set bereq.backend = stat.backend_prometheus("MAIN.uptime, *.g_bytes");
    }
}

With it, metrics can be pulled from the usual /metrics path:

$ curl http://localhost:6081/metrics -v
# HELP varnish_uptime Child process uptime
# TYPE varnish_uptime counter
varnish_uptime{host="varnish1",section="MAIN"} 0

# HELP varnish_s0_g_bytes Bytes outstanding
# TYPE varnish_s0_g_bytes gauge
varnish_s0_g_bytes{host="varnish1",section="SMA"} 2941248

# HELP varnish_Transient_g_bytes Bytes outstanding
# TYPE varnish_Transient_g_bytes gauge
varnish_Transient_g_bytes{host="varnish1",section="SMA"} 2244

API

Some functions accept a filters argument, which is a comma-separated STRING to include or exclude certain counters. Each element is interpreted like a varnishstat -f argument, meaning words starting with ^ will exclude matching counters.

backend_json

BACKEND backend_json(STRING filters = 0)

Produce a JSON object containing the format version (1) as well a counters object listing all the objects as varnishstat -j does.

Can only be called from a backend context (such as sub vcl_backend_fetch).

{
  "version": 1,
  "counters": {
    "MGT.uptime": {
      "description": "Management process uptime",
      "flag": "c",
      "format": "d",
      "value": 0
    },
    "MGT.child_start": {
      "description": "Child process started",
      "flag": "c",
      "format": "i",
      "value": 1
    },
    ...

Arguments:

  • filters accepts type STRING with a default value of 0 optional

Type: Function

Returns: Backend

string_json

STRING string_json(STRING filter)

Returns a JSON collection of counters based on the provided filter(s).

Arguments:

  • filter accepts type STRING

Type: Function

Returns: String

backend_prometheus

BACKEND backend_prometheus(STRING filters = 0, BOOL hostnames = 1)

Output a prometheus-compatible list of counters. Can only be called from a backend context (such as sub vcl_backend_fetch). The hostnames parameter denotes whether to include the hostname of the device in the prometheus output.

$ curl http://localhost:6081/metrics -v
# HELP varnish_main_uptime Child process uptime (MAIN.uptime)
# TYPE varnish_main_uptime counter
varnish_main_uptime{host="varnish1"} 0

# HELP varnish_sma_g_bytes Bytes outstanding (SMA.s2.g_bytes)
# TYPE varnish_sma_g_bytes gauge
varnish_sma_g_bytes{id="s2",host="varnish1"} 2941248

# HELP varnish_sma_g_bytes Bytes outstanding (SMA.Transient.g_bytes)
# TYPE varnish_sma_g_bytes gauge
varnish_sma_g_bytes{id="Transient",host="varnish1"} 2244

Arguments:

  • filters accepts type STRING with a default value of 0 optional

  • hostnames accepts type BOOL with a default value of 1 optional

Type: Function

Returns: Backend

get_value

INT get_value(STRING name)

Returns the integer value of a given counter. A value of -1 signifies an error or a failure to find a match. If a value exceeds the integer maximum limit it will be clamped to the integer max limit.

Arguments:

  • name accepts type STRING

Type: Function

Returns: Int

Availability

The stat VMOD is available in Varnish Cache Plus version 6.0.8r2 and later.