Module: NSA::Collectors::ActiveRecord
- Extended by:
- Statsd::Publisher
- Defined in:
- lib/nsa/collectors/active_record.rb
Constant Summary collapse
- MATCHERS =
Ordered by most common query type
[ [ :select, /^\s*SELECT.+?FROM\s+"?([^".\s),]+)"?/im ], [ :insert, /^\s*INSERT INTO\s+"?([^".\s]+)"?/im ], [ :update, /^\s*UPDATE\s+"?([^".\s]+)"?/im ], [ :delete, /^\s*DELETE.+FROM\s+"?([^".\s]+)"?/im ] ].freeze
- EMPTY_MATCH_RESULT =
[]
Class Method Summary collapse
Methods included from Statsd::Publisher
__statsd_publish, statsd_count, statsd_decrement, statsd_gauge, statsd_increment, statsd_set, statsd_time, statsd_timing
Class Method Details
.collect(key_prefix) ⇒ Object
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/nsa/collectors/active_record.rb', line 18 def self.collect(key_prefix) ::ActiveSupport::Notifications.subscribe("sql.active_record") do |_, start, finish, _id, payload| query_type, table_name = match_query(payload[:sql]) unless query_type.nil? stat_name = "#{key_prefix}.tables.#{table_name}.queries.#{query_type}.duration" duration_ms = (finish - start) * 1000 statsd_timing(stat_name, duration_ms) end end end |
.match_query(sql) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/nsa/collectors/active_record.rb', line 29 def self.match_query(sql) MATCHERS .lazy .map { |(type, regex)| match = (sql.match(regex) || EMPTY_MATCH_RESULT) [ type, match[1] ] } .detect { |(_, table_name)| ! table_name.nil? } end |