在NestJS应用中在Newrelic中注释匿名中间件 [英] Annotating an annonymus middleware in Newrelic in a NestJS app

查看:55
本文介绍了在NestJS应用中在Newrelic中注释匿名中间件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将NestJS(与Express Server一起使用)用于项目,并尝试使用New Relic优化某些端点上的性能,我注意到所有端点的响应时间中有很大一部分都花在了匿名中间件上,在某些时候达到89%.

I'm using NestJS (with Express Server) for a project and trying to optimize the performance on some of the endpoints, using New Relic I noticed that a big chunk of the response time of all endpoints is spent in an anonymous middleware, reaching 89% at some points.

有没有办法找出这是哪个中间件?

Is there a way to find out which middleware is this?

推荐答案

我已经在

I've made an answer to this in another question, but I figured I should go into a bit more depth here about what's going on under the hood of Nest.

Nest中的路由处理程序从技术上说是快速表达的中间件.即

A route handler in Nest is technically a middleware in express. I.e.

@Controller('test')
export class TestController {
  @Get()
  testGet() {
    return 'do the thing';
  }
}

通过一些非常棒的代码将Get转换为

Get's transformed under the hood (through some really awesome code) to

app.get('test', (req, res, next) => {
  res.send('do the thing');
})

现在添加过滤器

@Controller('test')
export class TestController {
  @Get()
  @UseFilter(SomeExceptionFilter)
  testGet() {
    return 'do the thing';
  }
}

更多的魔术发生了,我们得到了

More magic happens and we get this

app.get('test', (req, res, next) => {
  let returnVal = '';
  try {
    returnVal = controller.testGet();
  } catch (err) {
    returnVal = someExceptionFilterInstnace.catch(err, customArgumentHostThatNestMaintains);
  }
  res.send(returnVal);
})

(免责声明:由于发生了很多事情,这并不是对实际发生情况的完美1:1表示,但这应该可以使人们最终了解所有中间件)

(Disclaimer: this isn't a perfect 1:1 representation of what actually happens, as there's a lot going on, but this should get the idea across that it's all middleware in the end)

在您添加服务时,也是如此,它们只是express/fastify中间件内部的方法调用,因此在newrelic中不会以一种很好的方式被破坏.

This follows too as you add in services, they're just method calls inside of the express/fastify middleware, and so they don't get broken out in a nice way in newrelic.

不幸的是,我不知道有什么好方法可以在newrelic或如何添加更好的注释中对此提供帮助.查看中间件的时间,您有218 ms,这还不错.最有可能是在某个地方进行数据库操作,因此我将检查您的管道,防护和拦截器以及您的服务,并查看是否有任何可能的大查询.如果没什么,您可以在本地进行一些分析,以获取有关所花费时间的更多原始信息

Unfortunately, I don't know of a good way to help with this in newrelic or how to add better annotations. Looking at the timing of the middleware, you've got 218 ms, which isn't too bad. Most likely a database operation somewhere, so I'd check your pipes, guards, and interceptors, along with your service, and see if you have any possibly large queries. If nothing else, you can do some profiling locally and get a bit more raw information about the time being taken

这篇关于在NestJS应用中在Newrelic中注释匿名中间件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆