Class: Startback::Audit::Tracer

Inherits:
Object
  • Object
show all
Defined in:
lib/startback/audit/tracer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stack = [], listeners = [], redactor = default_redactor) ⇒ Tracer

Returns a new instance of Tracer.



8
9
10
11
12
# File 'lib/startback/audit/tracer.rb', line 8

def initialize(stack = [], listeners = [], redactor = default_redactor)
  @stack = stack
  @listeners = listeners
  @redactor = redactor
end

Instance Attribute Details

#stackObject (readonly)

Returns the value of attribute stack.



13
14
15
# File 'lib/startback/audit/tracer.rb', line 13

def stack
  @stack
end

Class Method Details

.emptyObject



15
16
17
# File 'lib/startback/audit/tracer.rb', line 15

def self.empty
  Tracer.new
end

Instance Method Details

#attach_to(trace_id, span_id, attributes = {}, parent_id = nil) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/startback/audit/tracer.rb', line 35

def attach_to(trace_id, span_id, attributes = {}, parent_id = nil)
  error!("Trace already attached") if attached?

  initial_span = Span.new(trace_id, parent_id, attributes, span_id)
  initial_stack = [ initial_span ]
  Tracer.new(initial_stack, @listeners, @redactor)
end

#attached?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/startback/audit/tracer.rb', line 19

def attached?
  !@stack.empty?
end

#fork(attributes = {}, &block) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/startback/audit/tracer.rb', line 43

def fork(attributes = {}, &block)
  attributes = @redactor.redact(attributes)
  span = last_span!.fork(attributes)
  @stack << span
  propagate_to_listeners(span)
  result, error = nil, nil
  timing = Benchmark.measure do
    result, error = exec_block_with_error_handling(block)
  end
  error ? raise(error) : result
ensure
  unless stack.empty?
    span = @stack.pop.finish(timing, error)
    propagate_to_listeners(span)
  end
end

#last_span!Object



23
24
25
26
27
# File 'lib/startback/audit/tracer.rb', line 23

def last_span!
  error!("Trace not attached") unless attached?

  @stack.last
end

#new_trace(attributes = {}) ⇒ Object



29
30
31
32
33
# File 'lib/startback/audit/tracer.rb', line 29

def new_trace(attributes = {})
  error!("Trace already attached") if attached?

  attach_to(SecureRandom.uuid, SecureRandom.uuid, attributes)
end

#on_span(listener = nil, &block) ⇒ Object



60
61
62
63
# File 'lib/startback/audit/tracer.rb', line 60

def on_span(listener = nil, &block)
  @listeners << (listener || block)
  self
end