Class: DeadBro::SqlAllocListener

Inherits:
Object
  • Object
show all
Defined in:
lib/dead_bro/sql_subscriber.rb

Overview

Listener that records GC allocation deltas per SQL event id

Instance Method Summary collapse

Instance Method Details

#finish(name, id, payload) ⇒ Object



568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
# File 'lib/dead_bro/sql_subscriber.rb', line 568

def finish(name, id, payload)
  start_map = Thread.current[DeadBro::SqlSubscriber::THREAD_LOCAL_ALLOC_START_KEY]
  return unless start_map && start_map.key?(id)

  start_count = start_map.delete(id)
  end_count = begin
    GC.stat[:total_allocated_objects]
  rescue
    nil
  end
  return unless start_count && end_count

  delta = end_count - start_count
  results = (Thread.current[DeadBro::SqlSubscriber::THREAD_LOCAL_ALLOC_RESULTS_KEY] ||= {})
  results[id] = delta
rescue
end

#start(name, id, payload) ⇒ Object



558
559
560
561
562
563
564
565
566
# File 'lib/dead_bro/sql_subscriber.rb', line 558

def start(name, id, payload)
  map = (Thread.current[DeadBro::SqlSubscriber::THREAD_LOCAL_ALLOC_START_KEY] ||= {})
  map[id] = GC.stat[:total_allocated_objects] if defined?(GC) && GC.respond_to?(:stat)
  # Backtraces used to be captured here for every SQL event, which was
  # dominating CPU on N+1-heavy requests (100s of full Thread#backtrace
  # allocations). The main subscriber now captures a trimmed backtrace
  # lazily — and only when a query exceeds slow_query_threshold_ms.
rescue
end