Created
March 27, 2012 20:10
-
-
Save danielberlinger/2219845 to your computer and use it in GitHub Desktop.
A Rails initializer for using Rails 3 notifications and statsd
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'statsd' | |
$statsd = Statsd.new('your_host_here') | |
ActiveSupport::Notifications.subscribe /process_action.action_controller/ do |*args| | |
event = ActiveSupport::Notifications::Event.new(*args) | |
controller = event.payload[:controller] | |
action = event.payload[:action] | |
format = event.payload[:format] || "all" | |
format = "all" if format == "*/*" | |
status = event.payload[:status] | |
key = "#{controller}.#{action}.#{format}.#{ENV["INSTRUMENTATION_HOSTNAME"]}" | |
ActiveSupport::Notifications.instrument :performance, :action => :timing, :measurement => "#{key}.total_duration", :value => event.duration | |
ActiveSupport::Notifications.instrument :performance, :action => :timing, :measurement => "#{key}.db_time", :value => event.payload[:db_runtime] | |
ActiveSupport::Notifications.instrument :performance, :action => :timing, :measurement => "#{key}.view_time", :value => event.payload[:view_runtime] | |
ActiveSupport::Notifications.instrument :performance, :measurement => "#{key}.status.#{status}" | |
end | |
def send_event_to_statsd(name, payload) | |
action = payload[:action] || :increment | |
measurement = payload[:measurement] | |
value = payload[:value] | |
key_name = "#{name.to_s.capitalize}.#{measurement}" | |
$statsd.__send__ action.to_s, key_name, (value || 1) | |
end | |
ActiveSupport::Notifications.subscribe /performance/ do |name, start, finish, id, payload| | |
send_event_to_statsd(name, payload) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I dunno what's up with the gists, seen a lot of replacements like these.
=> should probably be =>
Thanks for a helpful gist. Will try it out for a big rails project I'm currently working on.