Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A Rails initializer for using Rails 3 notifications and statsd
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
@carlwolff

This comment has been minimized.

Copy link

@carlwolff carlwolff commented Nov 7, 2012

I dunno what's up with the gists, seen a lot of replacements like these.
=&gt should probably be =>
Thanks for a helpful gist. Will try it out for a big rails project I'm currently working on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment