-
-
Couldn't load subscription status.
- Fork 68
Monitoring with Prometheus & Grafana.md
DockFlare can export native Prometheus metrics from the managed cloudflared agent, enabling detailed monitoring of your tunnel's health, traffic, and performance. This guide explains how to enable this feature and how to set up a monitoring stack to visualize the data.
To enable the metrics endpoint, you need to set one environment variable in your .env file.
| Variable | Required | Default | Description |
|---|---|---|---|
CLOUDFLARED_METRICS_PORT |
No | (unset) | If set to a port number (e.g., 2000), this enables the Prometheus metrics endpoint on the managed cloudflared agent. The agent will listen on 0.0.0.0:<PORT>, and the port will be exposed to the Docker host. If unset, the metrics endpoint is disabled. |
Example .env configuration:
# Enables the Prometheus metrics endpoint on port 2000
CLOUDFLARED_METRICS_PORT=2000DockFlare's built-in reconciliation logic will automatically handle the rest. If you add, change, or remove this variable and restart DockFlare, the cloudflared agent container will be automatically recreated with the correct settings.
cloudflared exposes a wide range of metrics. Key metrics to monitor include:
-
Tunnel Traffic:
cloudflared_tunnel_total_requests,cloudflared_tunnel_request_errors -
Performance:
cloudflared_tunnel_request_duration_seconds_bucket(used to calculate latency percentiles like P99, P95, etc.) -
Connections:
cloudflared_tunnel_ha_connections(connections to the Cloudflare edge),cloudflared_tunnel_concurrent_requests -
HTTP Details:
cloudflared_tunnel_response_by_code(breakdown of HTTP status codes),cloudflared_tunnel_requests_per_protocol -
Internal Process Health: Advanced metrics like
process_cpu_seconds_totalandgo_memstats_alloc_bytesfor debugging the agent's resource usage.
The easiest way to get started is by using the pre-configured dashboard provided in the DockFlare repository.
-
Find the Dashboard: The dashboard file is located at
examples/dashboard.json. -
Import to Grafana:
- Navigate to your Grafana instance.
- On the left menu, go to Dashboards.
- Click New -> Import.
- Either paste the JSON content or upload the
dashboard.jsonfile. - Select your Prometheus data source and click Import.
If you don't have a monitoring stack, you can use the following guide to set one up with Docker Compose.
Create these folders and files next to your main docker-compose.yml file:
.
├── docker-compose.yml # Your main compose file
├── prometheus.yml # New file
└── grafana-provisioning/ # New folder
└── datasources/ # New sub-folder
└── datasource.yml # New file
Add the following prometheus and grafana services to your existing docker-compose.yml:
services:
# ... your existing dockflare and other services ...
prometheus:
image: prom/prometheus:latest
container_name: prometheus
restart: unless-stopped
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
- ./prometheus_data:/prometheus # Persistent data for Prometheus
command:
- '--config.file=/etc/prometheus/prometheus.yml'
networks:
- your-dockflare-network # <-- IMPORTANT: Use the same network as DockFlare
labels:
- "dockflare.enable=true"
- "dockflare.hostname=prometheus.your-domain.com"
- "dockflare.service=http://prometheus:9090"
grafana:
image: grafana/grafana-oss:latest
container_name: grafana
restart: unless-stopped
volumes:
- ./grafana_data:/var/lib/grafana # Persistent data for Grafana
- ./grafana-provisioning:/etc/grafana/provisioning
networks:
- your-dockflare-network # <-- IMPORTANT: Use the same network as DockFlare
labels:
- "dockflare.enable=true"
- "dockflare.hostname=metrics.your-domain.com" # Exposes Grafana
- "dockflare.service=http://grafana:3000"If Grafana or Prometheus fail to start with a "Permission denied" error, you must set the correct ownership on the host directories before starting the containers.
# Stop the stack first docker-compose down # Set ownership for Grafana (user ID 472) sudo chown -R 472:472 ./grafana_data # Set ownership for Prometheus (user ID 65534) sudo chown -R 65534:65534 ./prometheus_data # Start the stack again docker-compose up -d
Create the prometheus.yml file. This tells Prometheus where to find your cloudflared agent.
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'cloudflared'
static_configs:
- targets: ['your-cloudflared-agent-name:2000']
# --- IMPORTANT ---
# 1. Replace 'your-cloudflared-agent-name' with the name of your agent container (e.g., 'cloudflared-agent-green-bern').
# 2. Replace '2000' with the port you set for CLOUDFLARED_METRICS_PORT.Create the grafana-provisioning/datasources/datasource.yml file. This automatically adds Prometheus as a data source in Grafana.
apiVersion: 1
datasources:
- name: Prometheus
type: prometheus
access: proxy
url: http://prometheus:9090
isDefault: true- Run
docker-compose up -d. - Navigate to your Prometheus URL (e.g.,
http://prometheus.your-domain.com). - Go to Status -> Targets. The
cloudflaredendpoint should be UP. - Navigate to your Grafana URL, log in (default:
admin/admin), and import the pre-built dashboard. You should see your tunnel's metrics