机架中间件中的跟踪错误

agb*_*ike 2 ruby rack ruby-on-rails

我现在不是 100% 确定,但我相信我收到了一个异常,导致请求的状态以 401 响应。

我已经消除了控制器中的所有代码,但问题仍然存在,并且似乎有些东西正在捕获异常以返回 401 状态,因此没有堆栈跟踪。

我已经创建了一个自定义中间件来尝试和调试,但我仍然无法找到问题的根源。

如何隔离问题发生的位置?

我们正在使用 heroku,所以 pry 似乎不起作用,我也没有对正在运行的服务器的命令行访问权限。我无法在本地开发机器上复制该问题。

小智 8

替代方法是以下模块,它在每个中间件入口/返回处插入调试消息(在每次返回后打印状态代码)(改编自https://github.com/DataDog/dd-trace-rb/issues/368

# Trace each middleware module entry/exit. Freely adapted from
# https://github.com/DataDog/dd-trace-rb/issues/368 "Trace each middleware in the Rails stack"
module MiddlewareTracer
  def call(env)
    Rails.logger.debug { "MiddleWare: entry #{self.class.name}" }
    status, headers, response = super(env)
    Rails.logger.debug { "MiddleWare: #{self.class.name}, returning with status #{status}" }
    [status, headers, response]
  end
end

# Instrument the middleware stack after initialization so that we
# know the stack won't be changed afterwards.
Rails.configuration.after_initialize do
  Rails.application.middleware.each do |middleware|
    klass = middleware.klass

    # There are a few odd middlewares that aren't classes.
    klass.prepend(MiddlewareTracer) if klass.respond_to?(:prepend)
  end
end
Run Code Online (Sandbox Code Playgroud)