# 🌥 Cloudflare Workers + qryn

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1668290919322/DiiniYU0g.png?width=250  align="left")

## 🌥 Cloudflare Workers

Cloudflare Workers provides a serverless execution environment that allows you to create [web applications and services](https://developers.cloudflare.com/workers/tutorials) without configuring or maintaining infrastructure by using Cloudflare’s global worldwide cloud network.

In this first episode of this seriese we will explore some useful ways to monitor our **Cloudflare** traffic only using **Workers** and our polyglot stack **[qryn](https://qryn.dev)** 

<br>

### 1. Dashflare

Our first option is [Dashflare](https://github.com/jorgelbg/dashflare), a privacy first analytics solution for monitoring Cloudflare traffic using Edge Workers attached to arbitrary routes and using Grafana for Dashboards and Loki for data storage - meaning its drop-in compatible with **qryn**!

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1668283254307/gotSZ_ULh.png align="left")

You can deploy `Dashflare` workers on your Cloudflare account in a single click:

[![Deploy to Cloudflare Workers](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/jorgelbg/dashflare)


#### 🤠 Configuration

We just need to configure a few ENV parameter to get started:

- `LOKI_HOST`: Secure URL for qryn (i.e https://qryn.glitch.me)
- `CLIENT_ID`: Set to any value. By default it is set to `fake`
- `FINGERPRINT`: Its used as the key for the session id hash calculation. 
- `DOMAIN`: URL of your "primary" domain on cloudflare for routes. 

#### 🗺 Routes
When the edge worker is deployed, we need to "forward" the requests from the main domain to the edge worker domain using cloudflare routes. Default routes will be generated using the `DOMAIN` environment variable.



#### 🔎 Explore

**Dashflare** includes a ready to use [pre-built Grafana dashboard](https://github.com/jorgelbg/dashflare/tree/master/grafana/dashboards)
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1668361901132/TAd8s-_ZS.png align="left")

<br>


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1668364598266/oY9gKn-rw.png?width=300 align="left")

### 2. Logpush / Edge Log Delivery
Our second option is [Cloudflare LogPush / Edge Log Delivery](https://developers.cloudflare.com/logs/get-started/enable-destinations/datadog/) which  supports pushing logs to storage services and log management providers such as **qryn**.

Edge Log Delivery can be configured to deliver logs directly to your destination without first flowing through either of our US or EU core data centers. This means that your logs are delivered from data centers inside your selected region to your destination.

#### 🤠 Configuration
Enable a supported [destination](https://developers.cloudflare.com/logs/get-started/enable-destinations/) using the Cloudflare Dashboard. 

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1668366478665/2-whnKear.png align="left")

**qryn** supports the **Datadog** and **New Relic** formats and API for ingesting logs.

##### Enable `Logpush` to **Datadog** via the dashboard.

- Log in to the Cloudflare dashboard.
  - Select the domain you want to use with **Logpush**.
  - Go to **Analytics > Logs**.
  - Click **Connect a service**. 

  A modal window opens where you will need to complete several steps.

- Select the `dataset` and `data fields` to include in your logs. 
- Select **Datadog** as the Delivery target.

  Enter or select the following destination information:

  - Datadog/qryn URL Endpoint, ie:  `https://qryn:3100/v1/input`
  - Click **Validate access** + **Save and Start Pushing** to enable Logpush.

  Once connected, check status using `Logs > Logpush`. 


#### 🔎 Explore

Cloudflare events can be explored and using the powerful LogQL query language

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1668361913587/wgd2BpfEX.png align="left")

There's no preset Grafana dashboard yet _(any takers?)_ but using the [Datadog dashboards](https://developers.cloudflare.com/fundamentals/data-products/analytics-integrations/datadog/) as reference we can easily track and display:

* **Threats** _(by type, threats by country)_
* **Requests** _(requests, cache state, top countries, content types)_
* **Bandwidth** _(bandwidth, encryption, cache states)_
* **Caching** _(hit rate, request caching rate over time)_
* **DNS** _(queries, response time, top hostnames by type, stale vs. uncached)_
* **HTTP responses** status errors
* **Page views**, Bot Traffic, Search Engines

<br>

### 3. DIY Worker

Cloudflare Workers are fun and easy to develop, with lots of examples available. 

You can easily build your own custom solution in a few lines of code. A good reference is [boscaiolog](https://github.com/metrico/boscaiolog) which provides a micro API to stream logs, metrics and telemetry in just a couple lines of code.

<img src="https://user-images.githubusercontent.com/1423657/200393618-3a88e968-44e4-4701-ad14-5a9fd8b427b7.png">

<br>

### Conclusion

**qryn** and its polyglot core can transparently ingest **Cloudflare logs**. 

Since the forwarding is performed by Cloudflare Workers and LogPush, no change are required to the website or code of your monitored webapplication.

Try this example and many more from the comfort of your screen using **qryn cloud**

<a href="https://qryn.cloud" target="_blank"><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1668301668184/J3GpPBIaz.png"></a>
