aws / aws/aws-xray-sdk-node

`captureFetchGlobal` returns a wrapped `fetch` but still overrides global `fetch`

Open
#711 1 comment 3 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
280
Forks
157
PR merge metrics
No merged PRs in 30d

Description

I'm using [aws-xray-sdk-fetch](https://www.npmjs.com/package/aws-xray-sdk-fetch) to trace HTTPS requests that goes outside AWS.

My application (AWS Lambda) also uses the [AWS AppConfig Agent client](https://docs.aws.amazon.com/appconfig/latest/userguide/appconfig-agent-how-to-use.html) via another `fetch` call to `localhost`.

When using `captureFetchGlobal`, all `fetch` calls are being traced—including AppConfig Agent's `localhost` requests.

## Expected Behavior

I would like to only trace external `fetch` calls and not trace `localhost` calls.

## Current Behavior

With `captureFetchGlobal`, the global `fetch` is wrapped, causing all `fetch` requests to be traced, including those to `localhost`.

```js
import { captureFetchGlobal } from "aws-xray-sdk-fetch";

const fetch = captureFetchGlobal();

export default async function send(url, body) {
// ✅ I want to trace this fetch
const response = await fetch(url, {
method : "POST",
body : JSON.stringify(body),
headers : {
"Content-Type" : "application/json"
}
});

if (!response.ok) {
throw new Error(`${response.statusText} (${response.statusCode})` );
}
}
```

In another function:

```js
export default async function getAppConfigLatestJson() {
// ❌ I do NOT want to trace this fetch
const response = await fetch(url);
return await response.json();
}
```

## Issue

The `captureFetchGlobal` function returns a specific `fetch` function but still wraps the global `fetch` object. This behavior is **counterintuitive**, as I expected only the assigned `fetch` to be traced.

How to only trace the first `fetch`?

![image](https://github.com/user-attachments/assets/af64987b-302b-4208-87e5-933e9642b384)

## Suggested Improvement

It would be helpful if `captureFetchGlobal` could:
- Avoid mutating the global `fetch` in an additional `captureFetchStandalone` function and return a wrapped instance that can be selectively used.
- Alternatively, provide an option to filter which requests to trace (e.g., by hostname or URL pattern).

Contributor guide

Open the contributing guide

Research direction

Start at the captureFetchGlobal entry point and trace how it creates the returned fetch and mutates the global fetch. Compare the requested standalone behavior with hostname or URL filtering, then define completion as allowing external requests to be traced without tracing the localhost AppConfig requests.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
api, backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.