aws / aws-xray-sdk-node

The official AWS X-Ray SDK for Node.js.
Apache License 2.0
272 stars 155 forks source link

[fetch] does not support fetch options like proxy #650

Open Kruspe opened 6 months ago

Kruspe commented 6 months ago

When using the aws-xray-sdk-fetch instrumentation it is not possible to supply further options to the fetch call like the agent to supply the undici ProxyAgent.

The capturedFetch call only calls the global available fetch call with one argument https://github.com/aws/aws-xray-sdk-node/blob/73e1fcaba509a6b6fcfc81254a25da37189bea4b/sdk_contrib/fetch/lib/fetch_p.js#L117 and does not pass down options provided by the user.

I would propose to add a third option to the function that patches the global fetch. This would allow the user to use all fetch options and additionally supply their own segment information if required. https://github.com/aws/aws-xray-sdk-node/blob/73e1fcaba509a6b6fcfc81254a25da37189bea4b/sdk_contrib/fetch/lib/fetch_p.js#L59

For this change it would be necessary to think about how not to introduce a breaking change for users that expect the second argument to be used for segment information. But I guess we can write some internal logic to check the supplied args.

Once I receive some feedback I can get started on a PR and implement the changes.

jj22ee commented 5 months ago

Hi, you should be able to pass options into the fetch call. For example, the following code sample will change the method type used in fetch from GET to POST. This will be reflected in the trace as well.

const { captureFetchGlobal } = require('aws-xray-sdk-fetch');
const fetch = captureFetchGlobal();

...

  const result = fetch('https://someURL.com', {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify({data:123, data2:"234"}),
  });

The options are added into the fetch call through the Request object: https://github.com/aws/aws-xray-sdk-node/blob/73e1fcaba509a6b6fcfc81254a25da37189bea4b/sdk_contrib/fetch/lib/fetch_p.js#L63-L117

Kruspe commented 5 months ago

Hey, what you say is true but Request does not support the dispatcher option as far as I know.

With fetch you can set the dispatcher and the request will go through the Proxy.

import { ProxyAgent } from 'undici';

fetch("http://localhost", {
  dispatcher: new ProxyAgent("http://localhost:4444"),
}));

If we create a request with similar options like this:

import { ProxyAgent } from 'undici';

const request = new Request("http://localhost", {
  dispatcher: new ProxyAgent("http://localhost:4444"),
});

fetch(request)

The Request does not contain the dispatcher option and the request will not pass through the proxy.

So inside the capturedFetch function we will not traverse the proxy. While the calls to the baseFetchFunction should be able to use the proxy as they use all the provided args

jj22ee commented 5 months ago

I see. In this case, the fetch function should not automatically pass the arguments into a Request object as it currently does here: https://github.com/aws/aws-xray-sdk-node/blob/master/sdk_contrib/fetch/lib/fetch_p.js#L65

Instead, if the Fetch options are supplied like in your first example, the original object that is passed into the instrumented fetch client should be used when the baseFetchFunction is called.

We would welcome a PR from your end if you have a fix in mind!

Kruspe commented 5 months ago

Alright sounds good. I'll take a stab at it :)

Kruspe commented 5 months ago

Hey @jj22ee, just had some time and implemented a small tweak to the code that checks if the dispatcher option is passed to the fetch call and if so, it passes it to the fetch call. It would also be possible to do some more sophisticated parsing of the arguments but this would require more tweaks to the code base. So I thought I share my first idea and you can give me some feedback if the simple implementation in #653 is enough.

jj22ee commented 4 months ago

Closing issue as PR is merged. This fix will be in the next release.

jj22ee commented 1 week ago

Hi @Kruspe, we have reverted the change in https://github.com/aws/aws-xray-sdk-node/pull/687 in aws-xray-sdk-fetch@3.10.2 due to issue in https://github.com/aws/aws-xray-sdk-node/issues/679 (X-Amzn-Trace-Id header not propagated to downstream when not using a request object in Fetch).

I've re-opened this issue. I think we can take another look at the initial approach in your original PR (https://github.com/aws/aws-xray-sdk-node/pull/653/commits/627b6df9a4b254abb05ed57dc1058f0512729452) with some additional changes:

Kruspe commented 1 week ago

I'll take a look tomorrow and see what the issue is. Thanks for the heads up!

Kruspe commented 5 days ago

Hey @jj22ee, the PR is there to implement this again. Have a look at let me know what you think.

During my testing I noticed that we ignore headers that are set by users if they do this:

fetch(new Request("https://somewhere.com"), {headers: {"foo": "bar"}});

These headers will never reach the server as we ignore them. This is different from the original fetch implementation that still respects those headers and will add them to the request.

Maybe it would make more sense to add the X-Amzn-Trace-Id to the header argument of params instead of to the request directly and thus being able to always use the params when calling fetch()