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
Instance Method Summary collapse
- #current_trace ⇒ Object
- #current_trace=(trace) ⇒ Object
-
#initialize(config) ⇒ Instrumenter
constructor
A new instance of Instrumenter.
- #release(trace) ⇒ Object
- #submit(trace) ⇒ Object
-
#trace(name, **kwargs) ⇒ Object
rubocop:disable Metrics/MethodLength.
Constructor Details
#initialize(config) ⇒ Instrumenter
Returns a new instance of Instrumenter.
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/mnemosyne/instrumenter.rb', line 11 def initialize(config) @config = config raise 'Config required!' unless @config @logger = config.logger @client = Client.new(config) logger.info 'Mnemosyne instrumenter started.' end |
Class Attribute Details
.instance ⇒ Object (readonly)
Returns the value of attribute instance.
71 72 73 |
# File 'lib/mnemosyne/instrumenter.rb', line 71 def instance @instance end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
9 10 11 |
# File 'lib/mnemosyne/instrumenter.rb', line 9 def logger @logger end |
Class Method Details
.current_trace ⇒ Object
106 107 108 109 |
# File 'lib/mnemosyne/instrumenter.rb', line 106 def current_trace return unless (instrumenter = instance) instrumenter.current_trace end |
.logger ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/mnemosyne/instrumenter.rb', line 98 def logger if (instrumenter = instance) instrumenter.logger else @logger ||= Logger.new($stdout) end end |
.start!(config = nil) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/mnemosyne/instrumenter.rb', line 73 def start!(config = nil) return @instance if @instance MUTEX.synchronize do return @instance if @instance @instance = new(config) 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
93 94 95 96 |
# File 'lib/mnemosyne/instrumenter.rb', line 93 def trace(*args) return unless (instrumenter = instance) instrumenter.trace(*args) 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
64 65 66 67 68 |
# File 'lib/mnemosyne/instrumenter.rb', line 64 def release(trace) return unless current_trace.equal?(trace) self.current_trace = nil end |
#submit(trace) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/mnemosyne/instrumenter.rb', line 49 def submit(trace) blob = { hostname: @config.hostname, platform: @config.platform, application: @config.application, } # TODO: nest blob.merge! trace.serialize logger.debug { "Submit trace #{trace.uuid}" } @client.send 'mnemosyne.trace', blob end |
#trace(name, **kwargs) ⇒ Object
rubocop:disable Metrics/MethodLength
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/mnemosyne/instrumenter.rb', line 31 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 |