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

improv(tracer): remove require in tracer provider #2557

Merged
merged 2 commits into from
May 22, 2024

Conversation

dreamorosi
Copy link
Contributor

Summary

Changes

Please provide a summary of what's being changed

This PR makes a minor change in the Tracer X-Ray SDK provider to avoid using the require keyword in our codebase. This will help customers identify where the issue is coming from thanks to a more accurate stack trace that reflects the root cause of the issue.

As discussed in the linked issue, the previous implementation hardcoded the require keyword in both CJS and ESM builds, which caused a runtime error in ESM customer bundles. This was done under the misguided assumption that the X-Ray SDK monkey patching would not work when importing the http and https modules with the import keyword (my bad).

With this PR we import the modules with the import keyword in our source, and then let the TypeScript build operation decide whether to keep or transpile the import to require based on the build config. This results in the ESM build not needing the polyfill** and the CJS one correctly transpiled using the require keyword as usual.

Note

** After this fix customers wanting to bundle Tracer and AWS X-Ray SDK in their ESM bundles still need to add the polyfill banner. The change only affects where the runtime error is thrown, which is now moved to the X-Ray SDK and not in Powertools for AWS Tracer.

2024-05-20T15:37:41.635Z        undefined       ERROR   Uncaught Exception      {"errorType":"Error","errorMessage":"Dynamic require of \"util\" is not supported","stack":["Error: Dynamic require of \"util\" is not supported","    at file:///var/task/index.mjs:11:9","    at node_modules/cls-hooked/context.js (file:///var/task/index.mjs:270:16)","    at __require2 (file:///var/task/index.mjs:14:50)","    at node_modules/aws-xray-sdk-core/dist/lib/context_utils.js (file:///var/task/index.mjs:1862:15)","    at __require2 (file:///var/task/index.mjs:14:50)","    at node_modules/aws-xray-sdk-core/dist/lib/aws-xray.js (file:///var/task/index.mjs:7641:24)","    at __require2 (file:///var/task/index.mjs:14:50)","    at node_modules/aws-xray-sdk-core/dist/lib/index.js (file:///var/task/index.mjs:7947:22)","    at __require2 (file:///var/task/index.mjs:14:50)","    at file:///var/task/index.mjs:8924:40"]}
INIT_REPORT Init Duration: 215.49 ms    Phase: init     Status: error   Error Type: Runtime.Unknown
2024-05-20T15:37:43.187Z        undefined       ERROR   Uncaught Exception      {"errorType":"Error","errorMessage":"Dynamic require of \"util\" is not supported","stack":["Error: Dynamic require of \"util\" is not supported","    at file:///var/task/index.mjs:11:9","    at node_modules/cls-hooked/context.js (file:///var/task/index.mjs:270:16)","    at __require2 (file:///var/task/index.mjs:14:50)","    at node_modules/aws-xray-sdk-core/dist/lib/context_utils.js (file:///var/task/index.mjs:1862:15)","    at __require2 (file:///var/task/index.mjs:14:50)","    at node_modules/aws-xray-sdk-core/dist/lib/aws-xray.js (file:///var/task/index.mjs:7641:24)","    at __require2 (file:///var/task/index.mjs:14:50)","    at node_modules/aws-xray-sdk-core/dist/lib/index.js (file:///var/task/index.mjs:7947:22)","    at __require2 (file:///var/task/index.mjs:14:50)","    at file:///var/task/index.mjs:8924:40"]}

Please add the issue number below, if no issue is present the PR might get blocked and not be reviewed

Issue number: #2555


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@dreamorosi dreamorosi self-assigned this May 20, 2024
@dreamorosi dreamorosi requested review from a team as code owners May 20, 2024 17:25
@boring-cyborg boring-cyborg bot added the tracer This item relates to the Tracer Utility label May 20, 2024
@pull-request-size pull-request-size bot added the size/XS PR between 0-9 LOC label May 20, 2024
@github-actions github-actions bot added the enhancement PRs that introduce minor changes, usually to existing features label May 20, 2024
@dreamorosi dreamorosi requested a review from am29d May 20, 2024 17:44
@dreamorosi dreamorosi linked an issue May 20, 2024 that may be closed by this pull request
2 tasks
Copy link

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

@dreamorosi dreamorosi merged commit ed528a6 into main May 22, 2024
12 checks passed
@dreamorosi dreamorosi deleted the improv/tracer_require branch May 22, 2024 00:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement PRs that introduce minor changes, usually to existing features size/XS PR between 0-9 LOC tracer This item relates to the Tracer Utility
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Maintenance: remove require from Tracer provider
2 participants