class BacktraceIO::Report

Attributes

agent[RW]
agentVersion[RW]
annotations[RW]
attributes[RW]
lang[RW]
langVersion[RW]
mainThread[RW]
sourceCode[RW]
threads[RW]
timestamp[RW]
uuid[RW]

Public Class Methods

new() click to toggle source
# File lib/backtraceio.rb, line 66
def initialize
    self.uuid = SecureRandom.uuid
    self.timestamp = Time.now.to_i
    self.sourceCode = {}

    self.threads = Thread.list.map do |t|
        processed = process_thread t
        [processed[:name], processed]
    end.to_h

    self.mainThread = "main"

    self.attributes = {}
    self.annotations = {}

    add_default_attributes

    self.lang = 'ruby'
    self.langVersion = RUBY_VERSION
    self.agent = 'backtrace-ruby'
    self.agentVersion = '0.1.0'
end

Public Instance Methods

add_default_attributes() click to toggle source
# File lib/backtraceio.rb, line 154
def add_default_attributes
    self.attributes['application'] = $0
    self.attributes['hostname'] = Socket.gethostname
end
add_exception_data(e) click to toggle source
# File lib/backtraceio.rb, line 147
def add_exception_data(e)
    t = Thread.current
    thread_name = name = t == Thread.main ? 'main' : t.object_id.to_s

    self.threads[thread_name][:stack] = make_thread_callstack e
end
get_source_code_for_location(bl) click to toggle source
# File lib/backtraceio.rb, line 101
def get_source_code_for_location(bl)
    lines = File.read(bl.path).each_line.to_a
    min = [bl.lineno-20, 0].max
    max = [bl.lineno+20, lines.size].min
    text = lines[min..max].join

    if lines.all?{ |l| l =~ /^\s*$/ }
        p text
        return nil
    end

    self.sourceCode[bl.object_id] = {
        text: text,
        startLine: min+1,
        startColumn: 1,
        startPos: 0,
        path: bl.path,
    }

    bl.object_id
end
make_thread_callstack(t) click to toggle source
# File lib/backtraceio.rb, line 123
def make_thread_callstack(t)
    t.backtrace_locations.map do |bl|
        data = {
            funcName: bl.base_label,
            line: bl.lineno.to_s,
            library: bl.path,
        }
        source = get_source_code_for_location bl
        data[:sourceCode] = source if source
        data
    end
end
process_thread(t) click to toggle source
# File lib/backtraceio.rb, line 136
def process_thread(t)
    name = t == Thread.main ? 'main' : t.object_id.to_s
    fault = Thread.current == t or t.status == nil

    {
        name: name,
        fault: fault,
        stack: make_thread_callstack(t),
    }
end
to_hash() click to toggle source
# File lib/backtraceio.rb, line 89
def to_hash
    fields = [
        :uuid, :timestamp, :lang, :langVersion, :agent, :agentVersion,
        :mainThread, :threads, :attributes, :annotations, :sourceCode
    ]
    fields.map{ |sym| [sym, self.send(sym)] }.to_h
end
to_json() click to toggle source
# File lib/backtraceio.rb, line 97
def to_json
    self.to_hash.to_json
end