New Relic 打破 continuation-local-storage context

New Relic breaking continuation-local-storage context

Node.js 中的 New Relic 有问题。 它似乎干扰了我的 continuation-local-storage 实现,我的意思是它似乎正在杀死我的 cls 上下文。

所以现在,我当前的设置一切正常,没有问题。只有当我添加行 require("newrelic");到 app.js 的顶部,正如他们所建议的那样,问题发生了。

以下是出现问题的特定函数。

function getProfile( req, res, next ) {
  var logger = new log_handler( { "script": __filename, "function": getProfile.name } );
  var session = cls.getNamespace( 'session' );
  var auth = req.headers.authorization.split( " " );

  var existingProfile = session.get( "profile" );
  if ( typeof existingProfile === "object" && existingProfile.hasOwnProperty( "federatedId" ) ) {
    return next( );
  }

  query_auth0.postTokenInfo( auth[1], function ( err, profile ) {
    if ( err ) {
      logger.warn( "query_auth0 encountered an error", { error: err.message } );
      return next( err );
    }

    session.set( "profile", profile );
    next( err );
  } );
}

我调用 query_auth0 之前的 session.get("profile") 语句有效并且确实返回会话配置文件(如果堆栈中有一组)。但是, session.set( "profile", profile);行给我错误。

query_auth0.postTokenInfo() 函数调用似乎中断了。那里有几层,给你一个关于它做什么的总结可能会更简单:

该函数只是构建对 auth0 的请求,然后通过我们自己的 request_handler 模块发送。这只是建立在第三方 "request" npm 模块上。 request_handler 与 cls 交互以拉出 request_ids,但不设置任何内容。

问题似乎是在发出请求后出现的(并且毫无问题地成功了),但是,当我尝试添加返回的配置文件时,我抛出了以下异常:

An Uncaught Exception Occurred!
Error: No context available. ns.run() or ns.bind() must be called first.
    at Namespace.set (E:\sitesroot[=11=]\node_modules\continuation-local-storage\context.js:27:11)
    at E:\sitesroot[=11=]\shared\middleware\get_profile.js:45:35
    at E:\sitesroot[=11=]\shared\query_auth0.js:51:12
    at E:\sitesroot[=11=]\shared\request_handler.js:51:12
    at E:\sitesroot[=11=]\node_modules\async\lib\async.js:52:16
    at Immediate.<anonymous> (E:\sitesroot[=11=]\node_modules\async\lib\async.js:1206:34)
    at Immediate.wrapped [as _onImmediate] (E:\sitesroot[=11=]\node_modules\newrelic\lib\transaction\tracer\index.js:155:28)
    at processImmediate [as _immediateCallback] (timers.js:383:17)

同样,此错误仅在我使用 newrelic 模块时发生。否则代码工作正常。 此外,问题似乎发生在请求完成并回调时。

有没有人在 node.js 中使用 New Relic 时遇到过类似的问题,至少可以为我指明正确的方向?

对于遇到此问题并感兴趣的任何人。

似乎在需要 newrelic 之后,它会以某种方式影响异步和连续本地存储因某种原因交互的方式。在需要 newrelic 之前没有问题。

然而,在网上进一步搜索后,我发现了一些与 async 和 cls 问题相关的主题,但是这些问题只在我需要 newrelic 时出现。

简而言之,我找到的解决方案是在 app.js 中要求 newrelic 之前要求 cls。我不确定为什么这解决了问题,但确实如此。 如果有人对原因有任何其他反馈,我们将不胜感激。