Class: OpenTrace::RequestCollector
- Inherits:
-
Object
- Object
- OpenTrace::RequestCollector
- Defined in:
- lib/opentrace/request_collector.rb
Constant Summary collapse
- MAX_TIMELINE_EVENTS =
200
Instance Attribute Summary collapse
-
#cache_hits ⇒ Object
readonly
Returns the value of attribute cache_hits.
-
#cache_reads ⇒ Object
readonly
Returns the value of attribute cache_reads.
-
#cache_writes ⇒ Object
readonly
Returns the value of attribute cache_writes.
-
#http_count ⇒ Object
readonly
Returns the value of attribute http_count.
-
#http_total_ms ⇒ Object
readonly
Returns the value of attribute http_total_ms.
-
#memory_after ⇒ Object
Returns the value of attribute memory_after.
-
#memory_before ⇒ Object
Returns the value of attribute memory_before.
-
#sql_count ⇒ Object
readonly
Returns the value of attribute sql_count.
-
#sql_total_ms ⇒ Object
readonly
Returns the value of attribute sql_total_ms.
-
#view_count ⇒ Object
readonly
Returns the value of attribute view_count.
-
#view_total_ms ⇒ Object
readonly
Returns the value of attribute view_total_ms.
Instance Method Summary collapse
-
#initialize(max_timeline: MAX_TIMELINE_EVENTS) ⇒ RequestCollector
constructor
A new instance of RequestCollector.
- #record_cache(action:, hit: nil, duration_ms: 0.0) ⇒ Object
- #record_http(method:, url:, host:, status:, duration_ms:, error: nil) ⇒ Object
- #record_sql(name:, duration_ms:, table: nil) ⇒ Object
- #record_view(template:, duration_ms:) ⇒ Object
- #summary ⇒ Object
Constructor Details
#initialize(max_timeline: MAX_TIMELINE_EVENTS) ⇒ RequestCollector
Returns a new instance of RequestCollector.
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 |
# File 'lib/opentrace/request_collector.rb', line 13 def initialize(max_timeline: MAX_TIMELINE_EVENTS) @max_timeline = max_timeline @sql_count = 0 @sql_total_ms = 0.0 @sql_slowest_ms = 0.0 @sql_slowest_name = nil @view_count = 0 @view_total_ms = 0.0 @view_slowest_ms = 0.0 @view_slowest_template = nil @cache_reads = 0 @cache_hits = 0 @cache_writes = 0 @cache_deletes = 0 @http_count = 0 @http_total_ms = 0.0 @http_slowest_ms = 0.0 @http_slowest_host = nil @memory_before = nil @memory_after = nil @timeline = [] @request_start = Process.clock_gettime(Process::CLOCK_MONOTONIC) end |
Instance Attribute Details
#cache_hits ⇒ Object (readonly)
Returns the value of attribute cache_hits.
7 8 9 |
# File 'lib/opentrace/request_collector.rb', line 7 def cache_hits @cache_hits end |
#cache_reads ⇒ Object (readonly)
Returns the value of attribute cache_reads.
7 8 9 |
# File 'lib/opentrace/request_collector.rb', line 7 def cache_reads @cache_reads end |
#cache_writes ⇒ Object (readonly)
Returns the value of attribute cache_writes.
7 8 9 |
# File 'lib/opentrace/request_collector.rb', line 7 def cache_writes @cache_writes end |
#http_count ⇒ Object (readonly)
Returns the value of attribute http_count.
7 8 9 |
# File 'lib/opentrace/request_collector.rb', line 7 def http_count @http_count end |
#http_total_ms ⇒ Object (readonly)
Returns the value of attribute http_total_ms.
7 8 9 |
# File 'lib/opentrace/request_collector.rb', line 7 def http_total_ms @http_total_ms end |
#memory_after ⇒ Object
Returns the value of attribute memory_after.
11 12 13 |
# File 'lib/opentrace/request_collector.rb', line 11 def memory_after @memory_after end |
#memory_before ⇒ Object
Returns the value of attribute memory_before.
11 12 13 |
# File 'lib/opentrace/request_collector.rb', line 11 def memory_before @memory_before end |
#sql_count ⇒ Object (readonly)
Returns the value of attribute sql_count.
7 8 9 |
# File 'lib/opentrace/request_collector.rb', line 7 def sql_count @sql_count end |
#sql_total_ms ⇒ Object (readonly)
Returns the value of attribute sql_total_ms.
7 8 9 |
# File 'lib/opentrace/request_collector.rb', line 7 def sql_total_ms @sql_total_ms end |
#view_count ⇒ Object (readonly)
Returns the value of attribute view_count.
7 8 9 |
# File 'lib/opentrace/request_collector.rb', line 7 def view_count @view_count end |
#view_total_ms ⇒ Object (readonly)
Returns the value of attribute view_total_ms.
7 8 9 |
# File 'lib/opentrace/request_collector.rb', line 7 def view_total_ms @view_total_ms end |
Instance Method Details
#record_cache(action:, hit: nil, duration_ms: 0.0) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/opentrace/request_collector.rb', line 67 def record_cache(action:, hit: nil, duration_ms: 0.0) case action when :read @cache_reads += 1 @cache_hits += 1 if hit when :write @cache_writes += 1 when :delete @cache_deletes += 1 end append_timeline({ t: :cache, a: action, hit: hit, ms: duration_ms.round(2), at: offset_ms }) end |
#record_http(method:, url:, host:, status:, duration_ms:, error: nil) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/opentrace/request_collector.rb', line 81 def record_http(method:, url:, host:, status:, duration_ms:, error: nil) @http_count += 1 @http_total_ms += duration_ms if duration_ms > @http_slowest_ms @http_slowest_ms = duration_ms @http_slowest_host = host end entry = { t: :http, n: "#{method} #{host}", ms: duration_ms.round(1), s: status, at: offset_ms } entry[:err] = error if error append_timeline(entry) end |
#record_sql(name:, duration_ms:, table: nil) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/opentrace/request_collector.rb', line 43 def record_sql(name:, duration_ms:, table: nil) @sql_count += 1 @sql_total_ms += duration_ms if duration_ms > @sql_slowest_ms @sql_slowest_ms = duration_ms @sql_slowest_name = name end append_timeline({ t: :sql, n: name, ms: duration_ms.round(1), at: offset_ms }) end |
#record_view(template:, duration_ms:) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/opentrace/request_collector.rb', line 55 def record_view(template:, duration_ms:) @view_count += 1 @view_total_ms += duration_ms if duration_ms > @view_slowest_ms @view_slowest_ms = duration_ms @view_slowest_template = template end append_timeline({ t: :view, n: template, ms: duration_ms.round(1), at: offset_ms }) end |
#summary ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/opentrace/request_collector.rb', line 95 def summary result = { sql_query_count: @sql_count, sql_total_ms: @sql_total_ms.round(1), sql_slowest_ms: @sql_slowest_ms.round(1), sql_slowest_name: @sql_slowest_name, view_render_count: @view_count, view_total_ms: @view_total_ms.round(1), view_slowest_ms: @view_slowest_ms.round(1), view_slowest_template: @view_slowest_template, cache_reads: @cache_reads, cache_hits: @cache_hits, cache_writes: @cache_writes, cache_hit_ratio: @cache_reads > 0 ? (@cache_hits.to_f / @cache_reads).round(2) : nil, n_plus_one_warning: @sql_count > 20 ? true : nil, timeline: @timeline.empty? ? nil : @timeline } # HTTP stats (only present if calls were made) if @http_count > 0 result[:http_external_count] = @http_count result[:http_external_total_ms] = @http_total_ms.round(1) result[:http_slowest_ms] = @http_slowest_ms.round(1) result[:http_slowest_host] = @http_slowest_host end # Memory stats (only present if memory_tracking is enabled) if @memory_before && @memory_after result[:memory_before_mb] = @memory_before result[:memory_after_mb] = @memory_after result[:memory_delta_mb] = (@memory_after - @memory_before).round(1) end result.compact end |