powertools-lambda-typescript icon indicating copy to clipboard operation
powertools-lambda-typescript copied to clipboard

Bug: Fetch traces only capture the hostname of the request URL

Open gruntster opened this issue 1 year ago • 2 comments

Expected Behavior

The request URL captured by the trace of a Fetch request should be the URL of the request.

Current Behavior

The request URL captured by the trace of a Fetch request is only the requested hostname.

Code snippet

await fetch("https://aws.amazon.com/free?test=1");

Steps to Reproduce

  1. Set AWS_XRAY_DEBUG_MODE environment variable to true of an instrumented Lambda function.
  2. Perform requests using fetch and node:https
await fetch("https://aws.amazon.com/free?test=1");
await axios.get("https://aws.amazon.com/free?test=1");
  1. Compare output of subsegments.

fetch emits incorrect request URL:

        {
          "id": "45b22f447784c44f",
          "name": "aws.amazon.com",
          "start_time": 1724108234.18,
          "namespace": "remote",
          "http": {
            "request": {
              "url": "aws.amazon.com",
              "method": "GET"
            },
            "response": {
              "status": 301,
              "content_length": 0
            }
          },
          "end_time": 1724108234.849
        }

node:https emits correct request URL:

        {
          "id": "e861c05fa31a14ab",
          "name": "aws.amazon.com",
          "start_time": 1724108234.856,
          "namespace": "remote",
          "http": {
            "request": {
              "url": "https://aws.amazon.com/free",
              "method": "GET"
            },
            "response": {
              "status": 301,
              "content_length": 0
            }
          },
          "end_time": 1724108235.218
        }

Possible Solution

Modify the onRequestStart function of the Tracer's ProviderService to generate a URL rather than use the hostname.

For example, modify to the following:

  const origin = getOriginURL(request.origin + request.path);
...
  (subsegment as HttpSubsegment).http = {
    request: {
      url: `${origin.protocol}//${origin.hostname}${origin.pathname}`,
      method,
    },
  };
...

Powertools for AWS Lambda (TypeScript) version

2.7.0

AWS Lambda function runtime

20.x

Packaging format used

npm

Execution logs

No response

gruntster avatar Aug 20 '24 01:08 gruntster

Thanks for opening your first issue here! We'll come back to you as soon as we can. In the meantime, check out the #typescript channel on our Powertools for AWS Lambda Discord: Invite link

boring-cyborg[bot] avatar Aug 20 '24 01:08 boring-cyborg[bot]

Hi @gruntster, thank you for taking the time to report the issue.

I looked into it and I am able to reproduce the behavior you described. When making requests with fetch, the trace data is indeed missing the path/pathname.

I'll be working on a fix before this week's release.

dreamorosi avatar Aug 20 '24 08:08 dreamorosi

⚠️ COMMENT VISIBILITY WARNING ⚠️

This issue is now closed. Please be mindful that future comments are hard for our team to see.

If you need more assistance, please either tag a team member or open a new issue that references this one.

If you wish to keep having a conversation with other community members under this issue feel free to do so.

github-actions[bot] avatar Sep 10 '24 15:09 github-actions[bot]

This is now released under v2.8.0 version!

github-actions[bot] avatar Sep 16 '24 12:09 github-actions[bot]