Module: Traceable::ClassMethods

Defined in:
lib/traceable/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#trace(msg = nil, **tags) ⇒ Object

For the (relatively rare?) case of calling trace() in a class method of a Traceable class - creates a new Tracer instance



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/traceable/class_methods.rb', line 54

def trace(msg = nil, **tags)
  tracer = Tracer.new(Tracer.default_parent)

  if block_given?
    tracer.do_block(msg, **tags) { |trace_tags| yield trace_tags }
  elsif msg
    tracer.info msg, **tags
  else
    tracer
  end
end

#traced_method(method_name, include_args: true) ⇒ Object

Put an automatic ‘#trace’ call around the specified method, so that entry and exit from the method will be automatically logged.

Can be used when defining the method:

class X
  include SISApp::Traceable
  traced_method def foo
    ...
  end
end

or it can be called after defining the method:

class X
  include SISApp::Traceable
  def foo
    ...
  end
  traced_method :foo
 end

The generated log message(s) will include the class name and method name



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/traceable/class_methods.rb', line 32

def traced_method(method_name, include_args: true)
  trace_name = "#{name}##{method_name}"
  orig_method = instance_method(method_name)
  params = orig_method.parameters
  include_args = false if params.empty?

  send(:define_method, method_name) do |*args, &block|
    trace_tags = include_args ? Traceable::Args.args_to_tags(params, args) : {}
    trace(trace_name, **trace_tags) do
      if block
        orig_method.bind(self).call(*args) { |*block_args| block.call(*block_args) }
      else
        orig_method.bind(self).call(*args)
      end
    end
  end

  method_name
end