Module: Traceable

Defined in:
lib/traceable.rb,
lib/traceable/args.rb,
lib/traceable/config.rb,
lib/traceable/tracer.rb,
lib/traceable/version.rb,
lib/traceable/class_methods.rb

Defined Under Namespace

Modules: ClassMethods Classes: Args, Config, Tracer

Constant Summary collapse

VERSION =
'1.3.1'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.configObject



22
23
24
# File 'lib/traceable/config.rb', line 22

def self.config
  @config ||= Config.new
end

.configure {|config| ... } ⇒ Object

Yields:



18
19
20
# File 'lib/traceable/config.rb', line 18

def self.configure(&_)
  yield config
end

.included(base) ⇒ Object



4
5
6
# File 'lib/traceable/class_methods.rb', line 4

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#init_tracer(parent: nil, tags: nil) ⇒ Object

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.



51
52
53
54
# 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_tracerObject



36
37
38
# File 'lib/traceable.rb', line 36

def local_tracer
  @tracer ||= init_tracer
end

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

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


24
25
26
27
28
29
30
31
32
33
34
# 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