Skip to content

Automated tracing library for Cloudflare Workers⚡️

License

Notifications You must be signed in to change notification settings

epsagon/epsagon-cloudflare

Repository files navigation


Epsagon Tracing for Cloudflare Workers

This package provides tracing to Cloudflare Workers for the collection of distributed tracing and performance metrics in Epsagon.

Contents

Installation

Installation is done via the usual npm install @epsagon/cloudflare.

Usage

To configure the package, you need to wrap your listener with the epsagon agent. So if your current code looks something like this:

addEventListener('fetch', (event) => {
  event.respondWith(handleRequest(event.request))
})

function handleRequest(request) {
  //your worker code.
}

You can change that to:

import { epsagon } from '@epsagon/cloudflare'

const epsagon_config = {
  token: 'epsagon-token',
  app_name: 'application-name',
}

const listener = epsagon(epsagon_config, (event) => {
  event.respondWith(handleRequest(event.request))
})

addEventListener('fetch', listener)

function handleRequest(request) {
  //your worker code.
}

Tracing Fetch Requests

To be able to associate the a subrequest with the correct incoming request, you will have to use the fetch defined on the tracer described above. The method on the tracer delegates all arguments to the regular fetch method, so the tracer.fetch function is a drop-in replacement for all fetch function calls.

Example:

async function handleRequest(request) {
  return request.tracer.fetch('link')
}