class StackifyRubyAPM::Util::Inspector

@api private

Public Class Methods

new(width = 80) click to toggle source
# File lib/stackify_apm/util/inspector.rb, line 5
def initialize(width = 80)
  @width = width
end

Public Instance Methods

transaction(transaction) click to toggle source
# File lib/stackify_apm/util/inspector.rb, line 9
def transaction(transaction)
  unless transaction.done?
    raise ArgumentError, 'Transaction still running'
  end

  width_factor = @width.to_f / ms(transaction.duration)

  lines = ['=' * @width]
  lines << "[T] #{transaction.name} " \
    "- #{transaction.type} (#{ms transaction.duration} ms)"
  lines << "+#{'-' * (@width - 2)}+"

  transaction.spans.each do |span|
    indent = (ms(span.relative_start) * width_factor).to_i

    if span.duration
      span_width = ms(span.duration) * width_factor
      duration_desc = ms(span.duration)
    else
      span_width = @width - indent
      duration_desc = 'RUNNING'
    end

    description = "[#{span.id}] " \
      "#{span.name} - #{span.type} (#{duration_desc} ms)"
    description_indent = [
      0,
      [indent, @width - description.length].min
    ].max

    lines << "#{' ' * description_indent}#{description}"
    lines << "#{' ' * indent}+#{'-' * [(span_width - 2), 0].max}+"
  end

  lines.map { |s| s[0..@width] }.join("\n")
rescue StandardError => e
  puts e
  puts e.backtrace.join("\n")
  nil
end

Private Instance Methods

ms(micros) click to toggle source
# File lib/stackify_apm/util/inspector.rb, line 52
def ms(micros)
  micros.to_f / 1_000
end