Sentry 与 NestJS 集成时无法获取 TypeScript 源映射

Con*_*son 7 sentry source-maps typescript nestjs

我最近创建了一个小型 NestJS 项目,试图将 Sentry 集成到其中。我已按照Nest-Raven包自述文件中的说明以及 Sentry 提供的TypeScript 集成说明进行操作。

不幸的是,我似乎无法让 Sentry 显示 TypeScript 源映射,只能显示常规 JS 源映射,如下所示:

哨兵源图

main.ts我已按照说明初始化了 Sentry

import { NestFactory } from '@nestjs/core';
import { RewriteFrames } from '@sentry/integrations';
import * as Sentry from '@sentry/node';
import { AppModule } from './app.module';

// This allows TypeScript to detect our global value
declare global {
  // eslint-disable-next-line @typescript-eslint/no-namespace
  namespace NodeJS {
    interface Global {
      __rootdir__: string;
    }
  }
}

global.__rootdir__ = __dirname || process.cwd();

async function bootstrap() {
  Sentry.init({
    dsn: 'https://mySentryDSN.ingest.sentry.io/0',
    integrations: [
      new RewriteFrames({
        root: global.__rootdir__,
      }),
    ],
  });
  const app = await NestFactory.create(AppModule);
  await app.listen(3000);
}
bootstrap();
Run Code Online (Sandbox Code Playgroud)

我还设置了Nest-Raven使用全局拦截器

import { APP_INTERCEPTOR } from '@nestjs/core';
import { RavenInterceptor, RavenModule } from 'nest-raven';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  imports: [
    RavenModule,
    ...
  ],
  controllers: [AppController],
  providers: [
    AppService,
    {
      provide: APP_INTERCEPTOR,
      useValue: new RavenInterceptor(),
    },
  ],
})
export class AppModule {}
Run Code Online (Sandbox Code Playgroud)

还有其他人遇到过这个问题吗?我想也许我需要按照这些说明将源映射直接上传到 Sentry ,但是据我所知 NestJS 不使用 Webpack,所以我不确定如何继续。

Con*_*son 5

所以事实证明问题是我提供给RewriteFrames构造函数的目录。global.__rootdir__我最初从Sentry Typescript 文档复制了实现,但在调试过程中我发现__dirnameprocess.cwd()返回了不同的路径。

  dirname: '/Users/<name>/Documents/Projects/nest-test-project/dist',
  cwd: '/Users/<name>/Documents/Projects/nest-test-project'
Run Code Online (Sandbox Code Playgroud)

由于__dirname返回的是真实值,因此提供给 Sentry 的路径是包含该文件夹的路径dist。当我更改代码以向 Sentry 提供项目的实际根路径后,所有 Typescript 源映射都被上传到 Sentry。

编辑:有些人要求提供我的代码tsconfig和上面的代码的示例。此后,我自己编写了SentryModule用于引导 Sentry 的代码,但更改的要点是找到您调用的位置new RewriteFrames({...})并传递正确的root,在我的例子中是process.cwd()

tsconfig.json

  dirname: '/Users/<name>/Documents/Projects/nest-test-project/dist',
  cwd: '/Users/<name>/Documents/Projects/nest-test-project'
Run Code Online (Sandbox Code Playgroud)

main.ts或 Sentry 初始化的任何地方

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "ES2017",
    "lib": [ "ES2020.Promise" ],
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "sourceMap": true,
    "inlineSources": true,
    "sourceRoot": "/"
  }
}

Run Code Online (Sandbox Code Playgroud)

  • 嘿,你可以有你使用过的完整设置吗?我还坚持使用 Nestjs 和 Sentry,但它也不适用于正确的路径。也许您可以发布对您有用的配置?提前致谢!:) (2认同)