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

fix(instrumentation): do not export Node.js types for Browser users #4378

Conversation

pichlermarc
Copy link
Member

Which problem is this PR solving?

InstrumentationNodeModuleFile and InstrumentationNodeModuleDefinition were exported even if the @opentelemetry/instrumentation package is used in a browser context. This PR changes it so that the Node type are not exported for Browser users anymore.

Fixes #4373

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

@pichlermarc pichlermarc requested a review from a team as a code owner December 18, 2023 13:21
@pichlermarc pichlermarc added bug Something isn't working priority:p1 Bugs which cause problems in end-user applications such as crashes, data inconsistencies, etc pkg:instrumentation labels Dec 18, 2023
Copy link

codecov bot commented Dec 18, 2023

Codecov Report

Merging #4378 (acd8fd8) into main (3cf2cf6) will increase coverage by 0.08%.
The diff coverage is n/a.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4378      +/-   ##
==========================================
+ Coverage   92.23%   92.32%   +0.08%     
==========================================
  Files         332      330       -2     
  Lines        9467     9454      -13     
  Branches     2011     2010       -1     
==========================================
- Hits         8732     8728       -4     
+ Misses        735      726       -9     

see 3 files with indirect coverage changes

@pichlermarc
Copy link
Member Author

I changed the test file back to what it was before to get the tests to run. As I felt like I'm just hiding a problem by doing that, I double checked if it actually works in a new ESM project with Node.js v20, and it does not.

SyntaxError: The requested module '@opentelemetry/instrumentation' does not provide an export named 'InstrumentationBase'

But still, looking at the transpiled files, I'm exporting them (and it's working with older vesions). @dyladan since you worked on #4336 - do you know the reason why this wouldn't work on Node.js 20 but works fine on older versions?

@pichlermarc
Copy link
Member Author

Ah this seems to be related to the loader changes that made it into Node 18.19.0.

So that's why the test also failed for Node 18 (https://github.com/open-telemetry/opentelemetry-js/actions/runs/7248698310/job/19745127352#step:8:4690). When I create an ESM app and import it there, it works with Node.js 18.18.2.

Maybe it has something to do with the loader or how it is set up? 🤔 If I don't use it, it works fine. (nothing is instrumented, obviously - but the import works)

@pichlermarc
Copy link
Member Author

Ah, I see - this may be due to DataDog/import-in-the-middle#29
Looks like it has not been released yet (I'll test locally, then put the results here).

So it looks like we're in kind of a tough spot at the moment:

  • Browser users are broken until we fix it
  • Fixing it makes browser usage work again, but breaks users that instrument ESM on Node.js v18.19 and Node.js v20

An alternative may be #3989 (switching to functional ESM output likely means we can circumvent the ITTM issue, but I've not tested it yet - I'll test it if the IITM fix does not do what I think it does)

@Flarna
Copy link
Member

Flarna commented Dec 18, 2023

An alternative may be #3989 (switching to functional ESM output likely means we can circumvent the ITTM issue, but I've not tested it yet - I'll test it if the IITM fix does not do what I think it does)

I doubt. Switching to ESM would break all CJS users as CJS can't use ESM.
The ESM instrumentation topics would persist because ESM customization hooks in node.js are still an ongoing topic. As a result breaking changes might happen.

@pichlermarc
Copy link
Member Author

An alternative may be #3989 (switching to functional ESM output likely means we can circumvent the ITTM issue, but I've not tested it yet - I'll test it if the IITM fix does not do what I think it does)

I doubt. Switching to ESM would break all CJS users as CJS can't use ESM.

Fair point. I did not word that correctly: I meant to say double publishing CJS and ESM (not switching to) so that ESM users may use an ESM version of the @opentelemetry/instrumentation package (thinking of the approach from this comment here: #3989 (comment)), but CJS users may continue to use the CJS version.

The ESM instrumentation topics would persist because ESM customization hooks in node.js are still an ongoing topic. As a result breaking changes might happen.

True. Right now, I think the way to go would be to fix browser users.

From what I can tell, even with an ESM version double-published, using IITM right now would break when a

which may be more than a handful, not just @opentelemetry/instrumentation.

I've actually tried the changes from DataDog/import-in-the-middle#30 in the meantime, that'd fix the issue of handling re-exports.

Comment on lines +17 to +18
export { InstrumentationNodeModuleFile } from './instrumentationNodeModuleFile';
export { InstrumentationNodeModuleDefinition } from './instrumentationNodeModuleDefinition';
Copy link
Member

Choose a reason for hiding this comment

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

The platform folder should not have any exports for 1 platform but not another. All exports should be used exactly identically. The same code that uses the node platform exports will also use the browser platform exports.

Comment on lines +18 to +19
// Exports for Node.js and Browser are different, therefore exporting *
export * from './platform/index';
Copy link
Member

Choose a reason for hiding this comment

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

This is not the way the platform-specific exports work and will cause things to break

Copy link
Member Author

Choose a reason for hiding this comment

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

Edit: some background info first: the problem is that we use path.normalize in InstrumentationNodeModuleDefinition, which does not exist in the browser.

@dyladan I see. We actually rely on this (different exports for different platforms) quite a bit in other packages in this repo (exporter packages, mostly), so we should likely also get on top of this there. It is also (in spirit) the way we had it before it was before we merged #4336.

From what I can tell there are only three other ways for us to fix this then:

  • provide and maintain our own normalize implementation
  • rely on a third-party package that provides an agnostic implementation
  • normalize in Node.js while using a no-op in the browser (this does not seem like a safe option)

WDYT?

Copy link
Member

Choose a reason for hiding this comment

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

Before #4336 we actually had the bad situation I'm trying to avoid and I had to fix it to get that PR to pass.

normalize in Node.js while using a no-op in the browser (this does not seem like a safe option)

Why do you think this is unsafe? There is no valid reason to use a NodeModuleFile in browser anyway and it would be completely broken in other ways. The only thing we need to fix is that we shouldn't import path in browser contexts.

Copy link
Member Author

Choose a reason for hiding this comment

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

Looking closer into this, I agree that it's likely fine if we do that (no-op in browser). Closing this PR, will re-open another one with that approach 👍

@JamieDanielson
Copy link
Member

For reference, new PR is #4386

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working pkg:instrumentation priority:p1 Bugs which cause problems in end-user applications such as crashes, data inconsistencies, etc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Instrumentation] Cannot build web project using webpack due to reference to nodejs core package (path)
4 participants