我想将 sentry 与 nest.js + express 集成,但我刚刚找到了 raven 版本,但已弃用。我按照哨兵文档与 express 集成,但不知道如何处理“所有控制器都应该住在这里”部分。
const express = require('express');
const app = express();
const Sentry = require('@sentry/node');
Sentry.init({ dsn: 'https://5265e36cb9104baf9b3109bb5da9423e@sentry.io/1768434' });
// The request handler must be the first middleware on the app
app.use(Sentry.Handlers.requestHandler());
**// All controllers should live here
app.get('/', function rootHandler(req, res) {
res.end('Hello world!');
});**
// The error handler must be before any other error middleware and after all controllers
app.use(Sentry.Handlers.errorHandler());
// Optional fallthrough error handler
app.use(function onError(err, req, res, next) {
// The error id is attached to `res.sentry` to be returned
// and optionally displayed to the user for support.
res.statusCode = 500;
res.end(res.sentry + "\n");
});
app.listen(3000);
Run Code Online (Sandbox Code Playgroud)
Eri*_*ker 29
我刚刚在 Github 上创建了一个示例项目来回答这个问题:
https://github.com/ericjeker/nestjs-sentry-example
以下是自述文件的部分副本。如果您有任何疑问,请告诉我。
创建Sentry模块、服务和拦截器
$ nest g module sentry
$ nest g service sentry
$ nest g interceptor sentry/sentry
Run Code Online (Sandbox Code Playgroud)
创建该方法并在其中SentryModule.forRoot()添加。Sentry.init(options)
调用SentryModule.forRoot({...})并AppModule与您首选的配置集成(我使用ConfigModule和一个.env文件)。
添加对 Express requestHandler 中间件的调用AppModule.configure()。
configure(consumer: MiddlewareConsumer): void {
consumer.apply(Sentry.Handlers.requestHandler()).forRoutes({
path: '*',
method: RequestMethod.ALL,
});
}
Run Code Online (Sandbox Code Playgroud)
使用该中间件非常重要,否则当前的 Hub 将是全局的,并且您将遇到冲突,因为 Sentry 通过线程创建 Hub,而 Node.js 不是多线程的。
我们希望在服务的构造函数中初始化事务。您可以在那里自定义您的主要交易。
请注意,因为我注入了 Express 请求,所以服务必须处于请求范围内。您可以在这里阅读更多相关内容。
@Injectable({ scope: Scope.REQUEST })
export class SentryService {
constructor(@Inject(REQUEST) private request: Request) {
// ... etc ...
}
}
Run Code Online (Sandbox Code Playgroud)
将SentryInterceptor捕获异常并完成事务。另请注意,当我们注入时,它必须处于请求范围内SentryService:
@Injectable({ scope: Scope.REQUEST })
export class SentryInterceptor implements NestInterceptor {
constructor(private sentryService: SentryService) {}
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
// ... etc ...
}
}
Run Code Online (Sandbox Code Playgroud)
作为示例,我添加了一个跨度。这不是必需的,但它只会使 Sentry 性能查看器中的跟踪更好。
SentryService您可以通过注入并调用startChild或简单地调用startChild当前跨度的方法来在应用程序中的任何位置添加更多跨度。
为了将哨兵与 nestjs 集成,我们按照以下步骤操作:
async function bootstrap() {
Sentry.init({ dsn: 'https://5265e36cb9104baf9b3109bb5da9423e@sentry.io/1768434' });
const app = await NestFactory.create(AppModule);
// middlewares
await app.listen(3000);
}
Run Code Online (Sandbox Code Playgroud)
@Module({
imports: [
RavenModule,...
],
controllers: [],
providers: [{
provide: APP_INTERCEPTOR,
useValue: new RavenInterceptor({
filters: [
// Filter exceptions of type HttpException. Ignore those that
// have status code of less than 500
{ type: HttpException, filter: (exception: HttpException) => 500 > exception.getStatus() },
],
}),
}],
})
Run Code Online (Sandbox Code Playgroud)
问题在这里跟踪https://github.com/getsentry/sentry-javascript/issues/2269,您可以在那里查看示例。
| 归档时间: |
|
| 查看次数: |
3587 次 |
| 最近记录: |