module Traceable

Constants

VERSION

Public Class Methods

config() click to toggle source
# File lib/traceable/config.rb, line 31
def self.config
  @config ||= Config.new
end
configure() { |config| ... } click to toggle source
# File lib/traceable/config.rb, line 25
def self.configure(&_)
  yield config
  update_args_config
  config
end
included(base) click to toggle source
# File lib/traceable/class_methods.rb, line 4
def self.included(base)
  base.extend(ClassMethods)
end
update_args_config() click to toggle source
# File lib/traceable/config.rb, line 35
def self.update_args_config
  Args.set_config_limits(
    max_string_length: config.max_string_length,
    max_array_values: config.max_array_values,
    max_hash_keys: config.max_hash_keys
  )
end

Public Instance Methods

init_tracer(parent: nil, tags: nil) click to toggle source

Create the tracer instance used for generating log messages. If a parent is givent, tags and other settings will be inherited from it. If a parent is not given, it will automatically inherit from the next highest tracer in the call stack, if any.

The default set of tags includes a unique ID string, so all log messages generated from that tracer will have the same ID string. In combination with auto-inheriting from a parent tracer, this means that all tracing messages starting from some common root will have the same ID string to be able to group together related messages in the log.

# File lib/traceable.rb, line 51
def init_tracer(parent: nil, tags: nil)
  parent ||= Tracer.default_parent
  @tracer = Tracer.new(parent, tags: tags)
end
local_tracer() click to toggle source
# File lib/traceable.rb, line 36
def local_tracer
  @tracer ||= init_tracer
end
trace(msg = nil, **tags) { |trace_tags| ... } click to toggle source

Generate a log message When called without a block, generates a single log message:

trace "this is a single message"
trace.error "something bad happened"

When called with a block, the given message is used to compose a log output at the entry and exit of the block.

trace "doing something nifty" do
   do_something
end
# File lib/traceable.rb, line 24
def trace(msg = nil, **tags)
  tracer = local_tracer

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