Class: Profiler::Collectors::FlameGraphCollector

Inherits:
BaseCollector show all
Defined in:
lib/profiler/collectors/flamegraph_collector.rb

Instance Attribute Summary

Attributes inherited from BaseCollector

#profile

Instance Method Summary collapse

Methods inherited from BaseCollector

descendants, #has_data?, inherited, #name, #panel_content, #render_html, #render_mode

Constructor Details

#initialize(profile) ⇒ FlameGraphCollector

Returns a new instance of FlameGraphCollector.



9
10
11
12
13
# File 'lib/profiler/collectors/flamegraph_collector.rb', line 9

def initialize(profile)
  super
  @events = []
  @subscriptions = []
end

Instance Method Details

#collectObject



134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/profiler/collectors/flamegraph_collector.rb', line 134

def collect
  @subscriptions.each { |sub| ActiveSupport::Notifications.unsubscribe(sub) }
  Thread.current[:profiler_flamegraph_collector] = nil

  root_events = build_hierarchy(@events)

  store_data({
    total_events: @events.size,
    total_duration: @events.empty? ? 0 : @events.map(&:duration).sum.round(2),
    root_events: root_events.map(&:to_h)
  })
end

#iconObject



15
16
17
# File 'lib/profiler/collectors/flamegraph_collector.rb', line 15

def icon
  "🔥"
end

#priorityObject



19
20
21
# File 'lib/profiler/collectors/flamegraph_collector.rb', line 19

def priority
  30
end

#record_custom_event(label:, started_at:, finished_at:, metadata: {}) ⇒ Object

Called by Profiler.measure to record custom instrumentation events



113
114
115
116
117
118
119
120
121
# File 'lib/profiler/collectors/flamegraph_collector.rb', line 113

def record_custom_event(label:, started_at:, finished_at:, metadata: {})
  @events << Models::TimelineEvent.new(
    name: label,
    started_at: started_at,
    finished_at: finished_at,
    category: "custom",
    payload: 
  )
end

#record_http_event(started_at:, finished_at:, url:, method:, status:) ⇒ Object

Called by NetHttpInstrumentation to record outbound HTTP events



124
125
126
127
128
129
130
131
132
# File 'lib/profiler/collectors/flamegraph_collector.rb', line 124

def record_http_event(started_at:, finished_at:, url:, method:, status:)
  @events << Models::TimelineEvent.new(
    name: "HTTP #{method} #{url}",
    started_at: started_at,
    finished_at: finished_at,
    category: "http",
    payload: { url: url, method: method, status: status }
  )
end

#subscribeObject



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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/profiler/collectors/flamegraph_collector.rb', line 34

def subscribe
  return unless defined?(ActiveSupport::Notifications)

  Thread.current[:profiler_flamegraph_collector] = self

  # Controller action
  @subscriptions << ActiveSupport::Notifications.monotonic_subscribe("process_action.action_controller") do |_name, started, finished, _unique_id, payload|
    @events << Models::TimelineEvent.new(
      name: "#{payload[:controller]}##{payload[:action]}",
      started_at: started,
      finished_at: finished,
      category: "controller",
      payload: {
        controller: payload[:controller],
        action: payload[:action],
        format: payload[:format],
        method: payload[:method],
        path: payload[:path],
        status: payload[:status]
      }
    )
  end

  # Template rendering
  @subscriptions << ActiveSupport::Notifications.monotonic_subscribe("render_template.action_view") do |_name, started, finished, _unique_id, payload|
    identifier = short_identifier(payload[:identifier])
    @events << Models::TimelineEvent.new(
      name: "Render: #{identifier}",
      started_at: started,
      finished_at: finished,
      category: "view",
      payload: { identifier: identifier, layout: payload[:layout] }
    )
  end

  # Partial rendering
  @subscriptions << ActiveSupport::Notifications.monotonic_subscribe("render_partial.action_view") do |_name, started, finished, _unique_id, payload|
    identifier = short_identifier(payload[:identifier])
    @events << Models::TimelineEvent.new(
      name: "Partial: #{identifier}",
      started_at: started,
      finished_at: finished,
      category: "partial",
      payload: { identifier: identifier }
    )
  end

  # SQL queries
  @subscriptions << ActiveSupport::Notifications.monotonic_subscribe("sql.active_record") do |_name, started, finished, _unique_id, payload|
    next if payload[:name] == "SCHEMA"
    next if payload[:sql] =~ /^(BEGIN|COMMIT|ROLLBACK|SAVEPOINT)/i

    sql = payload[:sql].to_s
    @events << Models::TimelineEvent.new(
      name: sql.length > 80 ? "#{sql[0, 80]}..." : sql,
      started_at: started,
      finished_at: finished,
      category: "sql",
      payload: { sql: sql, name: payload[:name] }
    )
  end

  # Cache operations
  %w[cache_read.active_support cache_write.active_support cache_delete.active_support].each do |event_name|
    @subscriptions << ActiveSupport::Notifications.monotonic_subscribe(event_name) do |name, started, finished, _unique_id, payload|
      op = name.split(".").first.sub("cache_", "")
      key = payload[:key].to_s
      @events << Models::TimelineEvent.new(
        name: "cache_#{op}: #{key.length > 60 ? "#{key[0, 60]}..." : key}",
        started_at: started,
        finished_at: finished,
        category: "cache",
        payload: { operation: op, key: key, hit: payload[:hit] }
      )
    end
  end
end

#tab_configObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/profiler/collectors/flamegraph_collector.rb', line 23

def tab_config
  {
    key: "flamegraph",
    label: "Flame Graph",
    icon: icon,
    priority: priority,
    enabled: true,
    default_active: false
  }
end

#toolbar_summaryObject



147
148
149
150
151
152
# File 'lib/profiler/collectors/flamegraph_collector.rb', line 147

def toolbar_summary
  {
    text: "#{@events.size} events",
    color: "blue"
  }
end