class AppPerfRpm::Tracer

Public Class Methods

profile(layer, opts = {}) { || ... } click to toggle source
# File lib/app_perf_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" => ::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() click to toggle source
# File lib/app_perf_rpm/tracer.rb, line 33
def random_percentage
  rand * 100
end
sample!(incoming_trace = nil, force = false) click to toggle source

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

# File lib/app_perf_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 !AppPerfRpm.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!() click to toggle source
# File lib/app_perf_rpm/tracer.rb, line 21
def sample_off!
  Thread.current[:sample] = false
end
sampled?() click to toggle source
# File lib/app_perf_rpm/tracer.rb, line 25
def sampled?
  !!Thread.current[:sample]
end
should_sample?() click to toggle source
# File lib/app_perf_rpm/tracer.rb, line 37
def should_sample?
  random_percentage <= ::AppPerfRpm.config.sample_rate.to_i
end
tracing?() click to toggle source
# File lib/app_perf_rpm/tracer.rb, line 29
def tracing?
  AppPerfRpm.tracing? && sampled?
end