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

"mod.require is not a function" in database tracing integrations #3293

Closed
4 of 9 tasks
sergio-dreamcode opened this issue Mar 1, 2021 · 6 comments
Closed
4 of 9 tasks

Comments

@sergio-dreamcode
Copy link

Package + Version

  • @sentry/browser
  • @sentry/node
  • raven-js
  • raven-node (raven for node)
  • other: @sentry/serverless

Version:

6.2.1

Description

In the past we reported #2971, that issue was preventing us from upgrading Sentry. With the release of 6.0.4 some weeks ago things started to work like a charm.

Once we were able to upgrade, we added Sentry performance tracing to our applications (frontend and backend) and it worked perfectly, however database tracing was missing. In order to improve this, we added the Postgres tracing integration to the backend. After investigating we found that the same issue from #2971 still is happening in the tracing system, not in the same way as #2971 (it does not crash the library) but prevents Postgres tracing to work. We are using version 6.2.1.

After adding the Postgres tracing integration as the documentation recommends, we noticed that the Postgres tracing was not working and found the next error in our logs:

ERROR Sentry Logger [Error]: Postgres Integration was unable to require `pg` package.

After checking the Postgres integration source (same will happens for all database integrations) I found that it calls to the dynamicRequire util which in turn calls mod.require.

In order to validate that we were having the same error I cloned a local copy of sentry-javascript and added an error log to display the Javascript exception and linked this local copy to our project. These were the logs:

ERROR Sentry Logger [Error]: Postgres Integration was unable to require `pg` package.
ERROR Sentry Logger [Error]: TypeError: mod.require is not a function

The code that I modified to get the TypeError: mod.require is not a function error was in the postgres.ts file:

...
  public setupOnce(_: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
    let client: PgClient;

    try {
      const pgModule = dynamicRequire(module, 'pg') as { Client: PgClient };
      client = pgModule.Client;
    } catch (e) {
      logger.error('Postgres Integration was unable to require `pg` package.');
      logger.error(e); // <<<< the line I added to validate
      return;
    }
...

It is important to note that the mod.require error seems to only happen when using Webpack. More details about this issue can be found in #2971 and #2515

@kamilogorek
Copy link
Contributor

The only solution to this right now might be it - https://webpack.js.org/configuration/module/#moduleparserjavascriptcommonjsmagiccomments + us adding that comment in the codebase. In the next major release, every package will live in its own place, so there wont be no need for dynamicRequire and we'll be able to run require('pg') directly.

@kamilogorek
Copy link
Contributor

Closing the issue, as it seems like the original issue has been partially resolved or there is a working solution. I'd prefer someone to create a new issue with a fresh description if it's still an issue.
Please do not hesitate to ping me if it is still relevant, and I will happily reopen and work on it.
Cheers!

@aldenquimby
Copy link
Contributor

aldenquimby commented Sep 28, 2023

@kamilogorek I am running into this issue with esbuild (esbuild 0.17.19, sentry 7.71.0)

Would you be open to a PR that allows passing the resolved module directly as an option? e.g.

      new Sentry.Integrations.Postgres({ module: pg }),

This would be similar to how sequelize's dialectModule works.

Alternatively, do you know of a fix similar to commonjsMagicComments but for esbuild? Thank you!

The solution I'm using locally (unfortunate that ts types also have a mismatch):

import pg from 'pg';

class PostgresWithEsbuild extends Sentry.Integrations.Postgres {
  // @ts-ignore
  public loadDependency() {
    return pg;
  }
}

Sentry.init({
  ...
  integrations: [new PostgresWithEsbuild()],
});

@kamilogorek
Copy link
Contributor

cc @AbhiPrasad @lforst

@AbhiPrasad
Copy link
Member

@aldenquimby feel free to open a PR, we can take a look!

@aldenquimby
Copy link
Contributor

@AbhiPrasad here you go! #9227

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

4 participants