Class: BacktraceIO::Report

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeReport

Returns a new instance of Report.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# 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

Instance Attribute Details

#agentObject

Returns the value of attribute agent.



62
63
64
# File 'lib/backtraceio.rb', line 62

def agent
  @agent
end

#agentVersionObject

Returns the value of attribute agentVersion.



63
64
65
# File 'lib/backtraceio.rb', line 63

def agentVersion
  @agentVersion
end

#annotationsObject

Returns the value of attribute annotations.



59
60
61
# File 'lib/backtraceio.rb', line 59

def annotations
  @annotations
end

#attributesObject

Returns the value of attribute attributes.



58
59
60
# File 'lib/backtraceio.rb', line 58

def attributes
  @attributes
end

#langObject

Returns the value of attribute lang.



60
61
62
# File 'lib/backtraceio.rb', line 60

def lang
  @lang
end

#langVersionObject

Returns the value of attribute langVersion.



61
62
63
# File 'lib/backtraceio.rb', line 61

def langVersion
  @langVersion
end

#mainThreadObject

Returns the value of attribute mainThread.



57
58
59
# File 'lib/backtraceio.rb', line 57

def mainThread
  @mainThread
end

#sourceCodeObject

Returns the value of attribute sourceCode.



64
65
66
# File 'lib/backtraceio.rb', line 64

def sourceCode
  @sourceCode
end

#threadsObject

Returns the value of attribute threads.



56
57
58
# File 'lib/backtraceio.rb', line 56

def threads
  @threads
end

#timestampObject

Returns the value of attribute timestamp.



55
56
57
# File 'lib/backtraceio.rb', line 55

def timestamp
  @timestamp
end

#uuidObject

Returns the value of attribute uuid.



54
55
56
# File 'lib/backtraceio.rb', line 54

def uuid
  @uuid
end

Instance Method Details

#add_default_attributesObject



154
155
156
157
# File 'lib/backtraceio.rb', line 154

def add_default_attributes
    self.attributes['application'] = $0
    self.attributes['hostname'] = Socket.gethostname
end

#add_exception_data(e) ⇒ Object



147
148
149
150
151
152
# 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) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# 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) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
# 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) ⇒ Object



136
137
138
139
140
141
142
143
144
145
# 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_hashObject



89
90
91
92
93
94
95
# 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_jsonObject



97
98
99
# File 'lib/backtraceio.rb', line 97

def to_json
    self.to_hash.to_json
end