Class: RailsVitals::Collector
- Inherits:
-
Object
- Object
- RailsVitals::Collector
- Defined in:
- lib/rails_vitals/collector.rb
Instance Attribute Summary collapse
-
#action ⇒ Object
readonly
Returns the value of attribute action.
-
#callbacks ⇒ Object
readonly
Returns the value of attribute callbacks.
-
#controller ⇒ Object
readonly
Returns the value of attribute controller.
-
#duration_ms ⇒ Object
readonly
Returns the value of attribute duration_ms.
-
#http_method ⇒ Object
readonly
Returns the value of attribute http_method.
-
#queries ⇒ Object
readonly
Returns the value of attribute queries.
-
#response_status ⇒ Object
readonly
Returns the value of attribute response_status.
-
#started_at ⇒ Object
readonly
Returns the value of attribute started_at.
Class Method Summary collapse
-
.current ⇒ Object
Thread-local storage accessors.
- .current=(collector) ⇒ Object
- .reset! ⇒ Object
Instance Method Summary collapse
- #add_callback(model:, kind:, duration_ms:) ⇒ Object
-
#add_query(sql:, duration_ms:, source:, binds: []) ⇒ Object
Called by the sql.active_record subscriber.
- #callbacks_by_model ⇒ Object
-
#finalize!(event) ⇒ Object
Called by the process_action.action_controller subscriber.
-
#initialize ⇒ Collector
constructor
A new instance of Collector.
- #slowest_queries(limit = 5) ⇒ Object
- #total_callback_time_ms ⇒ Object
- #total_db_time_ms ⇒ Object
- #total_query_count ⇒ Object
Constructor Details
#initialize ⇒ Collector
Returns a new instance of Collector.
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/rails_vitals/collector.rb', line 6 def initialize @queries = [] @callbacks = [] @controller = nil @action = nil @http_method = nil @response_status = nil @duration_ms = nil @started_at = Time.now end |
Instance Attribute Details
#action ⇒ Object (readonly)
Returns the value of attribute action.
3 4 5 |
# File 'lib/rails_vitals/collector.rb', line 3 def action @action end |
#callbacks ⇒ Object (readonly)
Returns the value of attribute callbacks.
3 4 5 |
# File 'lib/rails_vitals/collector.rb', line 3 def callbacks @callbacks end |
#controller ⇒ Object (readonly)
Returns the value of attribute controller.
3 4 5 |
# File 'lib/rails_vitals/collector.rb', line 3 def controller @controller end |
#duration_ms ⇒ Object (readonly)
Returns the value of attribute duration_ms.
3 4 5 |
# File 'lib/rails_vitals/collector.rb', line 3 def duration_ms @duration_ms end |
#http_method ⇒ Object (readonly)
Returns the value of attribute http_method.
3 4 5 |
# File 'lib/rails_vitals/collector.rb', line 3 def http_method @http_method end |
#queries ⇒ Object (readonly)
Returns the value of attribute queries.
3 4 5 |
# File 'lib/rails_vitals/collector.rb', line 3 def queries @queries end |
#response_status ⇒ Object (readonly)
Returns the value of attribute response_status.
3 4 5 |
# File 'lib/rails_vitals/collector.rb', line 3 def response_status @response_status end |
#started_at ⇒ Object (readonly)
Returns the value of attribute started_at.
3 4 5 |
# File 'lib/rails_vitals/collector.rb', line 3 def started_at @started_at end |
Class Method Details
.current ⇒ Object
Thread-local storage accessors
67 68 69 |
# File 'lib/rails_vitals/collector.rb', line 67 def self.current Thread.current[:rails_vitals_collector] end |
.current=(collector) ⇒ Object
71 72 73 |
# File 'lib/rails_vitals/collector.rb', line 71 def self.current=(collector) Thread.current[:rails_vitals_collector] = collector end |
.reset! ⇒ Object
75 76 77 |
# File 'lib/rails_vitals/collector.rb', line 75 def self.reset! Thread.current[:rails_vitals_collector] = nil end |
Instance Method Details
#add_callback(model:, kind:, duration_ms:) ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/rails_vitals/collector.rb', line 28 def add_callback(model:, kind:, duration_ms:) @callbacks << { model: model, kind: kind, duration_ms: duration_ms, called_at: Time.now } end |
#add_query(sql:, duration_ms:, source:, binds: []) ⇒ Object
Called by the sql.active_record subscriber
18 19 20 21 22 23 24 25 26 |
# File 'lib/rails_vitals/collector.rb', line 18 def add_query(sql:, duration_ms:, source:, binds: []) @queries << { sql: sql, duration_ms: duration_ms, source: source, binds: binds, called_at: Time.now } end |
#callbacks_by_model ⇒ Object
62 63 64 |
# File 'lib/rails_vitals/collector.rb', line 62 def callbacks_by_model @callbacks.group_by { |c| c[:model] } end |
#finalize!(event) ⇒ Object
Called by the process_action.action_controller subscriber
38 39 40 41 42 43 44 |
# File 'lib/rails_vitals/collector.rb', line 38 def finalize!(event) @controller = event.payload[:controller] @action = event.payload[:action] @http_method = event.payload[:method] @response_status = event.payload[:status] @duration_ms = event.duration end |
#slowest_queries(limit = 5) ⇒ Object
54 55 56 |
# File 'lib/rails_vitals/collector.rb', line 54 def slowest_queries(limit = 5) @queries.sort_by { |q| -q[:duration_ms] }.first(limit) end |
#total_callback_time_ms ⇒ Object
58 59 60 |
# File 'lib/rails_vitals/collector.rb', line 58 def total_callback_time_ms @callbacks.sum { |c| c[:duration_ms] } end |
#total_db_time_ms ⇒ Object
50 51 52 |
# File 'lib/rails_vitals/collector.rb', line 50 def total_db_time_ms @queries.sum { |q| q[:duration_ms] } end |
#total_query_count ⇒ Object
46 47 48 |
# File 'lib/rails_vitals/collector.rb', line 46 def total_query_count @queries.size end |