Class: Opbeat::Transaction

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

Constant Summary collapse

ROOT_TRACE_NAME =
'transaction'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, endpoint, kind = 'code.custom', result = nil) ⇒ Transaction

Returns a new instance of Transaction.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/opbeat/transaction.rb', line 8

def initialize client, endpoint, kind = 'code.custom', result = nil
  @client = client
  @endpoint = endpoint
  @kind = kind
  @result = result

  @timestamp = Util.nearest_minute.to_i

  @root_trace = Trace.new(self, ROOT_TRACE_NAME, ROOT_TRACE_NAME)
  @traces = [@root_trace]
  @notifications = []

  @start_time = Util.nanos
  @root_trace.start @start_time
end

Instance Attribute Details

#durationObject

Returns the value of attribute duration.



24
25
26
# File 'lib/opbeat/transaction.rb', line 24

def duration
  @duration
end

#endpointObject

Returns the value of attribute endpoint.



24
25
26
# File 'lib/opbeat/transaction.rb', line 24

def endpoint
  @endpoint
end

#kindObject

Returns the value of attribute kind.



24
25
26
# File 'lib/opbeat/transaction.rb', line 24

def kind
  @kind
end

#notificationsObject (readonly)

Returns the value of attribute notifications.



25
26
27
# File 'lib/opbeat/transaction.rb', line 25

def notifications
  @notifications
end

#resultObject

Returns the value of attribute result.



24
25
26
# File 'lib/opbeat/transaction.rb', line 24

def result
  @result
end

#root_traceObject (readonly)

Returns the value of attribute root_trace.



25
26
27
# File 'lib/opbeat/transaction.rb', line 25

def root_trace
  @root_trace
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



25
26
27
# File 'lib/opbeat/transaction.rb', line 25

def start_time
  @start_time
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



25
26
27
# File 'lib/opbeat/transaction.rb', line 25

def timestamp
  @timestamp
end

#tracesObject (readonly)

Returns the value of attribute traces.



25
26
27
# File 'lib/opbeat/transaction.rb', line 25

def traces
  @traces
end

Instance Method Details

#current_offsetObject



82
83
84
85
86
87
88
# File 'lib/opbeat/transaction.rb', line 82

def current_offset
  if curr = current_trace
    return curr.start_time
  end

  start_time
end

#current_traceObject



78
79
80
# File 'lib/opbeat/transaction.rb', line 78

def current_trace
  traces.reverse.find(&:running?)
end

#done(result = nil) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/opbeat/transaction.rb', line 31

def done result = nil
  @result = result

  @root_trace.done Util.nanos
  @duration = @root_trace.duration

  self
end

#done?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/opbeat/transaction.rb', line 40

def done?
  @root_trace.done?
end

#inspectObject



90
91
92
93
94
95
96
# File 'lib/opbeat/transaction.rb', line 90

def inspect
  info = %w{endpoint kind result duration timestamp start_time}
  <<-TEXT
<Transaction #{info.map { |m| "#{m}:#{send(m).inspect}" }.join(' ')}>
  #{traces.map(&:inspect).join("\n  ")}"
  TEXT
end

#releaseObject



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

def release
  @client.current_transaction = nil
end

#running_tracesObject



74
75
76
# File 'lib/opbeat/transaction.rb', line 74

def running_traces
  traces.select(&:running?)
end

#submit(result = nil) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/opbeat/transaction.rb', line 44

def submit result = nil
  done result

  release

  @client.submit_transaction self

  self
end

#trace(signature, kind = nil, extra = nil, &block) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/opbeat/transaction.rb', line 54

def trace signature, kind = nil, extra = nil, &block
  trace = Trace.new(self, signature, kind, running_traces, extra)

  rel_time = current_offset

  traces << trace

  trace.start rel_time

  return trace unless block_given?

  begin
    result = yield trace
  ensure
    trace.done
  end

  result
end