Monitor Cloudflare Tunnel with Grafana
It is not possible to push metrics directly from
cloudflared to Grafana. Instead,
cloudflared runs a metrics endpoint, which a Prometheus server periodically scrapes. Grafana then uses Prometheus as a data source to present metrics to the administrator.
This tutorial covers how to create the metrics endpoint, set up the Prometheus server, and view the data in Grafana.
Before you begin
Create the metrics endpoint
If your tunnel was created via the CLI, run the following command on the
cloudflared server (
$ cloudflared tunnel --metrics 192.168.1.1:60123 run my-tunnel
Set up Prometheus
On the Prometheus and Grafana server (
Extract Prometheus:$ tar xvfz prometheus-*.tar.gz$ cd prometheus-*
prometheus.ymlin a text editor and add the
cloudflaredjob to the end of the file:
prometheus.yml# my global configglobal:scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.# scrape_timeout is set to the global default (10s).# Alertmanager configurationalerting:alertmanagers:- static_configs:- targets:# - alertmanager:9093# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.rule_files:# - "first_rules.yml"# - "second_rules.yml"# A scrape configuration containing exactly one endpoint to scrape:# Here it's Prometheus itself.scrape_configs:# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.- job_name: "prometheus"# metrics_path defaults to '/metrics'# scheme defaults to 'http'.static_configs:- targets: ["localhost:9090"] ## Address of Prometheus dashboard- job_name: "cloudflared"static_configs:- targets: ["184.108.40.206:60123"] ## cloudflared server IP and the --metrics port configured for the tunnel
Start Prometheus:$ ./prometheus --config.file="prometheus.yml"
You can optionally configure Prometheus to run as a service so that it does not need to be manually started if the machine reboots.
Open a browser and go to
http://localhost:9090/. You should be able to access the Prometheus dashboard.
To verify that Prometheus is fetching tunnel metrics, enter
cloudflared_tunnel_total_requestsinto the expression console and select Execute.
Connect Grafana to Prometheus
Start Grafana as a system service:$ sudo systemctl daemon-reload$ sudo systemctl start grafana-server
Verify that Grafana is running:$ sudo systemctl status grafana-server
Open a browser and go to
http://localhost:3000/. The default HTTP port that Grafana listens to is
3000unless you have configured a different port.
On the sign-in page, enter your Grafana credentials.
To test without an account, you can enter
adminfor both the username and password and skip the password change step.
In Grafana, go to Connections > Data sources.
Select Add a new data source and select Prometheus.
In the Prometheus server URL field, enter the IP address and port of your Prometheus dashboard (
Select Save & test.
Build Grafana dashboard
- In Grafana, go to Dashboards > New > New dashboard.
- Select Add visualization.
- Select Prometheus.
- In the metrics field, enter
cloudflared_tunnel_total_requestsand select Run queries. You will see a graph showing the number of requests as a function of time.
You can add operations to the queries to modify what is displayed. For example, you could show all tunnel requests over a recent period of time, such as a day, rather than all tunnel requests since metrics began reporting.