Class: Justlogging::Transaction

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, env) ⇒ Transaction

Returns a new instance of Transaction.



15
16
17
18
19
20
21
# File 'lib/justlogging/transaction.rb', line 15

def initialize(id, env)
  @id = id
  @events = []
  @log_entry = nil
  @exception = nil
  @env = env
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



13
14
15
# File 'lib/justlogging/transaction.rb', line 13

def env
  @env
end

#eventsObject (readonly)

Returns the value of attribute events.



13
14
15
# File 'lib/justlogging/transaction.rb', line 13

def events
  @events
end

#exceptionObject (readonly)

Returns the value of attribute exception.



13
14
15
# File 'lib/justlogging/transaction.rb', line 13

def exception
  @exception
end

#idObject (readonly)

Returns the value of attribute id.



13
14
15
# File 'lib/justlogging/transaction.rb', line 13

def id
  @id
end

#log_entryObject (readonly)

Returns the value of attribute log_entry.



13
14
15
# File 'lib/justlogging/transaction.rb', line 13

def log_entry
  @log_entry
end

Class Method Details

.create(key, env) ⇒ Object



4
5
6
7
# File 'lib/justlogging/transaction.rb', line 4

def self.create(key, env)
  Thread.current[:justlogging_transaction_id] = key
  Justlogging.transactions[key] = Justlogging::Transaction.new(key, env)
end

.currentObject



9
10
11
# File 'lib/justlogging/transaction.rb', line 9

def self.current
  Justlogging.transactions[Thread.current[:justlogging_transaction_id]]
end

Instance Method Details

#add_event(event) ⇒ Object



31
32
33
# File 'lib/justlogging/transaction.rb', line 31

def add_event(event)
  @events << event
end

#add_exception(ex) ⇒ Object



35
36
37
# File 'lib/justlogging/transaction.rb', line 35

def add_exception(ex)
  @exception = ex
end

#complete!Object



101
102
103
104
105
106
107
# File 'lib/justlogging/transaction.rb', line 101

def complete!
  Thread.current[:justlogging_transaction_id] = nil
  current_transaction = Justlogging.transactions.delete(@id)
  if @events.any? || exception?
    Justlogging.agent.add_to_queue(current_transaction.to_hash)
  end
end

#exception?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/justlogging/transaction.rb', line 39

def exception?
  !! @exception
end

#formatted_eventsObject



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/justlogging/transaction.rb', line 52

def formatted_events
  @events.map do |event|
    {
      :name => event.name,
      :duration => event.duration,
      :time => event.time,
      :end => event.end,
      :payload => event.payload
    }
  end
end

#formatted_exceptionObject



43
44
45
46
47
48
49
50
# File 'lib/justlogging/transaction.rb', line 43

def formatted_exception
  return {} unless exception?
  {
    :backtrace => @exception.backtrace,
    :exception => @exception.name,
    :message => @exception.message
  }
end

#formatted_log_entryObject



64
65
66
67
68
69
70
71
# File 'lib/justlogging/transaction.rb', line 64

def formatted_log_entry
  {
    :name => request.fullpath,
    :environment => Rails.env,
    :server => @env['SERVER_NAME'],
    :kind => 'http_request'
  }.merge(formatted_payload)
end

#formatted_payloadObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/justlogging/transaction.rb', line 73

def formatted_payload
  if @log_entry
    {
      :duration => @log_entry.duration,
      :time => @log_entry.time,
      :end => @log_entry.end
    }.merge(@log_entry.payload).tap do |o|
      o[:action] = "#{o.delete(:controller)}##{o[:action]}"
    end
  else
    if exception?
      {:action => @exception.inspect.gsub(/^<#(.*)>$/, '\1')}
    else
      {}
    end
  end
end

#requestObject



23
24
25
# File 'lib/justlogging/transaction.rb', line 23

def request
  ActionDispatch::Request.new(@env)
end

#set_log_entry(event) ⇒ Object



27
28
29
# File 'lib/justlogging/transaction.rb', line 27

def set_log_entry(event)
  @log_entry = event
end

#to_hashObject



91
92
93
94
95
96
97
98
99
# File 'lib/justlogging/transaction.rb', line 91

def to_hash
  {
    :request_id => @id,
    :log_entry => formatted_log_entry,
    :events => formatted_events,
    :exception => formatted_exception,
    :failed => exception.present?
  }
end