Class: Honeybadger::Trace

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#durationObject (readonly)

Returns the value of attribute duration.



7
8
9
# File 'lib/honeybadger/trace.rb', line 7

def duration
  @duration
end

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/honeybadger/trace.rb', line 7

def id
  @id
end

#keyObject (readonly)

Returns the value of attribute key.



7
8
9
# File 'lib/honeybadger/trace.rb', line 7

def key
  @key
end

#metaObject (readonly)

Private helpers: use at your own risk.



101
102
103
# File 'lib/honeybadger/trace.rb', line 101

def meta
  @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

.currentObject



9
10
11
# File 'lib/honeybadger/trace.rb', line 9

def self.current
  Thread.current[:__hb_trace]
end

.ignore_eventsObject

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?

Returns:

  • (Boolean)


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.message]
  raise e
ensure
  @meta.merge!(:duration => @duration = 1000.0 * (Time.now - started))
  Agent.trace(self)
end

#to_hObject



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