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

问题描述

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

不幸的是,我似乎无法让Sentry显示TypeScript源映射,仅显示常规的JS源映射,如您在此处看到的那样:

Sentry Sourcemaps

我已按照说明在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();

我还设置了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 {}

还有其他人遇到此问题吗?我在想,也许我需要按照these intstructions直接将源地图上传到Sentry,但是据我所知,nestJS没有使用Webpack,所以我不确定如何继续。

解决方法

您可以使Nest与webpack as described here一起使用nest build --webpack编译器。 It looks like Sentry also has documentation on how to get source map support using typescript without webpack,因此也值得一试。

,

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

  dirname: '/Users/<name>/Documents/Projects/nest-test-project/dist',cwd: '/Users/<name>/Documents/Projects/nest-test-project'

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