@borderless/worker-aws-xray
v1.1.1
Published
AWS X-Ray client for Cloudflare Workers using `fetch`
Downloads
128
Maintainers
Readme
Worker AWS X-Ray
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