AWS X-Ray client for Cloudflare Workers using
fetch
.
npm install @borderless/worker-aws-xray --save
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);
});
MIT