Class: DeadBro::SqlTrackingMiddleware

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

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ SqlTrackingMiddleware

Returns a new instance of SqlTrackingMiddleware.



5
6
7
# File 'lib/dead_bro/sql_tracking_middleware.rb', line 5

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/dead_bro/sql_tracking_middleware.rb', line 9

def call(env)
  # Clear logs for this request
  DeadBro.logger.clear

  # Start SQL tracking for this request
  if defined?(DeadBro::SqlSubscriber)
    DeadBro::SqlSubscriber.start_request_tracking
  end

  # Start cache tracking for this request
  if defined?(DeadBro::CacheSubscriber)
    DeadBro::CacheSubscriber.start_request_tracking
  end

  # Start Redis tracking for this request
  if defined?(DeadBro::RedisSubscriber)
    DeadBro::RedisSubscriber.start_request_tracking
  end

  # Start view rendering tracking for this request
  if defined?(DeadBro::ViewRenderingSubscriber)
    DeadBro::ViewRenderingSubscriber.start_request_tracking
  end

  # Start lightweight memory tracking for this request
  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

  # Start outgoing HTTP accumulation for this request
  Thread.current[:dead_bro_http_events] = []

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

  @app.call(env)
ensure
  # Clean up thread-local storage
  if defined?(DeadBro::SqlSubscriber)
    Thread.current[:dead_bro_sql_queries]
    Thread.current[:dead_bro_sql_queries] = nil
  end

  if defined?(DeadBro::CacheSubscriber)
    Thread.current[:dead_bro_cache_events]
    Thread.current[:dead_bro_cache_events] = nil
  end

  if defined?(DeadBro::RedisSubscriber)
    Thread.current[:dead_bro_redis_events]
    Thread.current[:dead_bro_redis_events] = nil
  end

  if defined?(DeadBro::ViewRenderingSubscriber)
    Thread.current[:dead_bro_view_events]
    Thread.current[:dead_bro_view_events] = nil
  end

  if defined?(DeadBro::LightweightMemoryTracker)
    Thread.current[:dead_bro_lightweight_memory]
    Thread.current[:dead_bro_lightweight_memory] = nil
  end

  # Clean up HTTP events and tracking start time
  Thread.current[:dead_bro_http_events] = nil
  Thread.current[DeadBro::TRACKING_START_TIME_KEY] = nil
end