Class: BaselineRedRpm::Tracer

Inherits:
Object
  • Object
show all
Defined in:
lib/baseline_red_rpm/tracer.rb

Class Method Summary collapse

Class Method Details

.profile(layer, opts = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/baseline_red_rpm/tracer.rb', line 41

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" => ::BaselineRedRpm::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_percentageObject



33
34
35
# File 'lib/baseline_red_rpm/tracer.rb', line 33

def random_percentage
  rand * 100
end

.sample!(incoming_trace = nil, force = false) ⇒ Object

This method should be called by any components that are capable of starting the tracing process. ie. rack, sidekiq worker, etc



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/baseline_red_rpm/tracer.rb', line 9

def sample!(incoming_trace = nil, force = false)
  # Since we keep track of the active span, meaning we have entered into
  # tracing at some point, and we no longer have an active span,
  # reset tracing.
  sample_off! if !BaselineRedRpm.tracer.active_span

  # Now determine if we want to trace, either by an incoming
  # trace or meeting the sample rate.
  Thread.current[:sample] = force || !!incoming_trace || should_sample?
  Thread.current[:sample]
end

.sample_off!Object



21
22
23
# File 'lib/baseline_red_rpm/tracer.rb', line 21

def sample_off!
  Thread.current[:sample] = false
end

.sampled?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/baseline_red_rpm/tracer.rb', line 25

def sampled?
  !!Thread.current[:sample]
end

.should_sample?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/baseline_red_rpm/tracer.rb', line 37

def should_sample?
  random_percentage <= ::BaselineRedRpm.config.sample_rate.to_i
end

.tracing?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/baseline_red_rpm/tracer.rb', line 29

def tracing?
  BaselineRedRpm.tracing? && sampled?
end