Class: OpenTrace::RequestCollector

Inherits:
Object
  • Object
show all
Defined in:
lib/opentrace/request_collector.rb

Constant Summary collapse

MAX_TIMELINE_EVENTS =
200

Instance Attribute Summary collapse

Instance Method Summary collapse

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
# 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

  if @timeline_enabled
    @timeline = []
    @request_start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  else
    @timeline = nil
  end
end

Instance Attribute Details

#cache_hitsObject (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_readsObject (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_writesObject (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_countObject (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_msObject (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_afterObject

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_beforeObject

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_countObject (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_msObject (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_countObject (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_msObject (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



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/opentrace/request_collector.rb', line 76

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



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/opentrace/request_collector.rb', line 92

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_sql(name:, duration_ms:, table: nil) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/opentrace/request_collector.rb', line 48

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

  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



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/opentrace/request_collector.rb', line 62

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

#summaryObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/opentrace/request_collector.rb', line 108

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
  }

  # 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