Class: Profiler::Collectors::FlameGraphCollector
- Inherits:
-
BaseCollector
- Object
- BaseCollector
- Profiler::Collectors::FlameGraphCollector
- Defined in:
- lib/profiler/collectors/flamegraph_collector.rb
Instance Attribute Summary
Attributes inherited from BaseCollector
Instance Method Summary collapse
- #collect ⇒ Object
- #icon ⇒ Object
-
#initialize(profile) ⇒ FlameGraphCollector
constructor
A new instance of FlameGraphCollector.
- #priority ⇒ Object
-
#record_custom_event(label:, started_at:, finished_at:, metadata: {}) ⇒ Object
Called by Profiler.measure to record custom instrumentation events.
-
#record_http_event(started_at:, finished_at:, url:, method:, status:) ⇒ Object
Called by NetHttpInstrumentation to record outbound HTTP events.
- #subscribe ⇒ Object
- #tab_config ⇒ Object
- #toolbar_summary ⇒ Object
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 14 |
# File 'lib/profiler/collectors/flamegraph_collector.rb', line 9 def initialize(profile) super @events = [] @subscriptions = [] @mutex = Mutex.new end |
Instance Method Details
#collect ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/profiler/collectors/flamegraph_collector.rb', line 135 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 |
#icon ⇒ Object
16 17 18 |
# File 'lib/profiler/collectors/flamegraph_collector.rb', line 16 def icon "🔥" end |
#priority ⇒ Object
20 21 22 |
# File 'lib/profiler/collectors/flamegraph_collector.rb', line 20 def priority 30 end |
#record_custom_event(label:, started_at:, finished_at:, metadata: {}) ⇒ Object
Called by Profiler.measure to record custom instrumentation events
114 115 116 117 118 119 120 121 122 |
# File 'lib/profiler/collectors/flamegraph_collector.rb', line 114 def record_custom_event(label:, started_at:, finished_at:, metadata: {}) add_event 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
125 126 127 128 129 130 131 132 133 |
# File 'lib/profiler/collectors/flamegraph_collector.rb', line 125 def record_http_event(started_at:, finished_at:, url:, method:, status:) add_event 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 |
#subscribe ⇒ Object
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 111 |
# File 'lib/profiler/collectors/flamegraph_collector.rb', line 35 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| add_event 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]) add_event 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]) add_event 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 add_event 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 add_event 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_config ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/profiler/collectors/flamegraph_collector.rb', line 24 def tab_config { key: "flamegraph", label: "Flame Graph", icon: icon, priority: priority, enabled: true, default_active: false } end |
#toolbar_summary ⇒ Object
148 149 150 151 152 153 |
# File 'lib/profiler/collectors/flamegraph_collector.rb', line 148 def { text: "#{@events.size} events", color: "blue" } end |