Module: Method::Tracer

Defined in:
lib/method/tracer.rb

Defined Under Namespace

Modules: ClassMethods Classes: ConfigError

Class Method Summary collapse

Class Method Details

.active_spanObject



20
21
22
# File 'lib/method/tracer.rb', line 20

def active_span
  @active_span
end

.configure(tracer: OpenTracing.global_tracer, active_span: nil) ⇒ Object



24
25
26
27
# File 'lib/method/tracer.rb', line 24

def configure(tracer: OpenTracing.global_tracer, active_span: nil)
  @tracer = tracer
  @active_span = active_span
end

.extended(klazz) ⇒ Object



12
13
14
# File 'lib/method/tracer.rb', line 12

def extended(klazz)
  klazz.extend(ClassMethods)
end

.included(klazz) ⇒ Object



8
9
10
# File 'lib/method/tracer.rb', line 8

def included(klazz)
  klazz.extend(ClassMethods)
end

.method_tracerObject



16
17
18
# File 'lib/method/tracer.rb', line 16

def method_tracer
  @tracer || (raise ConfigError.new("Please configure the tracer using Method::Tracer.configure method"))
end

.trace(operation_name, tracer: method_tracer, **args, &block) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/method/tracer.rb', line 29

def trace(operation_name, tracer: method_tracer, **args,  &block)
  parent_span = args.include?(:child_of) ? args[:child_of] : active_span
  args[:child_of] = parent_span.respond_to?(:call) ? parent_span.call : parent_span

  current_span = tracer.start_span(operation_name, **args)

  yield current_span
rescue Exception => e
  if current_span
    current_span.set_tag('error', true)
    current_span.log(event: 'error', :'error.object' => e)
  end
  raise
ensure
  current_span.finish if current_span
end

.trace_method(klazz, method_name, **args, &block) ⇒ Object



46
47
48
# File 'lib/method/tracer.rb', line 46

def trace_method(klazz, method_name, **args, &block)
  trace("#{klazz.to_s}##{method_name.to_s}", **args, &block)
end