Class: DeadBro::ViewRenderingSubscriber

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

Constant Summary collapse

RENDER_TEMPLATE_EVENT =

Rails view rendering events

"render_template.action_view"
RENDER_PARTIAL_EVENT =
"render_partial.action_view"
RENDER_COLLECTION_EVENT =
"render_collection.action_view"
THREAD_LOCAL_KEY =
:dead_bro_view_events
MAX_TRACKED_EVENTS =
1000

Class Method Summary collapse

Class Method Details

.add_view_event(view_info) ⇒ Object



81
82
83
84
85
# File 'lib/dead_bro/view_rendering_subscriber.rb', line 81

def self.add_view_event(view_info)
  if Thread.current[THREAD_LOCAL_KEY] && should_continue_tracking?
    Thread.current[THREAD_LOCAL_KEY] << view_info
  end
end

.analyze_view_performance(view_events) ⇒ Object

Analyze view rendering performance



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/dead_bro/view_rendering_subscriber.rb', line 116

def self.analyze_view_performance(view_events)
  return {} if view_events.empty?

  total_duration = view_events.sum { |event| event[:duration_ms] }

  # Group by view type
  by_type = view_events.group_by { |event| event[:type] }

  # Find slowest views
  slowest_views = view_events.sort_by { |event| -event[:duration_ms] }.first(5)

  # Find most frequently rendered views
  view_frequency = view_events.group_by { |event| event[:identifier] }
    .transform_values(&:count)
    .sort_by { |_, count| -count }
    .first(5)

  # Calculate cache hit rates for partials
  partials = view_events.select { |event| event[:type] == "partial" }
  cache_hits = partials.count { |event| event[:cache_key] }
  cache_hit_rate = partials.any? ? (cache_hits.to_f / partials.count * 100).round(2) : 0

  # Collection rendering analysis
  collections = view_events.select { |event| event[:type] == "collection" }
  total_collection_items = collections.sum { |event| event[:count] || 0 }
  total_cached_items = collections.sum { |event| event[:cached_count] || 0 }
  collection_cache_hit_rate = (total_collection_items > 0) ?
    (total_cached_items.to_f / total_collection_items * 100).round(2) : 0

  {
    total_views_rendered: view_events.count,
    total_view_duration_ms: total_duration.round(2),
    average_view_duration_ms: (total_duration / view_events.count).round(2),
    by_type: by_type.transform_values(&:count),
    slowest_views: slowest_views.map { |view|
      {
        identifier: view[:identifier],
        duration_ms: view[:duration_ms],
        type: view[:type]
      }
    },
    most_frequent_views: view_frequency.map { |identifier, count|
      {
        identifier: identifier,
        count: count
      }
    },
    partial_cache_hit_rate: cache_hit_rate,
    collection_cache_hit_rate: collection_cache_hit_rate,
    total_collection_items: total_collection_items,
    total_cached_collection_items: total_cached_items
  }
end

.safe_identifier(identifier) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/dead_bro/view_rendering_subscriber.rb', line 105

def self.safe_identifier(identifier)
  return "" unless identifier.is_a?(String)

  # Extract meaningful parts of the file path
  # e.g., "/app/views/users/show.html.erb" -> "users/show.html.erb"
  identifier.split("/").last(3).join("/")
rescue
  identifier.to_s
end

.should_continue_tracking?Boolean

Check if we should continue tracking based on count and time limits

Returns:

  • (Boolean)


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/dead_bro/view_rendering_subscriber.rb', line 88

def self.should_continue_tracking?
  events = Thread.current[THREAD_LOCAL_KEY]
  return false unless events

  # Check count limit
  return false if events.length >= MAX_TRACKED_EVENTS

  # Check time limit
  start_time = Thread.current[DeadBro::TRACKING_START_TIME_KEY]
  if start_time
    elapsed_seconds = Time.now - start_time
    return false if elapsed_seconds >= DeadBro::MAX_TRACKING_DURATION_SECONDS
  end

  true
end

.start_request_trackingObject



71
72
73
# File 'lib/dead_bro/view_rendering_subscriber.rb', line 71

def self.start_request_tracking
  Thread.current[THREAD_LOCAL_KEY] = []
end

.stop_request_trackingObject



75
76
77
78
79
# File 'lib/dead_bro/view_rendering_subscriber.rb', line 75

def self.stop_request_tracking
  events = Thread.current[THREAD_LOCAL_KEY]
  Thread.current[THREAD_LOCAL_KEY] = nil
  events || []
end

.subscribe!(client: Client.new) ⇒ Object



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
# File 'lib/dead_bro/view_rendering_subscriber.rb', line 15

def self.subscribe!(client: Client.new)
  # Track template rendering
  ActiveSupport::Notifications.subscribe(RENDER_TEMPLATE_EVENT) do |name, started, finished, _unique_id, data|
    duration_ms = ((finished - started) * 1000.0).round(2)

    view_info = {
      type: "template",
      identifier: safe_identifier(data[:identifier]),
      layout: data[:layout],
      duration_ms: duration_ms,
      virtual_path: data[:virtual_path],
      rendered_at: Time.now.utc.to_i
    }

    add_view_event(view_info)
  end

  # Track partial rendering
  ActiveSupport::Notifications.subscribe(RENDER_PARTIAL_EVENT) do |name, started, finished, _unique_id, data|
    duration_ms = ((finished - started) * 1000.0).round(2)

    view_info = {
      type: "partial",
      identifier: safe_identifier(data[:identifier]),
      layout: data[:layout],
      duration_ms: duration_ms,
      virtual_path: data[:virtual_path],
      cache_key: data[:cache_key],
      rendered_at: Time.now.utc.to_i
    }

    add_view_event(view_info)
  end

  # Track collection rendering (for partials rendered in loops)
  ActiveSupport::Notifications.subscribe(RENDER_COLLECTION_EVENT) do |name, started, finished, _unique_id, data|
    duration_ms = ((finished - started) * 1000.0).round(2)

    view_info = {
      type: "collection",
      identifier: safe_identifier(data[:identifier]),
      layout: data[:layout],
      duration_ms: duration_ms,
      virtual_path: data[:virtual_path],
      cache_key: data[:cache_key],
      count: data[:count],
      cached_count: data[:cached_count],
      rendered_at: Time.now.utc.to_i
    }

    add_view_event(view_info)
  end
rescue
  # Never raise from instrumentation install
end