Menu
Grafana Cloud

Varnish Cache integration for Grafana Cloud

Varnish Cache is an open source web application accelerator, also called a caching HTTP reverse proxy. This integration for Grafana Cloud allows users to collect metrics and logs for their Varnish system.

Varnish Cache integration for Grafana cloud provides a streamlined way for users to easily monitor their Varnish system with little overhead while getting the crucial information they need.

This integration supports Varnish Cache 6.0 LTS.

This integration includes 6 useful alerts and 1 pre-built dashboard to help monitor and visualize Varnish Cache metrics and logs.

Before you begin

In order for the integration to properly work, you must set up the Varnish Cache exporter for Prometheus to export metrics from Varnish.

Set up Varnish Cache exporter

Install the version of the exporter that corresponds to your operating system. The latest releases and installs can be found on exporter installation. To start the exporter first unzip the file that you downloaded above and traverse to the directory where the executable is. The executable will be named similar to prometheus_varnish_exporter. An example of the exporter being run is shown below:

bash
sudo ./prometheus_varnish_exporter

The above will run a

bash
varnishstat -j

behind the scenes and open a port 9131 that can be accessed through localhost:9131. The exported Prometheus metrics can be seen at localhost:9131/metrics.

The exporter exposes port 9131 by default but you can change this using the -web.listen-address flag when running the exporter. The use of this flag would look like this:

bash
sudo ./prometheus_varnish_exporter -web.listen-address localhost:9595

If you have multiple Varnish instances, you must specify which you are targeting with the exporter. This can be done by running the exporter with the flag -n and the name of your varnish instance. Example shown below:

bash
sudo ./prometheus_varnish_exporter -n <instance_name>

This also means you would need one exporter for each instance of Varnish Cache, each with a unique port.

For more options and help with the exporter, you can run the below command:

bash
sudo ./prometheus_varnish_exporter -h

Once deployed, the Grafana Agent should be able to reach the Varnish Cache exporter’s exposed endpoint in order to scrape metrics.

Set up Varnish Cache logging

This integration leverages the varnishncsa utility provided by Varnish in order to produce formatted logs. Any format of log collection with varnishncsa will be picked up by the integration.

In order to setup varnishncsa, the utility must be enabled by running:

bash
sudo systemctl enable varnishncsa
sudo systemctl start varnishncsa

By default this integration provides two commands that enable frontend and backend logging capabilities. Since Varnish does not naturally record logs into a file, a log path must be specified for varnishncsa to write to. For darwin and linux systems respectively, the paths need to be /opt/varnish/log and /var/log/varnish. Note: replace <instance_name> and <log_path> with the respective names of the instance and log path. If there is only one instance, then the -n <instance_name> flag can be removed. The below commands must be re-run for each instance that log collection is required for.

bash
sudo varnishncsa -n <instance_name> -F '{"Timestamp": "%t", "Varnish-Side": "%{Varnish:side}x", "Age": %{age}o, "Handling": "%{Varnish:handling}x", "Request": "%r", "Status": "%s", "Response-Reason": "%{VSL:RespReason}x", "Fetch-Error": "%{VSL:FetchError}x", "X-Forwarded-For": "%{x-forwarded-for}i", "Remote-User": "%u", "Bytes": "%b", "Time-To-Serve": %D, "User-Agent": "%{User-agent}i", "Referer": "%{Referer}i", "X-Varnish": "%{x-varnish}o", "X-Magento-Tags": "%{x-magento-tags}o"}}' -D -w <log_path>/varnishncsa-frontend.log

sudo varnishncsa -n <instance_name> -b -F '{"Timestamp": "%t", "Varnish-Side": "%{Varnish:side}x", "Handling": "%{Varnish:handling}x", "Request": "%r", "Status": "%s", "Response-Reason": "%{VSL:RespReason}x", "Fetch-Error": "%{VSL:FetchError}x", "Bytes": "%b", "Time-To-Serve": %D}' -D -w <log_path>/varnishncsa-backend.log

For multiple instances, configure the files where logs are written to follow the following naming convention: varnishncsa-frontend-<instance_name>.log varnishncsa-backend-<instance_name>.log

Install Varnish Cache integration for Grafana Cloud

  1. In your Grafana Cloud stack, click Connections in the left-hand menu.
  2. Find Varnish Cache and click its tile to open the integration.
  3. Review the prerequisites in the Configuration Details tab and set up Grafana Agent to send Varnish Cache metrics and logs to your Grafana Cloud instance.
  4. Click Install to add this integration’s pre-built dashboard and alerts to your Grafana Cloud instance, and you can start monitoring your Varnish Cache setup.

Post-install configuration for the Varnish Cache integration

After enabling the metrics generation, instruct the Grafana Agent to scrape your Varnish Cache metrics.

The exporter exposes a default Prometheus endpoint route in the /metrics format endpoint. To scrape it, add the snippet in the next step to your agent configuration file.

Make sure to change targets in the snippet according to your environment.

If you want to show logs and metrics signals correlated in your dashboards as a single pane of glass, ensure the following:

  • job and instance label values must match for the Varnish Cache integration and logs scrape config in your agent configuration file.
  • job label must be set to integrations/varnish-cache (already configured in the snippets)
  • instance label must be set to a value that uniquely identifies your Varnish Cache instance. Please replace the default varnish_host value according to your environment - it should be set manually. Note that if you use localhost for multiple instances, the dashboards will not be able to filter correctly by instance.

Configuration snippets for Grafana Agent

Below metrics.configs.scrape_configs, insert the following lines and change the URLs according to your environment:

yaml
    - job_name: integrations/varnish-cache
      metrics_path: /metrics
      relabel_configs:
        - replacement: '<your-instance-name>'
          target_label: instance
      static_configs: 
        - targets:
          - '<exporter_host>:<exporter_port>'

Below logs.configs.scrape_configs, insert the following lines according to your environment.

yaml
    - job_name: integrations/varnish-cache
      static_configs:
        - targets: [localhost] 
          labels:
            job: integrations/varnish-cache
            instance: '<your-instance-name>'
            __path__: '<log_path>/varnishncsa*.log'

Full example configuration for Grafana Agent

Refer to the following Grafana Agent configuration for a complete example that contains all the snippets used for the Varnish Cache integration. This example also includes metrics that are sent to monitor your Grafana Agent instance.

yaml
integrations:
  prometheus_remote_write:
  - basic_auth:
      password: <your_prom_pass>
      username: <your_prom_user>
    url: <your_prom_url>
  agent:
    enabled: true
    relabel_configs:
    - action: replace
      source_labels:
      - agent_hostname
      target_label: instance
    - action: replace
      target_label: job
      replacement: "integrations/agent-check"
    metric_relabel_configs:
    - action: keep
      regex: (prometheus_target_sync_length_seconds_sum|prometheus_target_scrapes_.*|prometheus_target_interval.*|prometheus_sd_discovered_targets|agent_build.*|agent_wal_samples_appended_total|process_start_time_seconds)
      source_labels:
      - __name__
  # Add here any snippet that belongs to the `integrations` section.
  # For a correct indentation, paste snippets copied from Grafana Cloud at the beginning of the line.
logs:
  configs:
  - clients:
    - basic_auth:
        password: <your_loki_pass>
        username: <your_loki_user>
      url: <your_loki_url>
    name: integrations
    positions:
      filename: /tmp/positions.yaml
    scrape_configs:
      # Add here any snippet that belongs to the `logs.configs.scrape_configs` section.
      # For a correct indentation, paste snippets copied from Grafana Cloud at the beginning of the line.
    - job_name: integrations/varnish-cache
      static_configs:
        - targets: [localhost] 
          labels:
            job: integrations/varnish-cache
            instance: '<your-instance-name>'
            __path__: '<log_path>/varnishncsa*.log'
metrics:
  configs:
  - name: integrations
    remote_write:
    - basic_auth:
        password: <your_prom_pass>
        username: <your_prom_user>
      url: <your_prom_url>
    scrape_configs:
      # Add here any snippet that belongs to the `metrics.configs.scrape_configs` section.
      # For a correct indentation, paste snippets copied from Grafana Cloud at the beginning of the line.
    - job_name: integrations/varnish-cache
      metrics_path: /metrics
      relabel_configs:
        - replacement: '<your-instance-name>'
          target_label: instance
      static_configs: 
        - targets:
          - '<exporter_host>:<exporter_port>'
  global:
    scrape_interval: 60s
  wal_directory: /tmp/grafana-agent-wal

Dashboards

The Varnish Cache integration installs the following dashboards in your Grafana Cloud instance to help monitor your system.

  • Varnish overview

Varnish Cache overview (overview)

Varnish Cache overview (overview)

Varnish Cache overview (logs)

Varnish Cache overview (logs)

Alerts

The Varnish Cache integration includes the following useful alerts:

AlertDescription
VarnishCacheLowCacheHitRateWarning: Cache is not answering a sufficient percentage of read requests.
VarnishCacheHighMemoryUsageWarning: Varnish Cache is running low on available memory.
VarnishCacheHighCacheEvictionRateCritical: The cache is evicting too many objects.
VarnishCacheHighSaturationWarning: There are too many threads in queue, Varnish is saturated and responses are slowed.
VarnishCacheSessionsDroppingCritical: Incoming requests are being dropped due to a lack of free worker threads.
VarnishCacheBackendUnhealthyCritical: Backend has been marked as unhealthy due to slow 200 responses.

Metrics

The most important metrics provided by the Varnish Cache integration, which are used on the pre-built dashboard and Prometheus alerts, are as follows:

  • varnish_backend_beresp_bodybytes
  • varnish_backend_beresp_hdrbytes
  • varnish_main_backend_busy
  • varnish_main_backend_conn
  • varnish_main_backend_recycle
  • varnish_main_backend_req
  • varnish_main_backend_reuse
  • varnish_main_backend_unhealthy
  • varnish_main_cache_hit
  • varnish_main_cache_hitpass
  • varnish_main_cache_miss
  • varnish_main_client_req
  • varnish_main_n_expired
  • varnish_main_n_lru_nuked
  • varnish_main_pools
  • varnish_main_s_resp_bodybytes
  • varnish_main_s_resp_hdrbytes
  • varnish_main_sessions
  • varnish_main_sessions_total
  • varnish_main_thread_queue_len
  • varnish_main_threads
  • varnish_main_threads_created
  • varnish_main_threads_failed
  • varnish_main_threads_limited
  • varnish_sma_g_bytes
  • varnish_sma_g_space

Changelog

md
# 0.0.3 - September 2023

* New Filter Metrics option for configuring the Grafana Agent, which saves on metrics cost by dropping any metric not used by this integration. Beware that anything custom built using metrics that are not on the snippet will stop working.
* New hostname relabel option, which applies the instance name you write on the text box to the Grafana Agent configuration snippets, making it easier and less error prone to configure this mandatory label.

# 0.0.2 - August 2023

* Add regex filter for logs datasource

# 0.0.1 - July 2023

* Initial release

Cost

By connecting your Varnish Cache instance to Grafana Cloud, you might incur charges. To view information on the number of active series that your Grafana Cloud account uses for metrics included in each Cloud tier, see Active series and dpm usage and Cloud tier pricing.