Cloudflare workers
  • 22 Mar 2023
  • 3 Minutes to read
  • PDF

Cloudflare workers

  • PDF

Article Summary

Cloudflare Workers is a serverless computing platform that enables developers to deploy and execute JavaScript code at the edge of Cloudflare's global network. Cloudflare Workers allow developers to write serverless functions that run directly on Cloudflare's edge servers. Cloudflare Workers can be used to build a wide range of applications, including serverless APIs, dynamic websites, and real-time applications. They are also highly scalable and can handle large volumes of traffic. Traceable integrates with Cloudflare worker to collect data for further analysis. The following is a high-level architecture diagram:

Traceable Cloudflare worker captures the data coming from Cloudflare and sends it to the Traceable Platform agent. Note that the request from the Cloudflare worker to Traceable Platform agent does not block the user request.


Before you begin

Make sure to take a note of the following points before proceeding with configuration:

  • Make sure that a website is available in Cloudflare. In Cloudflare, a website refers to a web application or a set of web pages that are hosted on a server and accessed by users over the internet.
  • Download the Traceable's JavaScript code for Cloudflare from Traceable's download site. Navigate to agent → cloudflare → latest.
  • Make sure that Traceable platform agent is installed and you have its URL. For more information, see Platform agent.
    Important
    Traceable Platform agent must be enabled for TLS and have the port number 443 available. You must use a signed CA certificate. Self-signed certificates and non-standard TLS port cannot be used in Cloudflare worker subrequests.
    Cloudflare Workers subrequests are a feature that allows workers to make additional HTTP requests from the serverless function running at the edge. When a worker makes a subrequest, it sends an HTTP request to another server and waits for the response. The response is then returned to the worker, which can process it and use it to generate a final response to the original request.

Configuration

Configuring Cloudflare worker for Traceable consists of two steps:

  1. Creating a service.
  2. Enabling worker on a website.

Step 1 – Create a service

  1. Log into your Cloudflare account.
  2. Click on the Worker menu and provide the service name.
  3. Select HTTP handler
  4. Click on Create Service.
  5. Click on Quick edit on the service page (traceable-global-worker).
  6. On the next page, add the JavaScript code that you downloaded in the Before you begin section. Make sure to configure Traceable Platform agent as shown below. You can optionally also configure the service name.
  7. Click on Save and deploy.

Step 2 – Enable worker on a website

Complete the following steps to enable the worker on a website:

  1. Click on the Website menu and select the Website for which you wish to enable the worker.
  2. On the Website overview page, click on Worker Routes menu.
  3. On the Worker Routes page, click on Add Route. Configure the Route, service, and environment and click on Save.
    1. If you wish to capture traffic on every subdomain and all the paths of your Website, use *your-webiste.com/*
    2. If you wish to capture traffic for a specific domain, use www.your-website.com/*
    3. If you wish to capture traffic for a subset of paths, you can use your-website.com/some-path/*

Verification and troubleshooting

Send traffic through your domain. Login to Traceable Platform to view the traffic.  Complete the following steps:

  1. Navigate to Catalog → API Endpoints
  2. Click on the Cloudflare API. On the API details page, click on the Traces tab. 
  3. Click on a trace to expand the trace details.
  4. Click on the Attributes tab to view and verify that the configuration was successful.

If request and response are not captured, then check the Content-Type header. Request and response bodies are captured only if a Content-Type header is present and includes either json or xml as a substring. You can configure additional types by modifying the capture_content_types configuration options of the Worker script mentioned above.

INSTALL_OPTIONS = {
        "tpa_address": "https://REPLACE_WITH_TPA_HOST",
        "enable_debug": false,
        "service_name": "website",
        "capture_content_types": ["json", "xml", "grpc"]
    };

Remove routes and service

If you wish to remove routes and service, complete the following steps:

Remove routes

  1. Navigate to Worker Routes page. Under HTTP Routes, click on Edit link for the route that you wish to remove. 
  2. Select the service and environment and click on Remove.

Remove a service

  1. Navigate to Cloudflare Dashboard and click on Workers.
  2. On the Workers page, select the service that you wish to remove.
  3. On the service page, click on Manage Service button.
  4. On the manage settings page, click on Delete under Delete Service section.

Was this article helpful?

What's Next