Class: AppPerfRpm::Tracer
- Inherits:
-
Object
- Object
- AppPerfRpm::Tracer
- Defined in:
- lib/app_perf_rpm/tracer.rb
Defined Under Namespace
Classes: Instance
Class Method Summary collapse
- .generate_trace_id ⇒ Object
- .in_trace? ⇒ Boolean
- .log_event(event, opts = {}) ⇒ Object
- .profile(layer, opts = {}) ⇒ Object
- .random_percentage ⇒ Object
- .should_trace? ⇒ Boolean
- .start_span(layer, opts = {}) ⇒ Object
- .start_trace(layer, opts = {}) ⇒ Object
- .trace(layer, opts = {}) ⇒ Object
- .trace_id ⇒ Object
- .trace_id=(t) ⇒ Object
- .tracing? ⇒ Boolean
Class Method Details
.generate_trace_id ⇒ Object
106 107 108 |
# File 'lib/app_perf_rpm/tracer.rb', line 106 def generate_trace_id Digest::SHA1.hexdigest([Time.now, rand].join) end |
.in_trace? ⇒ Boolean
16 17 18 |
# File 'lib/app_perf_rpm/tracer.rb', line 16 def in_trace? !Thread.current[:trace_id].nil? end |
.log_event(event, opts = {}) ⇒ Object
102 103 104 |
# File 'lib/app_perf_rpm/tracer.rb', line 102 def log_event(event, opts = {}) ::AppPerfRpm.store([event, generate_trace_id, Time.now.to_f, opts]) end |
.profile(layer, opts = {}) ⇒ Object
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 |
# File 'lib/app_perf_rpm/tracer.rb', line 69 def profile(layer, opts = {}) if defined?(TracePoint) @times = {} traces = [] tracer = TracePoint.new(:call, :return) do |tp| backtrace = caller(0) key = "#{tp.defined_class}_#{tp.method_id}_#{backtrace.size}" if tp.event == :call @times[key] = Time.now.to_f else if @times[key] @times[key] = Time.now.to_f - @times[key].to_f traces << { "duration "=> @times[key].to_f, "class" => tp.defined_class, "method" => tp.method_id, "backtrace" => backtrace, "line" => ::AppPerfRpm::Backtrace.send(:clean_line, tp.path), "line_number" => tp.lineno } end end end result = tracer.enable { yield } @times = {} return traces, result else return [], yield end end |
.random_percentage ⇒ Object
24 25 26 |
# File 'lib/app_perf_rpm/tracer.rb', line 24 def random_percentage rand * 100 end |
.should_trace? ⇒ Boolean
28 29 30 |
# File 'lib/app_perf_rpm/tracer.rb', line 28 def should_trace? random_percentage < ::AppPerfRpm.configuration.sample_rate.to_i end |
.start_span(layer, opts = {}) ⇒ Object
20 21 22 |
# File 'lib/app_perf_rpm/tracer.rb', line 20 def start_span(layer, opts = {}) Instance.new(layer, opts) end |
.start_trace(layer, opts = {}) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/app_perf_rpm/tracer.rb', line 32 def start_trace(layer, opts = {}) start = Time.now.to_f trace_id = opts.delete("trace_id") if trace_id || should_trace? self.trace_id = trace_id || generate_trace_id result = trace(layer, opts) do |span| yield(span) end self.trace_id = nil else span = Span.new result = yield(span) end return result, (Time.now.to_f - start) * 1000 end |
.trace(layer, opts = {}) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/app_perf_rpm/tracer.rb', line 50 def trace(layer, opts = {}) result = nil if tracing? span = Span.new span.layer = layer span.trace_id = self.trace_id span.started_at = Time.now.to_f result = yield(span) span.ended_at = Time.now.to_f span..merge!(opts) ::AppPerfRpm.store(span) else result = yield end result end |
.trace_id ⇒ Object
4 5 6 |
# File 'lib/app_perf_rpm/tracer.rb', line 4 def trace_id Thread.current[:trace_id] end |
.trace_id=(t) ⇒ Object
8 9 10 |
# File 'lib/app_perf_rpm/tracer.rb', line 8 def trace_id=(t) Thread.current[:trace_id] = t end |
.tracing? ⇒ Boolean
12 13 14 |
# File 'lib/app_perf_rpm/tracer.rb', line 12 def tracing? Thread.current[:trace_id] end |