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_span(operation:, duration_ms:) ⇒ Object
- #record_sql(name:, duration_ms:, table: nil, fingerprint: 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 42 43 44 45 46 47 48 |
# File 'lib/opentrace/request_collector.rb', line 13 def initialize(max_timeline: MAX_TIMELINE_EVENTS) @max_timeline = max_timeline @timeline_enabled = max_timeline > 0 @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 @sql_fingerprints = {} # fingerprint => count if @timeline_enabled @timeline = [] @request_start = Process.clock_gettime(Process::CLOCK_MONOTONIC) else @timeline = nil end 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
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/opentrace/request_collector.rb', line 83 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 if @timeline_enabled append_timeline({ t: :cache, a: action, hit: hit, ms: duration_ms.round(2), at: offset_ms }) end end |
#record_http(method:, url:, host:, status:, duration_ms:, error: nil) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/opentrace/request_collector.rb', line 105 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 if @timeline_enabled 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 end |
#record_span(operation:, duration_ms:) ⇒ Object
99 100 101 102 103 |
# File 'lib/opentrace/request_collector.rb', line 99 def record_span(operation:, duration_ms:) if @timeline_enabled append_timeline({ t: :span, n: operation, ms: duration_ms.round(1), at: offset_ms }) end end |
#record_sql(name:, duration_ms:, table: nil, fingerprint: nil) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/opentrace/request_collector.rb', line 50 def record_sql(name:, duration_ms:, table: nil, fingerprint: 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 # Track duplicate queries by fingerprint if fingerprint && @sql_fingerprints.size < 100 @sql_fingerprints[fingerprint] = (@sql_fingerprints[fingerprint] || 0) + 1 end if @timeline_enabled append_timeline({ t: :sql, n: name, ms: duration_ms.round(1), at: offset_ms }) end end |
#record_view(template:, duration_ms:) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/opentrace/request_collector.rb', line 69 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 if @timeline_enabled append_timeline({ t: :view, n: template, ms: duration_ms.round(1), at: offset_ms }) end end |
#summary ⇒ Object
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 |
# File 'lib/opentrace/request_collector.rb', line 121 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.nil? || @timeline.empty?) ? nil : @timeline } # Duplicate query detection duplicates = @sql_fingerprints.select { |_, count| count > 1 } unless duplicates.empty? result[:duplicate_queries] = duplicates.size result[:worst_duplicate_count] = duplicates.values.max result[:top_duplicates] = duplicates .sort_by { |_, count| -count } .first(3) .map { |fp, count| { fingerprint: fp, count: count } } result[:n_plus_one_warning] = true if duplicates.values.max > 5 end # 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 |