Class: Traceable::Tracer
- Inherits:
-
Object
- Object
- Traceable::Tracer
- Defined in:
- lib/traceable/tracer.rb
Overview
rubocop:disable Metrics/ClassLength
Instance Attribute Summary collapse
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
Class Method Summary collapse
- .default_logger ⇒ Object
-
.default_parent ⇒ Object
rubocop:enable Metrics/AbcSize rubocop:enable Metrics/MethodLength.
- .default_tags ⇒ Object
-
.tracer_stack ⇒ Object
The tracer stack is a thread-local list of tracer instances, allowing the current tracer context to be automatically inherited by any other tracer instances created further down in the stack.
Instance Method Summary collapse
- #debug(msg, **tags) ⇒ Object
-
#do_block(msg, **tags, &_) ⇒ Object
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength.
- #emit(method, msg, **tags) ⇒ Object
- #emit_tags(method, tags) ⇒ Object
- #error(msg, **tags) ⇒ Object
- #fatal(msg, **tags) ⇒ Object
- #info(msg, **tags) ⇒ Object
-
#initialize(parent_tracer, tags: nil, logger: nil) ⇒ Tracer
constructor
A new instance of Tracer.
- #make_tags(**tags) ⇒ Object
- #warn(msg, **tags) ⇒ Object
Constructor Details
#initialize(parent_tracer, tags: nil, logger: nil) ⇒ Tracer
Returns a new instance of Tracer.
15 16 17 18 19 20 |
# File 'lib/traceable/tracer.rb', line 15 def initialize(parent_tracer, tags: nil, logger: nil) @parent = which_parent(parent_tracer) @logger = logger || (@parent ? @parent.logger : Tracer.default_logger) @tags = @parent ? @parent..dup : Tracer. @tags.merge!() if end |
Instance Attribute Details
#logger ⇒ Object
Returns the value of attribute logger.
11 12 13 |
# File 'lib/traceable/tracer.rb', line 11 def logger @logger end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
12 13 14 |
# File 'lib/traceable/tracer.rb', line 12 def parent @parent end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
13 14 15 |
# File 'lib/traceable/tracer.rb', line 13 def @tags end |
Class Method Details
.default_logger ⇒ Object
22 23 24 |
# File 'lib/traceable/tracer.rb', line 22 def self.default_logger Traceable.config.logger end |
.default_parent ⇒ Object
rubocop:enable Metrics/AbcSize rubocop:enable Metrics/MethodLength
108 109 110 |
# File 'lib/traceable/tracer.rb', line 108 def self.default_parent tracer_stack.last # nil if nothing currently on the stack end |
.default_tags ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/traceable/tracer.rb', line 26 def self. = Traceable.config..merge( trace: SecureRandom.uuid ) .each_key do |k| value = [k] [k] = value.call if value.respond_to?(:call) end end |
.tracer_stack ⇒ Object
The tracer stack is a thread-local list of tracer instances, allowing the current tracer context to be automatically inherited by any other tracer instances created further down in the stack.
The current tracer is pushed onto the stack when entering a traced block, then popped from the stack when leaving the traced block.
119 120 121 |
# File 'lib/traceable/tracer.rb', line 119 def self.tracer_stack Thread.current[:tracer_stack] ||= [] end |
Instance Method Details
#debug(msg, **tags) ⇒ Object
68 69 70 |
# File 'lib/traceable/tracer.rb', line 68 def debug(msg, **) emit :debug, msg, ** end |
#do_block(msg, **tags, &_) ⇒ Object
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/traceable/tracer.rb', line 74 def do_block(msg, **, &_) info "START: #{msg}", enter: true, ** block_start_time = Time.now.utc begin push yield rescue StandardError => ex elapsed = Time.now.utc - block_start_time if ex.instance_variable_defined?(:@traceable_rescued) origin = ex.instance_variable_get(:@traceable_rescued) = " [propagated from #{origin}]" else ex.instance_variable_set(:@traceable_rescued, msg) = ", #{ex.}" [:backtrace] ||= ex.backtrace.join('\n') end warn "EXCEPTION: #{msg} => #{ex.class.name}#{}", exception: true, elapsed: elapsed, class: ex.class.name, ** raise ensure pop unless ex elapsed = Time.now.utc - block_start_time info "END: #{msg}", exit: true, elapsed: elapsed, ** end end end |
#emit(method, msg, **tags) ⇒ Object
37 38 39 40 |
# File 'lib/traceable/tracer.rb', line 37 def emit(method, msg, **) = (message: msg, **) (method, ) end |
#emit_tags(method, tags) ⇒ Object
46 47 48 49 50 |
# File 'lib/traceable/tracer.rb', line 46 def (method, ) logger.send(method, ) rescue StandardError => ex warn "EXCEPTION in trace: #{ex}" end |
#error(msg, **tags) ⇒ Object
56 57 58 |
# File 'lib/traceable/tracer.rb', line 56 def error(msg, **) emit :error, msg, ** end |
#fatal(msg, **tags) ⇒ Object
52 53 54 |
# File 'lib/traceable/tracer.rb', line 52 def fatal(msg, **) emit :fatal, msg, ** end |
#info(msg, **tags) ⇒ Object
64 65 66 |
# File 'lib/traceable/tracer.rb', line 64 def info(msg, **) emit :info, msg, ** end |
#make_tags(**tags) ⇒ Object
42 43 44 |
# File 'lib/traceable/tracer.rb', line 42 def (**) @tags.merge() end |
#warn(msg, **tags) ⇒ Object
60 61 62 |
# File 'lib/traceable/tracer.rb', line 60 def warn(msg, **) emit :warn, msg, ** end |