Skip to content

AWS X-Ray client for Cloudflare Workers using `fetch`

License

Notifications You must be signed in to change notification settings

borderless/worker-aws-xray

Repository files navigation

Worker AWS X-Ray

NPM version NPM downloads Build status Test coverage

AWS X-Ray client for Cloudflare Workers using fetch.

Installation

npm install @borderless/worker-aws-xray --save

Usage

import { AwsXray, Segment, captureFetch } from "@borderless/worker-aws-xray";

// Capture `fetch` with tracing support.
const fetch = captureFetch(globalThis.fetch);

// Create an AWS X-Ray client wrapper (for sending segments later).
const client = new AwsXray({
  region: "us-west-2",
  accessKeyId: "abc",
  secretAccessKey: "123",
});

addEventListener("fetch", async (event) => {
  const segment = new Segment("worker");

  // Create a subsegment attached to `segment`.
  const subsegment = segment.startSegment();

  // Trace `fetch` requests with our captured client.
  const res = await fetch("http://example.com", { segment: subsegment });

  // Segments must be ended before tracing.
  subsegment.end();
  segment.end();

  // Forward traced segments to AWS.
  event.waitUntil(client.traceSegment(segment));

  // Finally respond to Cloudflare request.
  event.respondWith(res);
});

License

MIT