Skip to content

Instantly share code, notes, and snippets.

@danielberlinger
Created March 27, 2012 20:10
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielberlinger/2219845 to your computer and use it in GitHub Desktop.
Save danielberlinger/2219845 to your computer and use it in GitHub Desktop.
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
Copy link

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