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
61
62
63
64
65
66
67
68
69
# 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) }
      begin
        arg1 = args.first
        # method has keyword argument signature (or single-hash positional argument)
        if arg1.is_a?(Hash) && args.count == 1
          # add span as a kwarg
          span_block&.send(:call, **arg1.merge(span: span))
          super(**arg1, &block)
        else
          # method has positional argument signature, so just add span to front
          span_block&.send(:call, *([span] + args))
          super(*args, &block)
        end
      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