Created
January 25, 2016 16:08
-
-
Save KensoDev/e6c185dcf01492e4e144 to your computer and use it in GitHub Desktop.
statsd_instrumentation.rb
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
| 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}" | |
| # Send the status of the controller without the controller name | |
| # This is used for stats, if we have more 200, 500, 404 at any given time | |
| # | |
| ActiveSupport::Notifications.instrument :performance, | |
| :measurement => "action_controller.#{status}" | |
| 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 | |
| ActiveSupport::Notifications.subscribe(/performance/) do |name, start, finish, id, payload| | |
| StatsdIntegration.send_event_to_statsd(name, payload) | |
| end | |
| ActiveSupport::Notifications.subscribe("sql.active_record") do |name, start, finish, id, payload| | |
| if payload[:sql] =~ /^\s*(SELECT|DELETE|INSERT|UPDATE) / | |
| method = $1.downcase | |
| table = nil | |
| # Determine the table name for instrumentation. The below regexes work on | |
| # mysql but not sqlite. Probably won't work on postgresql either. | |
| case method | |
| when "select", "delete" | |
| table = $1 if payload[:sql] =~ / FROM `(\w+)`/ | |
| when "insert" | |
| table = $1 if payload[:sql] =~ /^\s*INSERT INTO `(\w+)`/ | |
| when "update" | |
| table = $1 if payload[:sql] =~ /^\s*UPDATE `(\w+)`/ | |
| end | |
| if table | |
| $statsd.increment("#{Rails.env.to_s}.active_record.#{table}.#{method}") | |
| $statsd.timing("#{Rails.env.to_s}.active_record.#{table}.#{method}.query_time", (finish - start) * 1000, 1) | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment