Class: Mnemosyne::Instrumenter
- Inherits:
-
Object
- Object
- Mnemosyne::Instrumenter
- Defined in:
- lib/mnemosyne/instrumenter.rb
Constant Summary collapse
- IDENT =
:__mnemosyne_current_trace- MUTEX =
Mutex.new
Class Attribute Summary collapse
-
.instance ⇒ Object
readonly
Returns the value of attribute instance.
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Class Method Summary collapse
- .current_trace ⇒ Object
- .logger ⇒ Object
- .start!(config = nil) ⇒ Object
- .trace(*args) ⇒ Object
- .with(instrumenter) ⇒ Object
Instance Method Summary collapse
- #current_trace ⇒ Object
- #current_trace=(trace) ⇒ Object
-
#initialize(config:, client:, logger:) ⇒ Instrumenter
constructor
A new instance of Instrumenter.
- #release(trace) ⇒ Object
- #submit(trace) ⇒ Object
- #trace(name, **kwargs) ⇒ Object
Constructor Details
#initialize(config:, client:, logger:) ⇒ Instrumenter
Returns a new instance of Instrumenter.
12 13 14 15 16 17 18 19 20 |
# File 'lib/mnemosyne/instrumenter.rb', line 12 def initialize(config:, client:, logger:) @client = client @config = config @logger = logger ::Mnemosyne::Probes.activate! logger.info 'Mnemosyne instrumenter started.' end |
Class Attribute Details
.instance ⇒ Object (readonly)
Returns the value of attribute instance.
61 62 63 |
# File 'lib/mnemosyne/instrumenter.rb', line 61 def instance @instance end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
10 11 12 |
# File 'lib/mnemosyne/instrumenter.rb', line 10 def logger @logger end |
Class Method Details
.current_trace ⇒ Object
108 109 110 111 |
# File 'lib/mnemosyne/instrumenter.rb', line 108 def current_trace return unless (instrumenter = instance) instrumenter.current_trace end |
.logger ⇒ Object
100 101 102 103 104 105 106 |
# File 'lib/mnemosyne/instrumenter.rb', line 100 def logger if (instrumenter = instance) instrumenter.logger else @logger ||= Logger.new($stdout) end end |
.start!(config = nil) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/mnemosyne/instrumenter.rb', line 63 def start!(config = nil) return @instance if @instance MUTEX.synchronize do return @instance if @instance client = Client.new(config) logger = config.logger @instance = new(config: config, client: client, logger: logger) end rescue => err = "Unable to start instrumenter: #{err}" if config && config.respond_to?(:logger) config.logger.warn else ::Kernel.warn end raise end |
.trace(*args) ⇒ Object
95 96 97 98 |
# File 'lib/mnemosyne/instrumenter.rb', line 95 def trace(*args) return unless (instrumenter = instance) instrumenter.trace(*args) end |
.with(instrumenter) ⇒ Object
86 87 88 89 90 91 92 93 |
# File 'lib/mnemosyne/instrumenter.rb', line 86 def with(instrumenter) old = instance @instance = instrumenter yield(instrumenter) ensure @instance = old end |
Instance Method Details
#current_trace ⇒ Object
22 23 24 |
# File 'lib/mnemosyne/instrumenter.rb', line 22 def current_trace Thread.current[IDENT] end |
#current_trace=(trace) ⇒ Object
26 27 28 |
# File 'lib/mnemosyne/instrumenter.rb', line 26 def current_trace=(trace) Thread.current[IDENT] = trace end |
#release(trace) ⇒ Object
54 55 56 57 58 |
# File 'lib/mnemosyne/instrumenter.rb', line 54 def release(trace) return unless current_trace.equal?(trace) self.current_trace = nil end |
#submit(trace) ⇒ Object
48 49 50 51 52 |
# File 'lib/mnemosyne/instrumenter.rb', line 48 def submit(trace) logger.debug { "Submit trace #{trace.uuid}" } @client.call trace end |
#trace(name, **kwargs) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/mnemosyne/instrumenter.rb', line 30 def trace(name, **kwargs) if (trace = current_trace) return yield trace if block_given? return trace end trace = self.current_trace = Trace.new(self, name, **kwargs) return trace unless block_given? begin yield trace ensure self.current_trace = nil trace.submit end end |