Class: RorVsWild::Plugin::ActiveRecord
- Inherits:
-
Object
- Object
- RorVsWild::Plugin::ActiveRecord
- Defined in:
- lib/rorvswild/plugin/active_record.rb
Constant Summary collapse
- IGNORED_QUERIES =
%w[EXPLAIN SCHEMA].freeze
- SQL_STRING_REGEX =
/'((?:''|\\'|[^'])*)'/- SQL_NUMERIC_REGEX =
/(?<!\w)\d+(\.\d+)?(?!\w)/- SQL_PARAMETER_REGEX =
/\$\d+/- SQL_IN_REGEX =
/(\bIN\s*\()([^)]+)(\))/i- SQL_ONE_LINE_COMMENT_REGEX =
/--.*$/- SQL_MULTI_LINE_COMMENT_REGEX =
/\/\*.*?\*\//m
Class Method Summary collapse
Instance Method Summary collapse
- #finish(name, id, payload) ⇒ Object
- #normalize_sql_query(sql) ⇒ Object
-
#publish_event(event) ⇒ Object
Async queries.
- #start(name, id, payload) ⇒ Object
Class Method Details
.setup(agent) ⇒ Object
8 9 10 11 12 |
# File 'lib/rorvswild/plugin/active_record.rb', line 8 def self.setup(agent) return if @installed setup_callback @installed = true end |
.setup_callback ⇒ Object
14 15 16 17 |
# File 'lib/rorvswild/plugin/active_record.rb', line 14 def self.setup_callback return unless defined?(ActiveSupport::Notifications.subscribe) ActiveSupport::Notifications.subscribe("sql.active_record", new) end |
Instance Method Details
#finish(name, id, payload) ⇒ Object
29 30 31 32 |
# File 'lib/rorvswild/plugin/active_record.rb', line 29 def finish(name, id, payload) return if IGNORED_QUERIES.include?(payload[:name]) RorVsWild::Section.stop end |
#normalize_sql_query(sql) ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/rorvswild/plugin/active_record.rb', line 53 def normalize_sql_query(sql) sql = sql.to_s.gsub(SQL_STRING_REGEX, "?") sql.gsub!(SQL_PARAMETER_REGEX, "?") sql.gsub!(SQL_NUMERIC_REGEX, "?") sql.gsub!(SQL_IN_REGEX, '\1?\3') sql.gsub!(SQL_ONE_LINE_COMMENT_REGEX, "") sql.gsub!(SQL_MULTI_LINE_COMMENT_REGEX, "") sql.strip! sql end |
#publish_event(event) ⇒ Object
Async queries
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/rorvswild/plugin/active_record.rb', line 35 def publish_event(event) section = Section.new section.total_ms = event.payload[:lock_wait] section.async_ms = event.duration - event.payload[:lock_wait] section.gc_time_ms = event.respond_to?(:gc_time) ? event.gc_time : 0 # gc_time since Rails 7.2.0 section.commands << normalize_sql_query(event.payload[:sql]) section.kind = "sql" (parent = Section.current) && parent.children_ms += section.total_ms execution = RorVsWild.agent.current_execution and execution.add_section(section) end |
#start(name, id, payload) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/rorvswild/plugin/active_record.rb', line 21 def start(name, id, payload) return if IGNORED_QUERIES.include?(payload[:name]) RorVsWild::Section.start do |section| section.commands << normalize_sql_query(payload[:sql]) section.kind = "sql" end end |