CommonJS (CJS)

了解如何在 CJS 应用程序中运行 Sentry。

大多数现代 Node 应用程序要么使用 CommonJS (CJS),要么在运行前将其编译为 CJS。 CommonJS 使用 require() 来加载模块。在使用 CommonJS 时,我们推荐的方法是在应用程序的顶部引入 instrument.js 文件。

你需要创建一个名为 instrument.js 的文件,该文件导入并初始化 Sentry:

instrument.js
Copied
const Sentry = require("@sentry/node");
const { nodeProfilingIntegration } = require("@sentry/profiling-node");

// Ensure to call this before requiring any other modules!
Sentry.init({
  dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
  integrations: [
    // Add our Profiling integration
    nodeProfilingIntegration(),
  ],

  // Add Tracing by setting tracesSampleRate
  // We recommend adjusting this value in production
  tracesSampleRate: 1.0,

  // Set sampling rate for profiling
  // This is relative to tracesSampleRate
  profilesSampleRate: 1.0,
});

你需要在引入其他任何模块之前引入或导入 instrument.js 文件。这是确保 Sentry 可以自动仪器化应用程序中所有模块的必要步骤:

app.js
Copied
// Require this first!
require("./instrument");

// Now require other modules
const http = require("http");

// Your application code goes here