Class: RuntimeProfiler::SqlEvent
- Inherits:
-
Object
- Object
- RuntimeProfiler::SqlEvent
- Defined in:
- lib/runtime_profiler/events/sql_event.rb
Instance Attribute Summary collapse
-
#finished_at ⇒ Object
readonly
Returns the value of attribute finished_at.
-
#payload ⇒ Object
readonly
Returns the value of attribute payload.
-
#started_at ⇒ Object
readonly
Returns the value of attribute started_at.
-
#trace ⇒ Object
readonly
Returns the value of attribute trace.
Instance Method Summary collapse
-
#initialize(args:, trace:) ⇒ SqlEvent
constructor
A new instance of SqlEvent.
- #key ⇒ Object
- #recordable? ⇒ Boolean
- #sanitized_sql ⇒ Object
- #total_runtime ⇒ Object
Constructor Details
#initialize(args:, trace:) ⇒ SqlEvent
Returns a new instance of SqlEvent.
5 6 7 8 |
# File 'lib/runtime_profiler/events/sql_event.rb', line 5 def initialize(args: , trace: ) _name, @started_at, @finished_at, _unique_id, @payload = args @trace = sanitize_trace!(trace) end |
Instance Attribute Details
#finished_at ⇒ Object (readonly)
Returns the value of attribute finished_at.
3 4 5 |
# File 'lib/runtime_profiler/events/sql_event.rb', line 3 def finished_at @finished_at end |
#payload ⇒ Object (readonly)
Returns the value of attribute payload.
3 4 5 |
# File 'lib/runtime_profiler/events/sql_event.rb', line 3 def payload @payload end |
#started_at ⇒ Object (readonly)
Returns the value of attribute started_at.
3 4 5 |
# File 'lib/runtime_profiler/events/sql_event.rb', line 3 def started_at @started_at end |
#trace ⇒ Object (readonly)
Returns the value of attribute trace.
3 4 5 |
# File 'lib/runtime_profiler/events/sql_event.rb', line 3 def trace @trace end |
Instance Method Details
#key ⇒ Object
32 33 34 |
# File 'lib/runtime_profiler/events/sql_event.rb', line 32 def key @key ||= Digest::MD5.hexdigest(sql.downcase) end |
#recordable? ⇒ Boolean
10 11 12 13 |
# File 'lib/runtime_profiler/events/sql_event.rb', line 10 def recordable? return true unless RuntimeProfiler.instrumented_sql_commands.respond_to?(:join) instrumented_sql_matcher =~ sql end |
#sanitized_sql ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/runtime_profiler/events/sql_event.rb', line 19 def sanitized_sql sql.squish! sql.gsub!(/(\s(=|>|<|>=|<=|<>|!=)\s)('[^']+'|[\$\+\-\w\.]+)/, '\1xxx') sql.gsub!(/(\sIN\s)\([^\(\)]+\)/i, '\1(xxx)') sql.gsub!(/(\sBETWEEN\s)('[^']+'|[\+\-\w\.]+)(\sAND\s)('[^']+'|[\+\-\w\.]+)/i, '\1xxx\3xxx') sql.gsub!(/(\sVALUES\s)\(.+\)/i, '\1(xxx)') sql.gsub!(/(\s(LIKE|ILIKE|SIMILAR TO|NOT SIMILAR TO)\s)('[^']+')/i, '\1xxx') sql.gsub!(/(\s(LIMIT|OFFSET)\s)(\d+)/i, '\1xxx') sql end |
#total_runtime ⇒ Object
15 16 17 |
# File 'lib/runtime_profiler/events/sql_event.rb', line 15 def total_runtime 1000.0 * (@finished_at - @started_at) end |