# Monitor Cloudflare Workers using Prometheus Exporter

> Complete guide to monitor Cloudflare workers using Prometheus Exporter

Source: https://last9.io/blog/monitor-cloudflare-workers-using-prometheus-exporter/

Here's a detailed blog post on monitoring [Cloudflare Workers](https://workers.cloudflare.com/) using Prometheus Exporter. We discuss the data flow of how [Prometheus Cloudflare Exporter](https://github.com/lablabs/cloudflare-exporter) fetches metrics from your Cloudflare account; then, by scraping the exporter, we [remote write](https://last9.io/blog/what-is-prometheus-remote-write/) to a long-term metrics storage like Levitate.

## Monitoring Cloudflare Workers

Traditionally, Prometheus-based monitoring systems periodically scrape targets and pull metrics. However, this strategy does not work well with Cloudflare Workers. Cloudflare Workers are serverless, which means they run only when invoked. Very similar to Functions as a Service.

> I have a written a post on [monitoring Google Cloud Functions with Prometheus.](https://last9.io/blog/monitor-google-cloud-functions-using-prometheus-and-pushgateway/)

The metrics published by Cloudflare workers are not in the Prometheus format, so we need a [Prometheus exporter](https://last9.io/blog/best-practices-using-and-writing-prometheus-exporters/) to convert them into Prometheus format and then scrape them.

The following diagram explains the data flow.

### Cloudflare Prometheus Exporter

Since Cloudflare Workers are part of Cloudflare infrastructure, they publish their metrics to the respective customer accounts. These metrics can be queried using Cloudflare Monitoring APIs, which is precisely what the [Cloudflare Exporter](https://github.com/lablabs/cloudflare-exporter) does. This exporter frequently queries your Cloudflare account for metrics, and depending upon your Cloudflare worker usage, the corresponding metrics and their values are part of the query result. As with any Prometheus Exporter, the collected metrics are available on the `/metrics` endpoint and a default port for any scraper to scrape.

The exporter needs the Cloudflare API Key to perform the query operations. A complete list of configuration options can be found [here](https://github.com/lablabs/cloudflare-exporter#configuration).

### Installation

The Cloudflare Exporter can be deployed as a containerized workload or as a binary running as a `systemctl` service on a virtual machine. We can follow the instructions as per the documentation provided [here](https://github.com/lablabs/cloudflare-exporter#docker).

### Prometheus Scrape Job

We can configure [Prometheus Agent](https://last9.io/docs/integrations/observability/prometheus/) to scrape metrics from the target, i.e., the deployed instance of Cloudflare Exporter. When Prometheus performs a scrape operation, it sends an HTTP request to the Exporter's `/metrics` endpoint, which returns all the current metric data.

Below is an example scrape config.

```yaml
global:
  scrape_interval: 1m
  scrape_timeout: 30s

scrape_configs:
  - job_name: "cloudflare-exporter"
    static_configs:
      - targets: ["<internal-ip-cloudflare-exporter-vm>:9090"]
```

If you are running this in Kubernetes, you can use this [Helm Chart](https://github.com/lablabs/cloudflare-exporter#helm-chart-repository).

### Remote Write Metrics

Prometheus has a feature called ["remote write"](https://last9.io/blog/what-is-prometheus-remote-write/) which allows it to send the metrics data it collects to a remote data store; in this case, we use [Levitate](https://last9.io/metrics/).

> Follow the document to [send metrics using Prometheus Agent](https://last9.io/docs/integrations/observability/prometheus/).

## What are the available metrics?

You can refer to the [Cloudflare Exporter](https://github.com/lablabs/cloudflare-exporter#list-of-available-metrics) GitHub repository, which maintains a list of available metrics. Here are some Cloudflare Worker-related metrics.

```
# CPU time quantiles by script name
cloudflare_worker_cpu_time

# Duration quantiles by script name (GB*s)
cloudflare_worker_duration

# Number of errors by script name
cloudflare_worker_errors_count

# Number of requests sent to worker by script name
cloudflare_worker_requests_count
```

A complete list of metrics can be found [here.](https://github.com/lablabs/cloudflare-exporter#list-of-available-metrics)

## Grafana Dashboard

The good folks who built the Cloudflare Prometheus Exporter have also published a Grafana dashboard. It can be downloaded from [here](https://grafana.com/grafana/dashboards/13133-cloudflare-zone-analytics/).

## Conclusion

Using the Cloudflare Prometheus exporter, you can monitor your Cloudflare Workers. You can also push them to long-term storage, like [Levitate](https://last9.io/metrics/), for durability and long-term analysis.
