跳至内容 跳至搜索

Action Controller Instrumentation

ActionController::Base 的多个方面添加了检测功能。 它还提供了一些与 process_action 相关的钩子。 这允许像 Active Record 和/或 DataMapper 这样的 ORM 插件到 ActionController 并显示相关信息。

查看 ActiveRecord::Railties::ControllerRuntime 获取示例。

命名空间
方法
A
C
R
S

实例公共方法

redirect_to(*)

# File actionpack/lib/action_controller/metal/instrumentation.rb, line 49
def redirect_to(*)
  ActiveSupport::Notifications.instrument("redirect_to.action_controller", request: request) do |payload|
    result = super
    payload[:status]   = response.status
    payload[:location] = response.filtered_location
    result
  end
end

render(*)

# File actionpack/lib/action_controller/metal/instrumentation.rb, line 28
def render(*)
  render_output = nil
  self.view_runtime = cleanup_view_runtime do
    ActiveSupport::Benchmark.realtime(:float_millisecond) { render_output = super }
  end
  render_output
end

send_data(data, options = {})

# File actionpack/lib/action_controller/metal/instrumentation.rb, line 43
def send_data(data, options = {})
  ActiveSupport::Notifications.instrument("send_data.action_controller", options) do
    super
  end
end

send_file(path, options = {})

# File actionpack/lib/action_controller/metal/instrumentation.rb, line 36
def send_file(path, options = {})
  ActiveSupport::Notifications.instrument("send_file.action_controller",
    options.merge(path: path)) do
    super
  end
end

实例私有方法

append_info_to_payload(payload)

每次处理完操作后,都会使用有效负载调用此方法,因此您可以添加更多信息。

# File actionpack/lib/action_controller/metal/instrumentation.rb, line 105
def append_info_to_payload(payload) # :doc:
  payload[:view_runtime] = view_runtime
end

cleanup_view_runtime()

一个钩子,允许您清理任何在视图中错误地考虑的时间,例如数据库查询时间。

def cleanup_view_runtime
  super - time_taken_in_something_expensive
end
# File actionpack/lib/action_controller/metal/instrumentation.rb, line 99
def cleanup_view_runtime # :doc:
  yield
end