INTEGRITY Cloudflare Docs

Export to PostHog

PostHog is a product analytics platform that helps you understand user behavior and debug issues. By exporting your Cloudflare Workers application telemetry to PostHog, you can:

PostHog logs view with attributes expanded and a timeline view at the top

This guide will walk you through configuring your Cloudflare Worker application to export OpenTelemetry-compliant logs to PostHog.

Prerequisites

Before you begin, ensure you have:

Step 1: Get your PostHog project API key

  1. Log in to your PostHog account
  2. Navigate to the Project settings
  3. Find your Project API key in the project details section
  4. Copy the API key - this is the same key used for capturing events and exceptions

The API key should look something like: phc_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Step 2: Determine your PostHog region endpoint

PostHog has different endpoints depending on your data region:

Region Logs Endpoint
US (default) https://us.i.posthog.com/i/v1/logs
EU https://eu.i.posthog.com/i/v1/logs

You can find your region in your PostHog project settings or by checking the URL when logged into PostHog (either us.posthog.com or eu.posthog.com).

Step 3: Configure Cloudflare Logs destination

Now you'll create a destination in the Cloudflare dashboard that points to PostHog.

  1. Navigate to your Cloudflare account's Workers Observability section
  2. Click Add destination
  3. Configure your logs destination:
    • Destination Name: posthog-logs (or any descriptive name)
    • Destination Type: Select Logs
    • OTLP Endpoint: Your PostHog logs endpoint (e.g., https://us.i.posthog.com/i/v1/logs or https://eu.i.posthog.com/i/v1/logs)
    • Custom Headers: Add the authentication header:
      • Header name: Authorization
      • Header value: Bearer <your-project-api-key> (e.g., Bearer phc_xxxxx...)
  4. Click Save
Cloudflare destination configuration for PostHog logs with destination name, type selection, OTLP endpoint, and custom headers

Step 4: Configure your Worker

With your destination created in the Cloudflare dashboard, update your Worker's configuration to enable logs export.

{
  "observability": {
    "logs": {
      "enabled": true,
      // Must match the destination name in the dashboard
      "destinations": ["posthog-logs"]
    }
  }
}
[observability.logs]
enabled = true
destinations = [ "posthog-logs" ]

After updating your configuration, deploy your Worker for the changes to take effect.

Step 5: View logs in PostHog

Once your Worker is deployed and receiving traffic:

  1. Log in to your PostHog account
  2. Navigate to the Logs section in the left sidebar
  3. Your Worker logs will appear with severity levels, timestamps, and attributes

You can filter logs by:

Adding custom attributes to logs

You can add custom attributes to your logs using standard console methods with structured data:

export default {
  async fetch(request, env) {
    // Basic logging
    console.log("Processing request");

    // Logs with additional context
    console.info("User action", {
      userId: "user_123",
      action: "api_call",
      path: new URL(request.url).pathname
    });

    // Error logging with details
    console.error("Request failed", {
      error: "Connection timeout",
      retryCount: 3
    });

    return new Response("OK");
  }
};

These attributes will be searchable and filterable in the PostHog logs interface.

Troubleshooting

Logs not appearing in PostHog

  1. Verify your API key: Ensure you're using your project API key (starts with phc_), not a personal API key
  2. Check the endpoint region: Confirm you're using the correct regional endpoint (US or EU) matching your PostHog instance
  3. Confirm destination status: In the Cloudflare dashboard, verify your destination shows a recent successful delivery
  4. Check sampling rate: If you've configured a sampling rate, not all logs may be sent

Authentication errors

If you see authentication errors in your destination status: