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) = @parent ? @parent..dup : Tracer. .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 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
110 111 112 |
# File 'lib/traceable/tracer.rb', line 110 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.
121 122 123 |
# File 'lib/traceable/tracer.rb', line 121 def self.tracer_stack Thread.current[:tracer_stack] ||= [] end |
Instance Method Details
#debug(msg, **tags) ⇒ Object
70 71 72 |
# File 'lib/traceable/tracer.rb', line 70 def debug(msg, **) emit :debug, msg, ** end |
#do_block(msg, **tags, &_) ⇒ Object
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength
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 105 106 |
# File 'lib/traceable/tracer.rb', line 76 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.message}" [:backtrace] ||= ex.backtrace.join('\n') end warn "EXCEPTION: #{msg} => #{ex.class.name}#{ex_message}", 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 51 52 |
# File 'lib/traceable/tracer.rb', line 46 def (method, ) logger.send(method, ) nil rescue StandardError => ex warn "EXCEPTION in trace: #{ex}" nil end |
#error(msg, **tags) ⇒ Object
58 59 60 |
# File 'lib/traceable/tracer.rb', line 58 def error(msg, **) emit :error, msg, ** end |
#fatal(msg, **tags) ⇒ Object
54 55 56 |
# File 'lib/traceable/tracer.rb', line 54 def fatal(msg, **) emit :fatal, msg, ** end |
#info(msg, **tags) ⇒ Object
66 67 68 |
# File 'lib/traceable/tracer.rb', line 66 def info(msg, **) emit :info, msg, ** end |
#make_tags(**tags) ⇒ Object
42 43 44 |
# File 'lib/traceable/tracer.rb', line 42 def (**) .merge() end |
#warn(msg, **tags) ⇒ Object
62 63 64 |
# File 'lib/traceable/tracer.rb', line 62 def warn(msg, **) emit :warn, msg, ** end |