module OneApm::DeveloperModeHelper

Constants

OA_SORT_REPLACEMENTS

Private Instance Methods

agent_views_path(path) click to toggle source
# File lib/one_apm/rack/developer_mode/helper.rb, line 52
def agent_views_path(path)
  path
end
application_caller(trace) click to toggle source

return the highest level in the call stack for the trace that is not rails or oneapm agent code

# File lib/one_apm/rack/developer_mode/helper.rb, line 22
def application_caller(trace)
  trace = strip_oa_from_backtrace(trace) unless params[:show_nr]
  trace.each do |trace_line|
    file, _line, gem = file_and_line(trace_line)
    unless file && exclude_file_from_stack_trace?(file, false, gem)
      return trace_line
    end
  end
  trace.last
end
application_stack_trace(trace, include_rails = false) click to toggle source
# File lib/one_apm/rack/developer_mode/helper.rb, line 33
def application_stack_trace(trace, include_rails = false)
  trace = strip_oa_from_backtrace(trace) unless params[:show_nr]
  trace.reject do |trace_line|
    file, _line, gem = file_and_line(trace_line)
    file && exclude_file_from_stack_trace?(file, include_rails, gem)
  end
end
collapsed_image_path() click to toggle source
# File lib/one_apm/rack/developer_mode/helper.rb, line 92
def collapsed_image_path()
  '/oneapm/assets/images/arrow-close.png'
end
colorize(value, yellow_threshold = 0.05, red_threshold = 0.15, s=to_ms(value)) click to toggle source
# File lib/one_apm/rack/developer_mode/helper.rb, line 79
def colorize(value, yellow_threshold = 0.05, red_threshold = 0.15, s=to_ms(value))
  if value > yellow_threshold
    color = (value > red_threshold ? 'red' : 'orange')
    "<font color=#{color}>#{s}</font>"
  else
    "#{s}"
  end
end
cycle(even, odd) click to toggle source
# File lib/one_apm/rack/developer_mode/helper.rb, line 239
def cycle(even, odd)
  @cycle ||= 'a'
  if @cycle == 'a'
    @cycle = 'b'
    even
  else
    @cycle = 'a'
    odd
  end
end
dot?() click to toggle source
# File lib/one_apm/rack/developer_mode/helper.rb, line 299
def dot?
  params['t'] == 'dot'
end
exclude_file_from_stack_trace?(file, include_rails, gem=nil) click to toggle source
# File lib/one_apm/rack/developer_mode/helper.rb, line 202
def exclude_file_from_stack_trace?(file, include_rails, gem=nil)
  return false if include_rails
  return true if file !~ /\.(rb|java)/
  return true if %w[rack activerecord activeresource activesupport actionpack railties].include? gem
  %w[/actionmailer/
           /activerecord
           /activeresource
           /activesupport
           /lib/mongrel
           /actionpack
           /passenger/
           /railties
           benchmark.rb].each { |s| return true if file.include? s }
   false
end
expand_segment_image(segment, depth) click to toggle source
# File lib/one_apm/rack/developer_mode/helper.rb, line 117
def expand_segment_image(segment, depth)
  if depth > 0
    if !segment.called_segments.empty?
      row_class =segment_child_row_class(segment)
      link_to_function("<img src=\"#{collapsed_image_path}\" id=\"image_#{row_class}\" class_for_children=\"#{row_class}\" class=\"#{(!segment.called_segments.empty?) ? 'parent_segment_image' : 'child_segment_image'}\" />", "toggle_row_class(this)")
    end
  end
end
expanded_image_path() click to toggle source
# File lib/one_apm/rack/developer_mode/helper.rb, line 88
def expanded_image_path()
  '/oneapm/assets/images/arrow-open.png'
end
explain_sql_url(segment) click to toggle source
# File lib/one_apm/rack/developer_mode/helper.rb, line 96
def explain_sql_url(segment)
  "explain_sql?id=#{@sample.sample_id}&amp;segment=#{segment.segment_id}"
end
file_and_line(stack_trace_line) click to toggle source

return three objects, the file path, the line in the file, and the gem the file belongs to if found

# File lib/one_apm/rack/developer_mode/helper.rb, line 171
def file_and_line(stack_trace_line)
  if stack_trace_line =~ /^(?:(\w+) \([\d.]*\) )?(.*):(\d+)/
    return $2, $3, $1
  else
    return nil
  end
end
format_timestamp(time) click to toggle source
# File lib/one_apm/rack/developer_mode/helper.rb, line 75
def format_timestamp(time)
  time.strftime("%H:%M:%S")
end
h(text) click to toggle source
# File lib/one_apm/rack/developer_mode/helper.rb, line 48
def h(text)
  text
end
line_wrap_sql(sql) click to toggle source
# File lib/one_apm/rack/developer_mode/helper.rb, line 104
def line_wrap_sql(sql)
  sql.gsub(/\,/,', ').squeeze(' ') if sql
end
mime_type_from_extension(extension) click to toggle source
# File lib/one_apm/rack/developer_mode/helper.rb, line 254
def mime_type_from_extension(extension)
  extension = extension[/[^.]*$/].dncase
  case extension
    when 'png'; 'image/png'
    when 'gif'; 'image/gif'
    when 'jpg'; 'image/jpg'
    when 'css'; 'text/css'
    when 'js'; 'text/javascript'
  else 'text/plain'
  end
end
profile_table(sample, options) click to toggle source
# File lib/one_apm/rack/developer_mode/helper.rb, line 285
def profile_table(sample, options)
  out = StringIO.new
  sample.profile.eliminate_methods!([/OneApm/]) rescue nil
  if dot? 
    printer = ::RubyProf::DotPrinter.new(sample.profile)
    printer.print(out, options)
    "\"#{out.string .gsub('"', '\"').gsub("\n", '')}\""
  else
    printer = ::RubyProf::CallStackPrinter.new(sample.profile)
    printer.print(out, options)
    out.string
  end
end
render_backtrace() click to toggle source
# File lib/one_apm/rack/developer_mode/helper.rb, line 41
def render_backtrace
  if @segment[:backtrace]
    content_tag('h3', '调用堆栈') +
    render(:partial => 'stack_trace')
  end
end
render_indentation_classes(depth) click to toggle source

render_segment_details should be called before calling this method

# File lib/one_apm/rack/developer_mode/helper.rb, line 142
def render_indentation_classes(depth)
  styles = []
   (1..depth).each do |d|
    styles <<  ".segment_indent_level#{d} { display: inline-block; margin-left: #{(d-1)*10}px }"
  end
  content_tag("style", styles.join(' '))
end
render_sample_details(sample) click to toggle source
# File lib/one_apm/rack/developer_mode/helper.rb, line 108
def render_sample_details(sample)
  @indentation_depth=0
  # skip past the root segments to the first child, which is always the controller
  first_segment = sample.root_segment.called_segments.first

  # render the segments, then the css classes to indent them
  render_segment_details(first_segment).to_s + render_indentation_classes(@indentation_depth).to_s
end
render_segment_details(segment, depth=0) click to toggle source
# File lib/one_apm/rack/developer_mode/helper.rb, line 179
def render_segment_details(segment, depth=0)
  @detail_segment_count ||= 0
  @detail_segment_count += 1

  return '' if @detail_segment_count > trace_row_display_limit

  @indentation_depth = depth if depth > @indentation_depth
  repeat = nil
  if segment.is_a?(OneApm::TransactionSample::CompositeSegment)
    html = ''
  else
    repeat = segment.parent_segment.detail_segments.length if segment.parent_segment.is_a?(OneApm::TransactionSample::CompositeSegment)
    html = render(:partial => 'segment', :object => [segment, depth, repeat])
    depth += 1
  end

  segment.called_segments.each do |child|
    html << render_segment_details(child, depth)
  end

  html
end
segment_child_row_class(segment) click to toggle source
# File lib/one_apm/rack/developer_mode/helper.rb, line 126
def segment_child_row_class(segment)
  "segment#{segment.segment_id}"
end
segment_duration_value(segment) click to toggle source
# File lib/one_apm/rack/developer_mode/helper.rb, line 100
def segment_duration_value(segment)
  link_to colorize(segment.duration, 0.05, 0.15, "#{with_delimiter(to_ms(segment.duration))} ms"), explain_sql_url(segment)
end
segment_row_classes(segment, depth) click to toggle source
# File lib/one_apm/rack/developer_mode/helper.rb, line 130
def segment_row_classes(segment, depth)
  classes = []

  classes << "segment#{segment.parent_segment.segment_id}" if depth > 1

  classes << "view_segment" if segment.metric_name.index('View') == 0
  classes << "summary_segment" if segment.is_a?(OneApm::TransactionSample::CompositeSegment)

  classes.join(' ')
end
timestamp(segment) click to toggle source

print the formatted timestamp for a segment

# File lib/one_apm/rack/developer_mode/helper.rb, line 71
def timestamp(segment)
  sprintf("%1.3f", segment.entry_timestamp)
end
to_ms(number) click to toggle source
# File lib/one_apm/rack/developer_mode/helper.rb, line 265
def to_ms(number)
 (number*1000).round
end
to_percentage(value) click to toggle source
# File lib/one_apm/rack/developer_mode/helper.rb, line 268
def to_percentage(value)
 (value * 100).round if value
end
trace_row_display_limit() click to toggle source

limit of how many detail/SQL rows we display - very large data sets (~10000+) crash browsers

# File lib/one_apm/rack/developer_mode/helper.rb, line 12
def trace_row_display_limit
  2000
end
trace_row_display_limit_reached() click to toggle source
# File lib/one_apm/rack/developer_mode/helper.rb, line 16
def trace_row_display_limit_reached
 (!@detail_segment_count.nil? && @detail_segment_count > trace_row_display_limit) || @sample.sql_segments.length > trace_row_display_limit
end
with_delimiter(val) click to toggle source
# File lib/one_apm/rack/developer_mode/helper.rb, line 271
def with_delimiter(val)
  return '0' if val.nil?
  parts = val.to_s.split('.')
  parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1,")
  parts.join '.'
end
write_segment_label(segment) click to toggle source

write the metric label for a segment metric in the detail view

# File lib/one_apm/rack/developer_mode/helper.rb, line 57
def write_segment_label(segment)
  link_to_function(segment.metric_name, "toggle_row_class($(this).closest('td').find('a')[0])")
end
write_stack_trace_line(trace_line) click to toggle source
# File lib/one_apm/rack/developer_mode/helper.rb, line 66
def write_stack_trace_line(trace_line)
  trace_line
end
write_summary_segment_label(segment) click to toggle source

write the metric label for a segment metric in the summary table of metrics

# File lib/one_apm/rack/developer_mode/helper.rb, line 62
def write_summary_segment_label(segment)
  segment.metric_name
end