我有一个 RoR Rest API,我想发出一个指标,其中包含我的 API 的每个响应的状态。除了 Controller 崩溃的情况外,我设法对所有情况都做到了这一点。
例如在 ApplicationController 中使用以下代码:
require 'statsd-ruby'
class ApplicationController < ActionController::API
after_action :push_status_metric
def push_status_metric
statsd = Statsd.new ENV['STATSD_LOCATION'], ENV['STATSD_PORT']
puts normalize_status_metric(response.status)
statsd.increment('ds.status.' + normalize_status_metric(response.status).to_s + '.int') unless request.fullpath == '/health'
end
private
def normalize_status_metric(status)
return 100 if status >= 100 && status < 200
return 200 if status >= 200 && status < 300
return 300 if status >= 300 && status < 400
return 400 if status >= 400 && status < 500
return 500 if status >= 500 && status < 600
0
end
end
但是这个解决方案不会捕获诸如 ActionController::RoutingError 和 ActiveRecord::RecordNotFound 之类的错误。
我尝试了以下代码:
rescue_from StandardError do |exception|
statsd = Statsd.new ENV['STATSD_LOCATION'], ENV['STATSD_PORT']
statsd.increment('ds.status.' + normalize_status_metric(response.status).to_s + '.int') unless request.fullpath == '/health'
raise exception
end
但是当执行此回调时,response.status 值始终为 200(似乎到目前为止框架尚未设置)。
最佳答案
知道 rails 记录器设法做到这一点,我们可以看看它的类,ActionController::LogSubscriber
和 process_action
方法。因此,该事件中的状态可能是 nil
,如果存在异常,我们可以看到他们如何将异常转换为状态:
status = payload[:status]
if status.nil? && payload[:exception].present?
exception_class_name = payload[:exception].first
status = ActionDispatch::ExceptionWrapper.status_code_for_exception(exception_class_name)
end
所以现在,我们可以通过自己订阅这个事件来做类似的事情,用 Active Support Instrumentation ,通过创建一个初始值设定项:
ActiveSupport::Notifications.subscribe 'process_action.action_controller' do |*args|
event = ActiveSupport::Notifications::Event.new(*args)
# opening a file here is probably a performance nightmare, but you'd be doing something with statsd, not a file, anyway
open('metrics.txt', 'a') do |f|
# get the action status, this code is from the ActionController::LogSubscriber
status = event.payload[:status]
if status.nil? && event.payload[:exception].present?
exception_class_name = event.payload[:exception].first
status = ActionDispatch::ExceptionWrapper.status_code_for_exception(exception_class_name)
end
f.puts "process_action.action_controller controller: #{event.payload[:controller]} - action: #{event.payload[:action]} - path: #{event.payload[:path]} - status: #{status}"
end
end
然后用一个简单的 Controller 点击几次:
class HomeController < ApplicationController
def non_standard_status
render html: 'This is not fine', status: :forbidden
end
def error
raise ActiveRecord::RecordNotFound, 'No Records Found'
end
def another_error
raise ArgumentError, 'Some Argument is wrong'
end
def this_is_fine
render html: 'This is fine'
end
end
产生一个文件:
process_action.action_controller controller: HomeController - action: non_standard_status - path: /forbidden - status: 403
process_action.action_controller controller: HomeController - action: error - path: /error - status: 404
process_action.action_controller controller: HomeController - action: another_error - path: /error2 - status: 500
process_action.action_controller controller: HomeController - action: this_is_fine - path: /fine - status: 200
关于ruby-on-rails - 在 Ruby on Rails 中执行每个 Controller 后获取响应状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49315748/