Class: InfluxReporter::Transaction

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

Constant Summary collapse

ROOT_TRACE_NAME =
'transaction'

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.

Parameters:

  • client (InfluxReporter::Client)
  • endpoint (String)
  • kind (String) (defaults to: 'code.custom')
  • result (Integer) (defaults to: nil)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/influx_reporter/transaction.rb', line 13

def initialize(client, endpoint, kind = 'code.custom', result = nil)
  @client = client
  @config = client.config if client.respond_to?(:config)
  @endpoint = endpoint
  @kind = kind
  @result = result

  @timestamp = Util.nanos

  @running_traces = []
  @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

#configObject (readonly)

Returns the value of attribute config.



32
33
34
# File 'lib/influx_reporter/transaction.rb', line 32

def config
  @config
end

#durationObject

Returns the value of attribute duration.



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

def duration
  @duration
end

#endpointObject

Returns the value of attribute endpoint.



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

def endpoint
  @endpoint
end

#kindObject

Returns the value of attribute kind.



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

def kind
  @kind
end

#notificationsObject (readonly)

Returns the value of attribute notifications.



32
33
34
# File 'lib/influx_reporter/transaction.rb', line 32

def notifications
  @notifications
end

#resultObject

Returns the value of attribute result.



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

def result
  @result
end

#root_traceObject (readonly)

Returns the value of attribute root_trace.



32
33
34
# File 'lib/influx_reporter/transaction.rb', line 32

def root_trace
  @root_trace
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



32
33
34
# File 'lib/influx_reporter/transaction.rb', line 32

def start_time
  @start_time
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



32
33
34
# File 'lib/influx_reporter/transaction.rb', line 32

def timestamp
  @timestamp
end

#tracesObject (readonly)

Returns the value of attribute traces.



32
33
34
# File 'lib/influx_reporter/transaction.rb', line 32

def traces
  @traces
end

Instance Method Details

#_trace_started(trace) ⇒ Object



81
82
83
# File 'lib/influx_reporter/transaction.rb', line 81

def _trace_started(trace)
  @running_traces.push trace
end

#_trace_stopped(trace) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/influx_reporter/transaction.rb', line 85

def _trace_stopped(trace)
  if @running_traces.last == trace
    @running_traces.pop
  else
    @running_traces.delete trace
  end
end

#current_offsetObject



111
112
113
114
115
116
117
# File 'lib/influx_reporter/transaction.rb', line 111

def current_offset
  if curr = current_trace
    return curr.start_time
  end

  start_time
end

#current_traceObject



107
108
109
# File 'lib/influx_reporter/transaction.rb', line 107

def current_trace
  @running_traces.last
end

#done(result = nil) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/influx_reporter/transaction.rb', line 38

def done(result = nil)
  @result = result

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

  self
end

#done?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/influx_reporter/transaction.rb', line 47

def done?
  @root_trace.done?
end

#extra_tags {|| ... } ⇒ Object

Yields:

  • ()


93
94
95
96
# File 'lib/influx_reporter/transaction.rb', line 93

def extra_tags
  @root_trace.extra[:tags] ||= {}
  yield @root_trace.extra[:tags]
end

#extra_values {|| ... } ⇒ Object

Yields:

  • ()


98
99
100
101
# File 'lib/influx_reporter/transaction.rb', line 98

def extra_values
  @root_trace.extra[:values] ||= {}
  yield @root_trace.extra[:values]
end

#inspectObject



119
120
121
122
123
124
125
# File 'lib/influx_reporter/transaction.rb', line 119

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



34
35
36
# File 'lib/influx_reporter/transaction.rb', line 34

def release
  @client.current_transaction = nil
end

#running_tracesObject



103
104
105
# File 'lib/influx_reporter/transaction.rb', line 103

def running_traces
  @running_traces.clone
end

#submit(result = nil) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/influx_reporter/transaction.rb', line 51

def submit(result = nil)
  done result

  release

  @client.submit_transaction self

  self
end

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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/influx_reporter/transaction.rb', line 61

def trace(signature, kind = nil, extra = nil)
  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