Class: Honeybadger::Trace
- Inherits:
-
Object
- Object
- Honeybadger::Trace
- Defined in:
- lib/honeybadger/trace.rb
Instance Attribute Summary collapse
-
#duration ⇒ Object
readonly
Returns the value of attribute duration.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#meta ⇒ Object
readonly
Private helpers: use at your own risk.
Class Method Summary collapse
- .create(id) ⇒ Object
- .current ⇒ Object
-
.ignore_events ⇒ Object
Internal: Disables event tracing for executed code block.
-
.ignoring_events? ⇒ Boolean
Internal: Is event tracing currently disabled?.
- .instrument(key, payload = {}, &block) ⇒ Object
Instance Method Summary collapse
- #add(event) ⇒ Object
- #add_query(event) ⇒ Object
- #complete(event, payload = {}) ⇒ Object
-
#initialize(id) ⇒ Trace
constructor
A new instance of Trace.
- #instrument(key, payload) ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(id) ⇒ Trace
Returns a new instance of Trace.
45 46 47 48 49 50 51 |
# File 'lib/honeybadger/trace.rb', line 45 def initialize(id) @id = id @events = [] @meta = {} @fast_queries = {} @duration = 0 end |
Instance Attribute Details
#duration ⇒ Object (readonly)
Returns the value of attribute duration.
7 8 9 |
# File 'lib/honeybadger/trace.rb', line 7 def duration @duration end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
7 8 9 |
# File 'lib/honeybadger/trace.rb', line 7 def id @id end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
7 8 9 |
# File 'lib/honeybadger/trace.rb', line 7 def key @key end |
#meta ⇒ Object (readonly)
Private helpers: use at your own risk.
101 102 103 |
# File 'lib/honeybadger/trace.rb', line 101 def @meta end |
Class Method Details
.create(id) ⇒ Object
13 14 15 |
# File 'lib/honeybadger/trace.rb', line 13 def self.create(id) Thread.current[:__hb_trace] = new(id) end |
.current ⇒ Object
9 10 11 |
# File 'lib/honeybadger/trace.rb', line 9 def self.current Thread.current[:__hb_trace] end |
.ignore_events ⇒ Object
Internal: Disables event tracing for executed code block.
block - The code which should not be traced.
Returns the return value from the block.
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/honeybadger/trace.rb', line 29 def self.ignore_events return yield if ignoring_events? begin Thread.current[:__hb_ignore_trace_events] = true yield ensure Thread.current[:__hb_ignore_trace_events] = false end end |
.ignoring_events? ⇒ Boolean
Internal: Is event tracing currently disabled?
41 42 43 |
# File 'lib/honeybadger/trace.rb', line 41 def self.ignoring_events? !!Thread.current[:__hb_ignore_trace_events] end |
.instrument(key, payload = {}, &block) ⇒ Object
17 18 19 20 21 22 |
# File 'lib/honeybadger/trace.rb', line 17 def self.instrument(key, payload = {}, &block) current = self.current self.create(SecureRandom.uuid).instrument(key, payload, &block) ensure Thread.current[:__hb_trace] = current end |
Instance Method Details
#add(event) ⇒ Object
53 54 55 56 57 |
# File 'lib/honeybadger/trace.rb', line 53 def add(event) return if ignoring_events? ce = clean_event(event) @events << ce.to_a if ce.render? end |
#add_query(event) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/honeybadger/trace.rb', line 59 def add_query(event) return if ignoring_events? return add(event) unless event.duration < 6 ce = clean_event(event) return unless ce.render? query = ce.to_s if @fast_queries[query] @fast_queries[query][:duration] += ce.event.duration @fast_queries[query][:count] += 1 else @fast_queries[query] = { :duration => ce.event.duration, :count => 1 } end end |
#complete(event, payload = {}) ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/honeybadger/trace.rb', line 74 def complete(event, payload = {}) @meta = clean_event(event).to_h.merge(payload) @duration = event.duration @key = "#{event.payload[:controller]}##{event.payload[:action]}" Thread.current[:__hb_trace] = nil Agent.trace(self) end |
#instrument(key, payload) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/honeybadger/trace.rb', line 82 def instrument(key, payload) @key = key @meta = payload started = Time.now yield rescue Exception => e @meta[:exception] = [e.class.name, e.] raise e ensure @meta.merge!(:duration => @duration = 1000.0 * (Time.now - started)) Agent.trace(self) end |
#to_h ⇒ Object
95 96 97 |
# File 'lib/honeybadger/trace.rb', line 95 def to_h @meta.merge({ :events => @events, :key => @key, :fast_queries => @fast_queries.map {|k,v| [ k, v[:duration], v[:count] ] } }) end |