|
| 1 | +This guide provides step-by-step instructions to integrate Cloudflare with OpenObserve. |
| 2 | + |
| 3 | +## Overview |
| 4 | +The Cloudflare integration enables streaming of HTTP request logs, response codes, security events, and related dimensions from Cloudflare to OpenObserve. |
| 5 | + |
| 6 | +Cloudflare offers two methods to send logs to OpenObserve. The method you choose depends on your Cloudflare plan and setup preferences: |
| 7 | + |
| 8 | +- **GraphQL API**: Available on the following Cloudflare plans- Free, Pro, Business, and Enterprise. This method pulls logs from Cloudflare at regular intervals using an API script. |
| 9 | +- **Logpush**: Available only on Cloudflare Enterprise plans. This method pushes logs from Cloudflare to OpenObserve in real time using an HTTP endpoint. |
| 10 | + |
| 11 | +This integration can help you monitor performance, identify security issues, and troubleshoot problems by using real-time Cloudflare traffic logs in OpenObserve. |
| 12 | + |
| 13 | +## Prerequisites |
| 14 | +- **A Cloudflare account**: Sign up [here](https://dash.cloudflare.com/sign-up). |
| 15 | +- **An OpenObserve account**: Sign up [here](https://openobserve.ai/downloads/). |
| 16 | +- **A Cloudflare API token**: |
| 17 | + |
| 18 | + - To use the **GraphQL method**, create a token with the **Read analyticsand logs** permission |
| 19 | + - To use the **Logpush method** (Enterprise only), create a token with the **Edit Logs** permission. |
| 20 | + |
| 21 | +- **GraphQL-specific requirements**: |
| 22 | + |
| 23 | + - **Tools**: Python 3.8 or later, terminal access, and ability to use a virtual environment. |
| 24 | + - **Traffic Source**: A live website, a Cloudflare Worker, or an existing traffic. |
| 25 | + |
| 26 | +## Steps to Integrate |
| 27 | + |
| 28 | +### Step 1: Retrieve Your OpenObserve Endpoint and Credentials |
| 29 | +To stream Cloudflare logs, log in to OpenObserve and follow these steps: |
| 30 | + |
| 31 | +1. From the left menu, select **Data Sources > Custom > Logs > Curl**. |
| 32 | +2. Extract the following details from the sample curl command: |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | + - **Endpoint**: `https://api.openobserve.ai/api/<organization_name>/<stream_name>/_json`. |
| 37 | + Replace `organization_name` with the organization name shown at the top right corner of the screen. Replace the `stream_name` with **cloudflare_logs**. |
| 38 | + - **Credentials**: |
| 39 | + The sample includes a string in the format: `[email protected]:802gZ3uo4N5S917s6Med`. |
| 40 | + Here, the username is `[email protected]` and the password is `FNIN8MWspXZRKRgS`. |
| 41 | + |
| 42 | +### Step 2: Generate Cloudflare Logs |
| 43 | +To generate real logs for the GraphQL method, you need actual traffic routed through Cloudflare. Choose one of the following approaches: |
| 44 | + |
| 45 | +**Option 1: Live website traffic using any Cloudflare plan** |
| 46 | + |
| 47 | +1. Add a domain, such as example.com, in the Cloudflare dashboard. |
| 48 | +2. Create a proxied DNS A record that points to your server. The proxy status must show the orange cloud icon. |
| 49 | +3. Generate traffic by visiting the site, sharing the URL, or allowing users to access it. |
| 50 | +4. Logs are produced when requests reach Cloudflare’s edge. |
| 51 | + |
| 52 | +**Option 2: Cloudflare Worker on Free, Pro, Business, or Enterprise plans** |
| 53 | + |
| 54 | +1. In the Cloudflare dashboard, go to **Workers and Pages**, then select **Create Worker**. |
| 55 | +2. Assign a name, such as log-generator. |
| 56 | +3. Use the following code: |
| 57 | + |
| 58 | +```js linenums="1" |
| 59 | +addEventListener("fetch", event => { |
| 60 | +event.respondWith(handleRequest(event.request)); |
| 61 | +}); |
| 62 | + |
| 63 | +async function handleRequest(request) { |
| 64 | +const status = Math.random() > 0.8 ? 503 : 200; // 20% chance of 503 |
| 65 | +return new Response("Hello from Cloudflare!", { |
| 66 | + status: status, |
| 67 | + headers: { "Content-Type": "text/plain" } |
| 68 | +}); |
| 69 | +} |
| 70 | +``` |
| 71 | +4. Select **Deploy** and note the generated URL, such as `log-generator.example-subdomain.workers.dev`. |
| 72 | +5. You may optionally add a route under **Workers Routes**, such as `example.com/log/*`. |
| 73 | +6. Generate traffic by visiting the Worker URL, linking it on a webpage, or enabling access through a known route. |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | +**Option 3: Existing traffic on Business or Enterprise plans** |
| 79 | + |
| 80 | +- Use a domain or Cloudflare Worker that is already proxied through Cloudflare. |
| 81 | +- If the service already receives external traffic, no additional configuration is required. Logs will begin flowing automatically as requests are processed. |
| 82 | + |
| 83 | +**Verify traffic in Cloudflare** <br> |
| 84 | + |
| 85 | +1. From the Cloudflare dashboard, navigate to **Analytics** and select the **Traffic** tab. |
| 86 | +2. Verify that incoming request data appears. This usually takes only a few minutes. |
| 87 | + |
| 88 | +### Step 3: Stream Logs to OpenObserve |
| 89 | +There are two options to send logs from Cloudflare to OpenObserve. Click each tab to learn more: |
| 90 | + |
| 91 | +=== "Option 1: GraphQL API" |
| 92 | + **Generate API token**: |
| 93 | + |
| 94 | + 1. In the Cloudflare dashboard, go to **My Profile > API Tokens > Create Token**. |
| 95 | + 2. Select the **Read analytics** and logs template. Apply the token to all zones or selected zones. |
| 96 | + > **Note**: You can also create a custom token with the following scope: **Zone > Analytics > Read**. |
| 97 | + 3. Copy the API token. For example, `xUL39zImSqAG5-JJNpLMC7HVIqGuckorb9AKo-Tx`. |
| 98 | + |
| 99 | + **Set up environment** |
| 100 | + |
| 101 | + 1. Open a terminal and run the following commands to prepare the Python environment: |
| 102 | + |
| 103 | + ``` linenums="1" |
| 104 | + python3 -m venv venv |
| 105 | + source venv/bin/activate |
| 106 | + pip install requests |
| 107 | + ``` |
| 108 | + 2. Create a file named **cloudflare_logs_to_openobserve.py** and paste the following script: |
| 109 | + |
| 110 | + ``` linenums="1" |
| 111 | + import requests |
| 112 | + import time |
| 113 | + import json |
| 114 | + from datetime import datetime, timedelta, timezone |
| 115 | + import base64 |
| 116 | + import random |
| 117 | + |
| 118 | + # Cloudflare config |
| 119 | + API_TOKEN = "xUL39zImSqAG5-JJNpLMC7HVIqGuckorb9AKo-Tx" |
| 120 | + |
| 121 | + ZONE_IDS = [] # Specify zone IDs or leave empty for all zones |
| 122 | + |
| 123 | + # OpenObserve config |
| 124 | + OPENOBSERVE_URL = "https://api.openobserve.ai/api/your_organization_id/cloudflare_logs/_json" |
| 125 | + OPENOBSERVE_USER = "[email protected]" |
| 126 | + OPENOBSERVE_PASS = "your_password" |
| 127 | + |
| 128 | + def simulate_cloudflare_logs(): |
| 129 | + methods = ["GET", "POST", "PUT"] |
| 130 | + uris = ["/", "/api/users", "/checkout"] |
| 131 | + statuses = [200, 404, 429, 503] |
| 132 | + countries = ["US", "IN", "UK"] |
| 133 | + return [{ |
| 134 | + "dimensions": { |
| 135 | + "datetime": datetime.now(timezone.utc).isoformat(), |
| 136 | + "clientRequestHTTPMethodName": random.choice(methods), |
| 137 | + "clientRequestURI": random.choice(uris), |
| 138 | + "edgeResponseStatus": random.choice(statuses), |
| 139 | + "clientCountryName": random.choice(countries) |
| 140 | + }, |
| 141 | + "sum": { |
| 142 | + "bytes": random.randint(100, 5000), |
| 143 | + "requests": random.randint(1, 5) |
| 144 | + } |
| 145 | + } for _ in range(random.randint(1, 5))] |
| 146 | + |
| 147 | + def fetch_cloudflare_logs(zone_ids): |
| 148 | + url = "https://api.cloudflare.com/client/v4/graphql" |
| 149 | + headers = { |
| 150 | + "X-Auth-Email": EMAIL, |
| 151 | + "Authorization": f"Bearer {API_TOKEN}", |
| 152 | + "Content-Type": "application/json" |
| 153 | + } |
| 154 | + end_time = datetime.now(timezone.utc) |
| 155 | + start_time = end_time - timedelta(minutes=5) |
| 156 | + |
| 157 | + zone_filter = f'zoneTag: "{zone_ids[0]}"' if zone_ids else "" |
| 158 | + query = """ |
| 159 | + { |
| 160 | + viewer { |
| 161 | + zones(%s) { |
| 162 | + httpRequests1mGroups(limit: 1000, filter: {datetime_geq: "%s", datetime_leq: "%s"}) { |
| 163 | + dimensions { |
| 164 | + datetime |
| 165 | + clientRequestHTTPMethodName |
| 166 | + clientRequestURI |
| 167 | + edgeResponseStatus |
| 168 | + clientCountryName |
| 169 | + } |
| 170 | + sum { |
| 171 | + bytes |
| 172 | + requests |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + """ % ("filter: {%s}" % zone_filter if zone_filter else "", |
| 179 | + start_time.isoformat(), end_time.isoformat()) |
| 180 | + |
| 181 | + response = requests.post(url, headers=headers, json={"query": query}) |
| 182 | + if response.status_code != 200: |
| 183 | + print(f"API Error: {response.status_code} - {response.text}") |
| 184 | + return simulate_cloudflare_logs() |
| 185 | + try: |
| 186 | + data = response.json() |
| 187 | + if not data or not data.get("data", {}).get("viewer", {}).get("zones"): |
| 188 | + print("No logs yet. Using simulated data.") |
| 189 | + return simulate_cloudflare_logs() |
| 190 | + logs = [] |
| 191 | + for zone in data["data"]["viewer"]["zones"]: |
| 192 | + logs.extend(zone["httpRequests1mGroups"]) |
| 193 | + return logs |
| 194 | + except (json.JSONDecodeError, AttributeError): |
| 195 | + print("API response issue. Switching to simulation.") |
| 196 | + return simulate_cloudflare_logs() |
| 197 | + |
| 198 | + def send_to_openobserve(logs): |
| 199 | + if not logs: |
| 200 | + print("No logs to send.") |
| 201 | + return |
| 202 | + auth_str = base64.b64encode(f"{OPENOBSERVE_USER}:{OPENOBSERVE_PASS}".encode()).decode() |
| 203 | + headers = {"Authorization": f"Basic {auth_str}"} |
| 204 | + payload = [log["dimensions"] | log["sum"] for log in logs] |
| 205 | + response = requests.post(OPENOBSERVE_URL, headers=headers, json=payload) |
| 206 | + print(f"Sent {len(payload)} logs at {datetime.now(timezone.utc)}: {response.status_code}") |
| 207 | + |
| 208 | + if __name__ == "__main__": |
| 209 | + print("Streaming Cloudflare logs to OpenObserve...") |
| 210 | + while True: |
| 211 | + try: |
| 212 | + logs = fetch_cloudflare_logs(ZONE_IDS) |
| 213 | + send_to_openobserve(logs) |
| 214 | + time.sleep(60) |
| 215 | + except Exception as e: |
| 216 | + print(f"Error: {e}") |
| 217 | + logs = simulate_cloudflare_logs() |
| 218 | + send_to_openobserve(logs) |
| 219 | + time.sleep(60) |
| 220 | + ``` |
| 221 | + 3. In your terminal, run the above script: |
| 222 | + |
| 223 | + ``` |
| 224 | + python cloudflare_logs_to_openobserve.py |
| 225 | + ``` |
| 226 | + Logs transition from simulated to real as the traffic starts within 5 to 10 minutes. |
| 227 | + |
| 228 | + >(Optional) To run the script continuously in the background, use: |
| 229 | + |
| 230 | + >``` |
| 231 | + >nohup python cloudflare_logs_to_openobserve.py & |
| 232 | + >``` |
| 233 | + |
| 234 | +=== "Option 2: Logpush" |
| 235 | + **Generate an API Token**: |
| 236 | + |
| 237 | + 1. In the Cloudflare dashboard, go to **My Profile** and select **API Tokens**. |
| 238 | + 2. Click **Create Token**. |
| 239 | + 3. Use the **Edit Logs** template. You can also use a custom token by enabling the following permissions: |
| 240 | + **Account > Logs > Edit**. |
| 241 | + 4. Set the scope to your account or specific zones. |
| 242 | + 5. Copy the generated API token. |
| 243 | + |
| 244 | + **Configure Logpush**: |
| 245 | + |
| 246 | + 1. In the Cloudflare dashboard, go to **Analytics & Logs** > **Logpush**. |
| 247 | + 2. Click **Create a Logpush Job**. |
| 248 | + 3. Choose **HTTP Requests** dataset. |
| 249 | + 4. Select **HTTP destination**. |
| 250 | + 5. In the destination URL field, enter: |
| 251 | + `https://api.openobserve.ai/api/your_organization_id/cloudflare_logs/_json` |
| 252 | + 5. Add an Authorization header with the following values: |
| 253 | + |
| 254 | + - **Key**: Authorization |
| 255 | + - **Value**: Basic <base64-encoded-credentials> |
| 256 | + |
| 257 | + To encode your credentials, use the following command in a terminal: |
| 258 | + |
| 259 | + ``` linenums="1" |
| 260 | + echo -n "[email protected]:802gZ3uo4N5S917s6Med" | base64 |
| 261 | + ``` |
| 262 | + Save the job. Logpush will begin sending logs immediately. |
| 263 | + |
| 264 | +## Verify Logs in OpenObserve |
| 265 | +1. Log in to OpenObserve. |
| 266 | +2. Go to **Logs**. |
| 267 | +3. From the stream selector dropdown, select **cloudflare_logs**. |
| 268 | +4. Select the time range. |
| 269 | +5. Click **Run Query**. |
| 270 | +You should see entries similar to the following: |
| 271 | + |
| 272 | +```json linenums="1" |
| 273 | +{ |
| 274 | + "_timestamp": 1742220353596180, |
| 275 | + "bytes": 2109, |
| 276 | + "clientcountryname": "IN", |
| 277 | + "clientrequesthttpmethodname": "POST", |
| 278 | + "clientrequesturi": "/", |
| 279 | + "datetime": "2025-03-17T14:05:53.447689+00:00", |
| 280 | + "edgeresponsestatus": 503, |
| 281 | + "requests": 1 |
| 282 | +} |
| 283 | +``` |
| 284 | + |
| 285 | + |
| 286 | +Logs appear immediately when using Logpush. If you are testing with GraphQL, the simulated data appears first. Real logs are usually visible within 5 to 10 minutes. |
| 287 | + |
| 288 | +## Troubleshooting |
| 289 | + |
| 290 | +**GraphQL Integration**: |
| 291 | + |
| 292 | +- If the message No logs yet appears, check traffic under Analytics > Traffic. |
| 293 | +- If an API Error occurs, examine the full response using response.text. |
| 294 | + |
| 295 | +**Logpush Integration**: |
| 296 | + |
| 297 | +- Confirm the destination endpoint and authentication values in the Logpush job settings. |
| 298 | + |
| 299 | +**No Logs in OpenObserve**: |
| 300 | + |
| 301 | +- Verify that the organization ID and credentials are correct. |
0 commit comments