getsentry / sentry-javascript

Official Sentry SDKs for JavaScript
https://sentry.io
MIT License
7.68k stars 1.49k forks source link

Mixing node and edge instrumentations causes webpack errors #11823

Closed r34son closed 2 weeks ago

r34son commented 3 weeks ago

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/nextjs

SDK Version

8.0.0-beta.4

Framework Version

No response

Link to Sentry event

No response

SDK Setup

import { init } from '@sentry/nextjs'; import { nodeProfilingIntegration } from '@sentry/profiling-node'; import { SENTRY_CAPTURE_RATE, SENTRY_DSN } from 'sentry.constants.mjs';

export function register() { if (process.env.NEXT_RUNTIME === 'nodejs') { init({ dsn: SENTRY_DSN, debug: false, tracesSampleRate: SENTRY_CAPTURE_RATE, profilesSampleRate: SENTRY_CAPTURE_RATE, environment: process.env.ENV, integrations: [nodeProfilingIntegration()], }); } if (process.env.NEXT_RUNTIME === 'edge') { init({ dsn: SENTRY_DSN, tracesSampleRate: SENTRY_CAPTURE_RATE, environment: process.env.ENV, }); } }

Steps to Reproduce

https://docs.sentry.io/platforms/javascript/guides/nextjs/migration/v7-to-v8/#opentelemetry-instrumentation 3 step says to remove instrumentation.node.ts. Tried it and it causes errors because now webpack emits one chunk for edge and node instrumentation. And it fails because node api is not available in edge.

Before i was using:

export async function register() {
  if (process.env.NEXT_RUNTIME === 'nodejs') {
    await import('./instrumentation.node');
  }
  if (process.env.NEXT_RUNTIME === 'edge') {
    await import('./instrumentation.edge');
  }
}

Should i use it? but i am not sure that this dynamic imports are good way to init sdk

Expected Result

Build without errors

Actual Result

next build

▲ Next.js 14.3.0-canary.28

./node_modules/.pnpm/detect-libc@2.0.3/node_modules/detect-libc/lib/detect-libc.js:6:1 Module not found: Can't resolve 'child_process' 4 | 'use strict'; 5 |

6 | const childProcess = require('child_process'); | ^ 7 | const { isLinux, getReport } = require('./process'); 8 | const { LDD_PATH, readFile, readFileSync } = require('./filesystem'); 9 |

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module: ./node_modules/.pnpm/@sentry+profiling-node@8.0.0-beta.4/node_modules/@sentry/profiling-node/lib/esm/index.js ./src/instrumentation.ts

./node_modules/.pnpm/detect-libc@2.0.3/node_modules/detect-libc/lib/filesystem.js:6:1 Module not found: Can't resolve 'fs' 4 | 'use strict'; 5 |

6 | const fs = require('fs'); | ^ 7 | 8 | /* 9 | The path where we can find the ldd

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module: ./node_modules/.pnpm/detect-libc@2.0.3/node_modules/detect-libc/lib/detect-libc.js ./node_modules/.pnpm/@sentry+profiling-node@8.0.0-beta.4/node_modules/@sentry/profiling-node/lib/esm/index.js ./src/instrumentation.ts

node:module Module build failed: UnhandledSchemeError: Reading from "node:module" is not handled by plugins (Unhandled scheme). Webpack supports "data:" and "file:" URIs by default. You may need an additional plugin to handle "node:" URIs. at /Users/seitasanov/ownProjects/profile/nodemodules/.pnpm/next@14.3.0-canary.28@babel+core@7.24.1@opentelemetry+api@1.8.0@playwright+test@1.43.1_rea_h4jxyf2ocyapuotaulkocqpl24/node_modules/next/dist/compiled/webpack/bundle5.js:28:401799 at Hook.eval [as callAsync] (eval at create (/Users/seitasanov/ownProjects/profile/nodemodules/.pnpm/next@14.3.0-canary.28@babel+core@7.24.1@opentelemetry+api@1.8.0@playwright+test@1.43.1_rea_h4jxyf2ocyapuotaulkocqpl24/node_modules/next/dist/compiled/webpack/bundle5.js:13:28858), :6:1) at Object.processResource (/Users/seitasanov/ownProjects/profile/nodemodules/.pnpm/next@14.3.0-canary.28@babel+core@7.24.1@opentelemetry+api@1.8.0@playwright+test@1.43.1_rea_h4jxyf2ocyapuotaulkocqpl24/node_modules/next/dist/compiled/webpack/bundle5.js:28:401724) at processResource (/Users/seitasanov/ownProjects/profile/nodemodules/.pnpm/next@14.3.0-canary.28@babel+core@7.24.1@opentelemetry+api@1.8.0@playwright+test@1.43.1_rea_h4jxyf2ocyapuotaulkocqpl24/node_modules/next/dist/compiled/loader-runner/LoaderRunner.js:1:5308) at iteratePitchingLoaders (/Users/seitasanov/ownProjects/profile/nodemodules/.pnpm/next@14.3.0-canary.28@babel+core@7.24.1@opentelemetry+api@1.8.0@playwright+test@1.43.1_rea_h4jxyf2ocyapuotaulkocqpl24/node_modules/next/dist/compiled/loader-runner/LoaderRunner.js:1:4667) at runLoaders (/Users/seitasanov/ownProjects/profile/nodemodules/.pnpm/next@14.3.0-canary.28@babel+core@7.24.1@opentelemetry+api@1.8.0@playwright+test@1.43.1_rea_h4jxyf2ocyapuotaulkocqpl24/node_modules/next/dist/compiled/loader-runner/LoaderRunner.js:1:8590) at NormalModule._doBuild (/Users/seitasanov/ownProjects/profile/nodemodules/.pnpm/next@14.3.0-canary.28@babel+core@7.24.1@opentelemetry+api@1.8.0@playwright+test@1.43.1_rea_h4jxyf2ocyapuotaulkocqpl24/node_modules/next/dist/compiled/webpack/bundle5.js:28:401586) at NormalModule.build (/Users/seitasanov/ownProjects/profile/nodemodules/.pnpm/next@14.3.0-canary.28@babel+core@7.24.1@opentelemetry+api@1.8.0@playwright+test@1.43.1_rea_h4jxyf2ocyapuotaulkocqpl24/node_modules/next/dist/compiled/webpack/bundle5.js:28:403614) at /Users/seitasanov/ownProjects/profile/nodemodules/.pnpm/next@14.3.0-canary.28@babel+core@7.24.1@opentelemetry+api@1.8.0@playwright+test@1.43.1_rea_h4jxyf2ocyapuotaulkocqpl24/node_modules/next/dist/compiled/webpack/bundle5.js:28:82061 at NormalModule.needBuild (/Users/seitasanov/ownProjects/profile/nodemodules/.pnpm/next@14.3.0-canary.28@babel+core@7.24.1@opentelemetry+api@1.8.0@playwright+test@1.43.1_rea_h4jxyf2ocyapuotaulkocqpl24/node_modules/next/dist/compiled/webpack/bundle5.js:28:407746)

Import trace for requested module: node:module ./node_modules/.pnpm/@sentry+profiling-node@8.0.0-beta.4/node_modules/@sentry/profiling-node/lib/esm/index.js

node:path Module build failed: UnhandledSchemeError: Reading from "node:path" is not handled by plugins (Unhandled scheme). Webpack supports "data:" and "file:" URIs by default. You may need an additional plugin to handle "node:" URIs. at /Users/seitasanov/ownProjects/profile/nodemodules/.pnpm/next@14.3.0-canary.28@babel+core@7.24.1@opentelemetry+api@1.8.0@playwright+test@1.43.1_rea_h4jxyf2ocyapuotaulkocqpl24/node_modules/next/dist/compiled/webpack/bundle5.js:28:401799 at Hook.eval [as callAsync] (eval at create (/Users/seitasanov/ownProjects/profile/nodemodules/.pnpm/next@14.3.0-canary.28@babel+core@7.24.1@opentelemetry+api@1.8.0@playwright+test@1.43.1_rea_h4jxyf2ocyapuotaulkocqpl24/node_modules/next/dist/compiled/webpack/bundle5.js:13:28858), :6:1) at Object.processResource (/Users/seitasanov/ownProjects/profile/nodemodules/.pnpm/next@14.3.0-canary.28@babel+core@7.24.1@opentelemetry+api@1.8.0@playwright+test@1.43.1_rea_h4jxyf2ocyapuotaulkocqpl24/node_modules/next/dist/compiled/webpack/bundle5.js:28:401724) at processResource (/Users/seitasanov/ownProjects/profile/nodemodules/.pnpm/next@14.3.0-canary.28@babel+core@7.24.1@opentelemetry+api@1.8.0@playwright+test@1.43.1_rea_h4jxyf2ocyapuotaulkocqpl24/node_modules/next/dist/compiled/loader-runner/LoaderRunner.js:1:5308) at iteratePitchingLoaders (/Users/seitasanov/ownProjects/profile/nodemodules/.pnpm/next@14.3.0-canary.28@babel+core@7.24.1@opentelemetry+api@1.8.0@playwright+test@1.43.1_rea_h4jxyf2ocyapuotaulkocqpl24/node_modules/next/dist/compiled/loader-runner/LoaderRunner.js:1:4667) at runLoaders (/Users/seitasanov/ownProjects/profile/nodemodules/.pnpm/next@14.3.0-canary.28@babel+core@7.24.1@opentelemetry+api@1.8.0@playwright+test@1.43.1_rea_h4jxyf2ocyapuotaulkocqpl24/node_modules/next/dist/compiled/loader-runner/LoaderRunner.js:1:8590) at NormalModule._doBuild (/Users/seitasanov/ownProjects/profile/nodemodules/.pnpm/next@14.3.0-canary.28@babel+core@7.24.1@opentelemetry+api@1.8.0@playwright+test@1.43.1_rea_h4jxyf2ocyapuotaulkocqpl24/node_modules/next/dist/compiled/webpack/bundle5.js:28:401586) at NormalModule.build (/Users/seitasanov/ownProjects/profile/nodemodules/.pnpm/next@14.3.0-canary.28@babel+core@7.24.1@opentelemetry+api@1.8.0@playwright+test@1.43.1_rea_h4jxyf2ocyapuotaulkocqpl24/node_modules/next/dist/compiled/webpack/bundle5.js:28:403614) at /Users/seitasanov/ownProjects/profile/nodemodules/.pnpm/next@14.3.0-canary.28@babel+core@7.24.1@opentelemetry+api@1.8.0@playwright+test@1.43.1_rea_h4jxyf2ocyapuotaulkocqpl24/node_modules/next/dist/compiled/webpack/bundle5.js:28:82061 at NormalModule.needBuild (/Users/seitasanov/ownProjects/profile/nodemodules/.pnpm/next@14.3.0-canary.28@babel+core@7.24.1@opentelemetry+api@1.8.0@playwright+test@1.43.1_rea_h4jxyf2ocyapuotaulkocqpl24/node_modules/next/dist/compiled/webpack/bundle5.js:28:407746)

Import trace for requested module: node:path ./node_modules/.pnpm/@sentry+profiling-node@8.0.0-beta.4/node_modules/@sentry/profiling-node/lib/esm/index.js

node:url Module build failed: UnhandledSchemeError: Reading from "node:url" is not handled by plugins (Unhandled scheme). Webpack supports "data:" and "file:" URIs by default. You may need an additional plugin to handle "node:" URIs. at /Users/seitasanov/ownProjects/profile/nodemodules/.pnpm/next@14.3.0-canary.28@babel+core@7.24.1@opentelemetry+api@1.8.0@playwright+test@1.43.1_rea_h4jxyf2ocyapuotaulkocqpl24/node_modules/next/dist/compiled/webpack/bundle5.js:28:401799 at Hook.eval [as callAsync] (eval at create (/Users/seitasanov/ownProjects/profile/nodemodules/.pnpm/next@14.3.0-canary.28@babel+core@7.24.1@opentelemetry+api@1.8.0@playwright+test@1.43.1_rea_h4jxyf2ocyapuotaulkocqpl24/node_modules/next/dist/compiled/webpack/bundle5.js:13:28858), :6:1) at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (/Users/seitasanov/ownProjects/profile/nodemodules/.pnpm/next@14.3.0-canary.28@babel+core@7.24.1@opentelemetry+api@1.8.0@playwright+test@1.43.1_rea_h4jxyf2ocyapuotaulkocqpl24/node_modules/next/dist/compiled/webpack/bundle5.js:13:26012) at Object.processResource (/Users/seitasanov/ownProjects/profile/nodemodules/.pnpm/next@14.3.0-canary.28@babel+core@7.24.1@opentelemetry+api@1.8.0@playwright+test@1.43.1_rea_h4jxyf2ocyapuotaulkocqpl24/node_modules/next/dist/compiled/webpack/bundle5.js:28:401724) at processResource (/Users/seitasanov/ownProjects/profile/nodemodules/.pnpm/next@14.3.0-canary.28@babel+core@7.24.1@opentelemetry+api@1.8.0@playwright+test@1.43.1_rea_h4jxyf2ocyapuotaulkocqpl24/node_modules/next/dist/compiled/loader-runner/LoaderRunner.js:1:5308) at iteratePitchingLoaders (/Users/seitasanov/ownProjects/profile/nodemodules/.pnpm/next@14.3.0-canary.28@babel+core@7.24.1@opentelemetry+api@1.8.0@playwright+test@1.43.1_rea_h4jxyf2ocyapuotaulkocqpl24/node_modules/next/dist/compiled/loader-runner/LoaderRunner.js:1:4667) at runLoaders (/Users/seitasanov/ownProjects/profile/nodemodules/.pnpm/next@14.3.0-canary.28@babel+core@7.24.1@opentelemetry+api@1.8.0@playwright+test@1.43.1_rea_h4jxyf2ocyapuotaulkocqpl24/node_modules/next/dist/compiled/loader-runner/LoaderRunner.js:1:8590) at NormalModule._doBuild (/Users/seitasanov/ownProjects/profile/nodemodules/.pnpm/next@14.3.0-canary.28@babel+core@7.24.1@opentelemetry+api@1.8.0@playwright+test@1.43.1_rea_h4jxyf2ocyapuotaulkocqpl24/node_modules/next/dist/compiled/webpack/bundle5.js:28:401586) at NormalModule.build (/Users/seitasanov/ownProjects/profile/nodemodules/.pnpm/next@14.3.0-canary.28@babel+core@7.24.1@opentelemetry+api@1.8.0@playwright+test@1.43.1_rea_h4jxyf2ocyapuotaulkocqpl24/node_modules/next/dist/compiled/webpack/bundle5.js:28:403614) at /Users/seitasanov/ownProjects/profile/nodemodules/.pnpm/next@14.3.0-canary.28@babel+core@7.24.1@opentelemetry+api@1.8.0@playwright+test@1.43.1_rea_h4jxyf2ocyapuotaulkocqpl24/node_modules/next/dist/compiled/webpack/bundle5.js:28:82061

Import trace for requested module: node:url ./node_modules/.pnpm/@sentry+profiling-node@8.0.0-beta.4/node_modules/@sentry/profiling-node/lib/esm/index.js

Build failed because of webpack errors

r34son commented 3 weeks ago

Problem with @sentry/profiling-node integration

JonasBa commented 3 weeks ago

@r34son profiling will not work without a nodejs API, so I would recommend you remove this for edge runtime. I will need to look at how and if it's even possible to support this. I'm sorry if this isnt of much help.

r34son commented 3 weeks ago

@r34son profiling will not work without a nodejs API, so I would recommend you remove this for edge runtime. I will need to look at how and if it's even possible to support this. I'm sorry if this isnt of much help.

It is actually the problem, because of one chunk emited for both runtimes

lforst commented 2 weeks ago

It should be fine to inti the edge SDK with a dynamic import. If possible, try to leave the node SDK init synchronous.

r34son commented 2 weeks ago

Thanks!

r34son commented 2 weeks ago

If I init sdk this way:

import { init } from '@sentry/nextjs';
import { nodeProfilingIntegration } from '@sentry/profiling-node';
import { SENTRY_CAPTURE_RATE, SENTRY_DSN } from 'sentry.constants.mjs';

export async function register() {
  if (process.env.NEXT_RUNTIME === 'nodejs') {
    init({
      dsn: SENTRY_DSN,
      debug: false,
      tracesSampleRate: SENTRY_CAPTURE_RATE,
      profilesSampleRate: SENTRY_CAPTURE_RATE,
      environment: process.env.ENV,
      integrations: [nodeProfilingIntegration()],
    });
  }
  if (process.env.NEXT_RUNTIME === 'edge') {
    await import('./instrumentation.edge');
  }
}

It somehow fails in turbopack:

image
r34son commented 2 weeks ago

Only works importing @sentry/profiling-node dynamically:

import { init } from '@sentry/nextjs';
import { SENTRY_CAPTURE_RATE, SENTRY_DSN } from 'sentry.constants.mjs';

export async function register() {
  if (process.env.NEXT_RUNTIME === 'nodejs') {
    const { nodeProfilingIntegration } = await import('@sentry/profiling-node');
    init({
      dsn: SENTRY_DSN,
      debug: false,
      tracesSampleRate: SENTRY_CAPTURE_RATE,
      profilesSampleRate: SENTRY_CAPTURE_RATE,
      environment: process.env.ENV,
      integrations: [nodeProfilingIntegration()],
    });
  }
  if (process.env.NEXT_RUNTIME === 'edge') {
    await import('./instrumentation.edge');
  }
}
lforst commented 2 weeks ago

This is actually a good insight. Thanks for raising this! I see this as a documentation issue we need to resolve.