Module: Bigcommerce::Lightstep::Traceable::ClassMethods

Defined in:
lib/bigcommerce/lightstep/traceable.rb

Overview

Extend the class with the tracing methods.

Instance Method Summary collapse

Instance Method Details

#trace(method_name, operation_name, tags: nil, &span_block) ⇒ Object

Trace the perform method for the command with the given operation name as the span name

Parameters:

  • method_name (Symbol)

    The method to trace

  • operation_name (String)

    The name to give the span

  • tags (Hash) (defaults to: nil)

    A key/value hash of tags to set on the created span

  • span_block (Proc)

    A block to yield before calling perform; useful for setting tags on the outer span



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/bigcommerce/lightstep/traceable.rb', line 41

def trace(method_name, operation_name, tags: nil, &span_block)
  method_name = method_name.to_sym
  mod = Module.new
  mod.define_method(method_name) do |args, &block|
    tracer = ::Bigcommerce::Lightstep::Tracer.instance
    tracer.start_span(operation_name) do |span|
      tags&.each { |k, v| span.set_tag(k.to_s, v) }
      span_block&.send(:call, **args.merge(span: span))
      begin
        super(**args, &block)
      rescue StandardError => e
        span.set_tag('error', true)
        span.set_tag('error.message', e.message)
        span.set_tag('error.class', e.class.to_s)
        raise
      end
    end
  end
  prepend(mod)
end