跳到內文 跳到搜尋

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)

每次處理動作之後,都會使用 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