Class: DeadBro::JobSqlTrackingMiddleware

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

Class Method Summary collapse

Class Method Details

.subscribe!Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/dead_bro/job_sql_tracking_middleware.rb', line 5

def self.subscribe!
  # Start SQL tracking when a job begins - use the start event, not the complete event
  ActiveSupport::Notifications.subscribe("perform_start.active_job") do |name, started, finished, _unique_id, data|
    # Clear logs for this job
    DeadBro.logger.clear

    # Set tracking start time once for all subscribers (before starting any tracking)
    Thread.current[DeadBro::TRACKING_START_TIME_KEY] = Time.now

    DeadBro::SqlSubscriber.start_request_tracking

    # Start lightweight memory tracking for this job
    if defined?(DeadBro::LightweightMemoryTracker)
      DeadBro::LightweightMemoryTracker.start_request_tracking
    end

    # Start detailed memory tracking when allocation tracking is enabled
    if DeadBro.configuration.allocation_tracking_enabled && defined?(DeadBro::MemoryTrackingSubscriber)
      DeadBro::MemoryTrackingSubscriber.start_request_tracking
    end
  end
rescue
  # Never raise from instrumentation install
end