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



667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
# File 'lib/dead_bro/sql_subscriber.rb', line 667

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



657
658
659
660
661
662
663
664
665
# File 'lib/dead_bro/sql_subscriber.rb', line 657

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