Class: AppPerfRpm::Backtrace

Inherits:
Object
  • Object
show all
Defined in:
lib/app_perf_rpm/backtrace.rb

Class Method Summary collapse

Class Method Details

.backtrace(opts = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/app_perf_rpm/backtrace.rb', line 6

def backtrace(opts = {})
  kind = opts[:kind]

  if kind
    bt = Kernel.caller
    bt = clean(bt)
    bt = filter(bt, kind)
    trim_backtrace(bt)
  end
end

.clean(backtrace) ⇒ Object



17
18
19
20
21
# File 'lib/app_perf_rpm/backtrace.rb', line 17

def clean(backtrace)
  Array(backtrace)
    .map {|b| clean_line(b) }
    .select {|b| b !~ %r{lib/app_perf_rpm} }
end

.clean_line(line) ⇒ Object



66
67
68
69
70
# File 'lib/app_perf_rpm/backtrace.rb', line 66

def clean_line(line)
  line
    .sub(/#{::AppPerfRpm.config.app_root.to_s}\//, "[APP_PATH]/")
    .sub(gems_regexp, '\2 (\3) [GEM_PATH]/\4')
end

.filter(backtrace, kind) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/app_perf_rpm/backtrace.rb', line 23

def filter(backtrace, kind)
  case kind
  when :all
    backtrace
  when :app
    backtrace.select {|b| b =~ /\[APP_PATH\]/ }
  when :gem
    backtrace.select {|b| b =~ /\[GEM_PATH\]/ }
  else
    []
  end
end

.source_extract(opts = {}) ⇒ Object

{

    code: source_fragment(file, line_number),
    line_number: line_number
  }
else
  nil
end

end



49
50
51
52
53
54
55
56
# File 'lib/app_perf_rpm/backtrace.rb', line 49

def source_extract(opts = {})
  backtrace = opts[:backtrace] || Kernel.caller(0)

  Array(backtrace).select {|bt| bt[/^#{::AppPerfRpm.config.app_root.to_s}\//] }.map do |trace|
    file, line_number = extract_file_and_line_number(trace)
    source_to_hash(file, line_number)
  end
end

.source_to_hash(file, line_number) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/app_perf_rpm/backtrace.rb', line 58

def source_to_hash(file, line_number)
  {
    "file" => clean_line(file),
    "code" => source_fragment(file, line_number),
    "line_number" => line_number
  }
end