Sign inSign up

nulltix/cloudflare-prometheus-exporter

By nulltix

•Updated 3 months ago

Image
0

6.2K

nulltix/cloudflare-prometheus-exporter repository overview

⁠Cloudflare Prometheus Exporter

A Prometheus exporter for Cloudflare Analytics metrics, providing real-time HTTP and DNS analytics data from your Cloudflare zones.

Python Version License Renovate enabled Release Docker Pulls Security: CodeQL Security: Trivy codecov OpenSSF Scorecard

⁠Features

  • Multi-Zone Support: Monitor multiple Cloudflare zones.
  • Thread-Safe Metric Collection: Ensures safe data handling in multi-threaded environments.
  • Structured JSON Logging: Provides logs in a structured format for easier analysis.
  • Configurable via Environment Variables: Flexible configuration options for deployment.
  • Prometheus Metrics Exposure: Exposes HTTP, firewall, and quota metrics for monitoring.
  • Error Handling: Robust handling of API errors during metrics collection.
  • Health Checks: Monitors the status of the metrics server.

⁠Data Sampling

Important

Due to Cloudflare data sampling on Analytics GraphQL API, numbers reported by exporter are not exact, but rather close approximation. You could still rely on this data with high degree of confidence, but your dashboards and alerts should rely more on ratios and percentages, rather than on exact numbers (e.g. cache hit/miss ratio, [45]xx errors ratio).

Read more here: https://developers.cloudflare.com/analytics/graphql-api/sampling/⁠

⁠Configuration

Configuration is handled via environment variables or .env file:

VariableRequiredDefaultDescription
CF_API_TOKENYes-Cloudflare API token
CF_LISTEN_PORTNo8080Port to expose Prometheus metrics on (range: 1024-65535)
CF_LOG_LEVELNoINFOLogging level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
CF_MAX_WORKERSNo5Maximum number of concurrent worker threads (range: 3-10)
CF_API_URLNoURL⁠Cloudflare GraphQL API endpoint
CF_CMB_REGIONNoglobalRegion for CMB compliance ('global', 'eu', or 'us')
CF_SCRAPE_DELAYNo60Scrape interval in seconds (60-300, must be multiple of 60)
CF_ZONESNo-Comma-separated list of zone IDs to monitor
CF_EXCLUDE_ZONESNo-Comma-separated list of zone IDs to exclude
CF_EXCLUDE_DATASETSNo-Comma-separated list of datasets to exclude

Note

Zone IDs can be found in the [Cloudflare dashboard Overview page](https://developers.cloudflare.com/fundamentals/setup/find-account-and-zone-ids/) under the API section. They are 32-character hexadecimal strings.
⁠Cloudflare Metadata Boundary (CMB) Compliance

This exporter supports Cloudflare's data residency requirements through CMB regions. For a complete list of available datasets and their regional availability, see Cloudflare CMB GraphQL Datasets documentation⁠.

Choose the appropriate CF_CMB_REGION based on your compliance requirements.

⁠Required Permissions

The Cloudflare API token needs the following permissions:

Account:

  • Analytics:Read
  • Account Settings:Read
  • Billing:Read

Zone:

  • Analytics:Read
  • Firewall Services:Read

⁠Supported Metrics

Generic Labels for All Metrics:

  • zone - Cloudflare zone name
  • account - Cloudflare account name
  • account_id - Cloudflare account ID
Metric NameCustom Labels SetDescription
cloudflare_requests_totalcountry, statusTotal number of HTTP requests made to the Cloudflare service.
cloudflare_bytes_totalcountry, statusTotal amount of data (in bytes) transferred through the Cloudflare service.
cloudflare_cached_requests_totalcountry, statusTotal number of HTTP requests that were served from the cache.
cloudflare_cached_bytes_totalcountry, statusTotal amount of data (in bytes) transferred from the cache.
cloudflare_firewall_events_totalaction, rule_id, sourceTotal number of events triggered by the firewall rules.
cloudflare_enterprise_zone_quota_maxNoneMaximum quota allowed for the enterprise zone.
cloudflare_enterprise_zone_quota_currentNoneCurrent usage of the enterprise zone quota.
cloudflare_enterprise_zone_quota_availableNoneRemaining quota available for use in the enterprise zone.

⁠Usage

# Start the exporter
uv run python -m cloudflare_exporter.main

The exporter will start serving metrics on http://localhost:8080/metrics (or configured port).

⁠Docker
# Build the image
docker buildx build --platform linux/amd64,linux/arm64 -t cloudflare-exporter:latest .

# Run the container
docker run -p 8080:8080 --env-file .env cloudflare-exporter

⁠Helm Chart

The Cloudflare Prometheus Exporter Helm chart is available for download from our GitHub Pages repository:

You can install the chart using the following command:

helm repo add cloudflare-exporter https://n0zz.github.io/cloudflare-prometheus-exporter
helm install my-release cloudflare-exporter/cloudflare-prometheus-exporter

⁠Development

⁠Prerequisites
  • Python 3.13⁠
  • uv⁠ - Python package manager
  • just⁠ - Command runner
  • Helm⁠ - Kubernetes package manager (optional, for chart development)
  • yq⁠ - YAML processor (optional, for chart template validation)
  • Trivy⁠ - Vulnerability scanner (optional, for local security scans)
⁠Setup
# Install dependencies
uv sync

# List available commands
just
⁠Commands
# Run unit tests
just unit-test

# Run integration tests (requires valid Cloudflare token, see below)
just integration-test

# Run type checking
just typecheck

# Format code
just format

# Run linting
just lint

# Run security checks (dependencies and code analysis)
just security-check

# Run Trivy vulnerability scan on repo
just trivy-scan

# Run Trivy vulnerability scan on Docker image
just trivy-image

# Build the Helm chart
just build-helm-chart

# Build Docker image
just build-docker-image
⁠Integration Tests

Integration tests run against the real Cloudflare API and require a valid CF_API_TOKEN.

Locally: Create a .env file with your token and run just integration-test.

CI: Integration tests run automatically in GitHub Actions if the CF_API_TOKEN secret is configured in the repository settings. If the secret is not set, the job is skipped gracefully. To enable them in your fork, add a CF_API_TOKEN repository secret under Settings > Secrets and variables > Actions.

⁠Security

This project includes several security scanning tools:

  1. Trivy - Scans for:

    • Container vulnerabilities
    • Infrastructure as Code issues
    • Git repository secrets
    • Software composition analysis (SCA)
  2. Bandit - Static application security testing (SAST) for Python code

  3. Zizmor - GitHub Actions workflow security auditing (action pinning, injection risks, permissions)

Security scans run:

  • On every push to main
  • On every pull request
  • Weekly (scheduled) for the main branch
  • Manually via just security-check

Results are available in the GitHub Security tab.

⁠Prometheus Configuration

When deployed via Helm, the chart includes a ServiceMonitor resource — no additional configuration is needed if you use Prometheus Operator.

For standalone Prometheus without the operator, add a scrape config to your prometheus.yml:

scrape_configs:
  - job_name: 'cloudflare'
    static_configs:
      - targets: ['localhost:8080']
    scrape_interval: 60s

⁠Grafana Dashboard

Grafana dashboard: Grafana Cloud⁠

Dashboard

⁠Further Reading

⁠Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Install dependencies: uv sync
  4. Run tests and linting: just test and just lint
  5. Commit your changes following the commit lint guidelines (git commit -m 'feat: add amazing feature')
  6. Push to the branch (git push origin feature/amazing-feature)
  7. Open a Pull Request

⁠License

This project is licensed under the MIT License - see the LICENSE⁠ file for details.

⁠Support

For support, please open an issue on the GitHub repository or contact the maintainers.

Tag summary

Content type

Image

Digest

sha256:6da98637b…

Size

52.6 MB

Last updated

3 months ago

docker pull nulltix/cloudflare-prometheus-exporter