Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch #590

Merged
merged 30 commits into from
Mar 9, 2024
Merged

Fetch #590

merged 30 commits into from
Mar 9, 2024

Conversation

jasonterando
Copy link
Contributor

Issue #, if available: 585

Description of changes: Add fetch patch, including support for built-in fetch API for NodeJS current versions and node-fetch for older NodeJS versions.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@jasonterando jasonterando requested a review from a team as a code owner May 9, 2023 14:25
@@ -1,6 +1,6 @@
{
"name": "aws-xray-sdk-core",
"version": "3.5.0",
"version": "3.4.1",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This corresponds to the current release version (3.5.0) and should never be changed to a prior version - this version number is only modified by the X-Ray team when preparing a new release. I imagine this is what is causing most of the CI failures.

To fix this, please keep this version as is - 3.5.0. We have an sdk_contrib directory that houses all community contributed instrumentations, so it would be great if this instrumentation could be moved there.

Also thought it would be worth mentioning since we do not usually maintain instrumentations in sdk_contrib, would you be willing to maintain it going forward and keep track of any issues that come in?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: Do you want to create a patchers directory in sdk_contrib for this, since all of the existing contributions are all middleware hooks?

package.json Outdated
@@ -21,6 +21,7 @@
"aws-xray-sdk-core": "3.5.0",
"aws-xray-sdk-express": "3.5.0",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Following up to the comment on moving to sdk_contrib, the dependencies that are unique to this instrumentation would then be placed in the package.json of the new instrumentation package in sdk_contrib.
You can reference this fastify instrumentation PR as a guide to help

* This module patches the global fetch instance for NodeJS 18+
*/

var contextUtils = require('../context_utils');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: use const instead of var

Comment on lines 32 to 35
"mocha": "^10.2.0",
"semver": "^5.3.0",
"tsd": "^0.28.1",
"typescript": "^5.0.4"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mocha, tsd, and typescript are not needed here since they get pulled from the root package.json

@jasonterando
Copy link
Contributor Author

Hi, my wife and I are on vacation for our 25th anniversary. I had to leave the computer at home lol. I will be able to knock these out at the end of the month, sorry for the delay.

@codecov-commenter
Copy link

codecov-commenter commented May 30, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 83.49%. Comparing base (4644c81) to head (216f75e).

❗ Current head 216f75e differs from pull request most recent head dd7f457. Consider uploading reports for the commit dd7f457 to get more accurate results

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #590   +/-   ##
=======================================
  Coverage   83.49%   83.49%           
=======================================
  Files          37       37           
  Lines        1805     1805           
=======================================
  Hits         1507     1507           
  Misses        298      298           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jasonterando
Copy link
Contributor Author

I'm going to try and resurrect this. Let me know if this seems feasible or if I should abandon. Thanks!

@jj22ee
Copy link
Contributor

jj22ee commented Dec 18, 2023

Hi, can you address @carolabadeer's comment:
#590 (comment)

@jasonterando
Copy link
Contributor Author

Hi, can you address @carolabadeer's comment: #590 (comment)

Done and committed.

@jj22ee
Copy link
Contributor

jj22ee commented Dec 19, 2023

Hi, can you address @carolabadeer's comment: #590 (comment)

Done and committed.

You may have addressed the comment below the comment I linked. chai-as-promised can be removed from the package.json/package-lock.json.

packages/core/tsconfig.debug.json

Was this file intended to be removed when you moved your contributions to sdk_contrib folder, which I see you've added the same tsconfig.debug.json there? If so, can you remove this file.

@jasonterando
Copy link
Contributor Author

Hi, can you address @carolabadeer's comment: #590 (comment)

Done and committed.

You may have addressed the comment below the comment I linked. chai-as-promised can be removed from the package.json/package-lock.json.

packages/core/tsconfig.debug.json

Was this file intended to be removed when you moved your contributions to sdk_contrib folder, which I see you've added the same tsconfig.debug.json there? If so, can you remove this file.

Committed removal of chai-as-promised and tsconfig.debug.json

@jj22ee
Copy link
Contributor

jj22ee commented Dec 20, 2023

Can you look into the failing tests? There seems to be an issue with activeFetch.
Also, packages/core/tsconfig.debug.json wasn't actually removed in your latest commit.

All similar:

   1) Unit tests
       captured fetch
         short circuits if headers include trace ID:
     TypeError: activeFetch is not a function
      at Context.<anonymous> (test\unit\fetch_p.test.js:186:13)
      at processImmediate (node:internal/timers:466:21)
      at process.callbackTrampoline (node:internal/async_hooks:130:17)

  2) Unit tests
       captured fetch
         calls base function when no parent and automatic mode:
     TypeError: activeFetch is not a function
      at Context.<anonymous> (test\unit\fetch_p.test.js:195:13)
      at processImmediate (node:internal/timers:466:21)
      at process.callbackTrampoline (node:internal/async_hooks:130:17)

@jj22ee
Copy link
Contributor

jj22ee commented Jan 23, 2024

Thanks, just a few more small changes in addition to the new comments.

A previous request was to remove chai-as-promised from package.json/package-lock.json. Looks like you instead removed it from sdk_contrib/fetch/package.json instead. Can you move this dependency back into sdk_contrib?

Also please remove the following files

  • packages/core/tsconfig.debug.json
  • .dockerignore
  • sdk_contrib/fetch/package-lock.json (usually we just keep the top level package-lock.json)
  • packages/core/package-lock.json

@jasonterando
Copy link
Contributor Author

Ugh... I'll have to look at the tests failing after the updated npm test script later tonight

@jasonterando
Copy link
Contributor Author

I believe this line is failing:
TypeError: Cannot stub non-existent property resolveManualSegmentParams
because the updated aws-xray.js which adds the export for resolveManualSegmentParams is probably not being used in the pipeline. Do we need to release that first before it's available for use by sdk_contrib, or is there some "trick" I can do in package.json to make this work?

@jj22ee
Copy link
Contributor

jj22ee commented Jan 24, 2024

Do we need to release that first before it's available for use by sdk_contrib, or is there some "trick" I can do in package.json to make this work?

Yeah, releasing "export for resolveManualSegmentParams" would be the more proper way of addressing this issue. The riskier and less ideal workaround would be to merge+release your PR, then hope that the Continuous Build Github Workflows will pass after the new release. We'll see if there's a better alternative.

@jasonterando
Copy link
Contributor Author

jasonterando commented Jan 24, 2024 via email

@jj22ee
Copy link
Contributor

jj22ee commented Feb 27, 2024

@jasonterando
New X-Ray SDK version is released, and I added a couple code changes to unblock your PR's unit tests
https://github.com/aws/aws-xray-sdk-node/blob/master/CHANGELOG.md#354

I looked into the segment generated by this PR, and I noticed that the Fetch Response information was missing in the segment.

"subsegments": [
    {
        "id": "22a15354bc8781eb",
        "name": "random-endpoint.com",
        "start_time": 1708993528.985,
        "end_time": 1708993529.44,
        "http": {
            "request": {
                "url": "",
                "method": "GET"
            },
            "response": {}
        },
        "namespace": "remote"
    }
]

I saw that this is because the subsegment.addRemoteRequestData() method was designed specifically for the built-in HTTP module Request/Response, and Fetch has a different Request/Response object. Therefore the data (request url/method and response status/content-length) must be obtained differently.

See how it is done for HTTP:

  • Subsegment.prototype.addRemoteRequestData = function addRemoteRequestData(req, res, downstreamXRayEnabled) {
    this.http = new RemoteRequestData(req, res, downstreamXRayEnabled);
    if ('traced' in this.http.request) {
    this.traced = this.http.request.traced;
    delete this.http.request.traced;
    }
    };
  • RemoteRequestData.prototype.init = function init(req, res, downstreamXRayEnabled) {
    this.request = {
    url: (req.agent && req.agent.protocol) ? (req.agent.protocol + '//' + (req.host || req.getHeader('host')) + stripQueryStringFromPath(req.path)) : '',
    method: req.method || '',
    };
    if (downstreamXRayEnabled) {
    this.request.traced = true;
    }
    if (res) {
    this.response = getHttpResponseData(res);
    }
    };

Do you have bandwidth anytime to implement extracting Fetch request/response data into subsegment and corresponding unit tests? Otherwise I could take a stab at it later.

@jasonterando
Copy link
Contributor Author

@jj22ee - yeah I can take a look at it, probably not until the weekend though, day job is hectic. Should I work from fetch-pr?

@jj22ee
Copy link
Contributor

jj22ee commented Feb 28, 2024

You can still work from this PR's fetch branch. As for adding your own custom addRemoteRequestData() method for Fetch, you can add it to fetch_p.js to modify the subsegment's http property

@jasonterando
Copy link
Contributor Author

Hi, just committed updates that write to subsegment.http. One thing that wasn't clear was how traced is supposed to work. It looks like RemoteRequestData.init sets request.traced to true if downstreamXRayEnabled) is true, but then traced gets subsequently moved to the subsegment in addRemoteRequestData. In my code, I'm just setting subsegment.traced to true if downstreamXRayEnabled is true. If that's wrong, and I need to do the "two-step", let me know.

Subsegment.prototype.addFetchRequestData = function addFetchRequestData(request, response, downstreamXRayEnabled) {
this.http = {
request: {
url: request.url.toString(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
url: request.url.toString(),
url: (request.url) ? request.url.toString() : '',

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, done. I also put in a non-set check for method. .d.ts file is added for Subsegment.addFetchRequestData

@jj22ee
Copy link
Contributor

jj22ee commented Mar 9, 2024

Thanks for adding subsegment_fetch.js! Added one suggestion above.
Also, can you add the corresponding subsegment_fetch.d.ts file. After that, everything looks good to merge.

…r method are not set store '' in http data
Copy link
Contributor

@jj22ee jj22ee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@jj22ee jj22ee merged commit 17599cb into aws:master Mar 9, 2024
13 checks passed
@jj22ee
Copy link
Contributor

jj22ee commented Mar 9, 2024

TYSM for the contribution @jasonterando!

@am29d
Copy link

am29d commented Mar 20, 2024

Thanks a lot @jasonterando for the effort and @jj22ee for the review. Truly appreciate the merge ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants