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

Expose node/deno global to the other mode #20826

Open
timfish opened this issue Oct 8, 2023 · 3 comments
Open

Expose node/deno global to the other mode #20826

timfish opened this issue Oct 8, 2023 · 3 comments

Comments

@timfish
Copy link

timfish commented Oct 8, 2023

We're attempting to publish a Deno specific Sentry SDK.

The Sentry SDK currently saves state to globalThis and monkey patches console/fetch to instrument these but changes earlier this year to segregate globals has complicated things.

If we publish an npm-only Deno SDK, we don't have access to instrument the "deno-mode" global.
If we publish on deno.land, we won't have access to instrument the "node-mode" global.

It appears that the only way we can instrument both globals and capture breadcrumbs/spans for both deno/node code is to create a library that spans both modes!

It would be super helpful if we could access the global from the other mode.

Deno.global.deno / Deno.global.node?

@timfish timfish changed the title Expose node/deno globals to the other mode Expose node/deno global to the other mode Oct 8, 2023
@lucacasonato
Copy link
Member

They are the same global. Depending on whether you access it from "deno code" or "node code" you'll receive different values for certain properties (such as console). This segregation only applies to any of these properties:

  • Buffer (node only)
  • clearImmediate (node only)
  • clearInterval (both, but different implementation)
  • clearTimeout (both, but different implementation)
  • console (both, but different implementation)
  • global (node only)
  • performance (both, but different implementation)
  • process (node only)
  • setImmediate (node only)
  • setInterval (both, but different implementation)
  • setTimeout (both, but different implementation)
  • window (deno only)

I assume you are trying to instrument console?

@timfish
Copy link
Author

timfish commented Oct 9, 2023

Yes, we're trying instrument console.

The fetch instrumentation appears to not be working working too although that might be for other reasons.

@lucacasonato
Copy link
Member

Fetch should be unaffected by this - please check that the reason it is not working is not for other reasons.

Right now, what I can suggest you do is the following:

// in an npm module
import { readDenoGlobal, writeDenoGlobal } from "data:application/javascript,export function readDenoGlobal(key) { return globalThis[key] }; export function writeDenoGlobal(key, value) { globalThis[key] = value; }";

function readNodeGlobal(key) {
  return globalThis[key];
}

function writeNodeGlobal(key, value) {
  globalThis[key] = value;
}

This little hack works because the functions defined in the data url module are executed in "deno mode" while the top level functions in this module are executed in "node mode" because they are defined in an NPM module.

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

No branches or pull requests

2 participants