Class: Mnemosyne::Instrumenter

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/mnemosyne/instrumenter.rb

Constant Summary collapse

IDENT =
:__mnemosyne_current_trace
MUTEX =
Mutex.new

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger

Constructor Details

#initialize(config:, client:) ⇒ Instrumenter

Returns a new instance of Instrumenter.



12
13
14
15
16
17
18
19
# File 'lib/mnemosyne/instrumenter.rb', line 12

def initialize(config:, client:)
  @client = client
  @config = config

  ::Mnemosyne::Probes.activate!

  logger.debug(Mnemosyne) { 'Instrumenter started' }
end

Class Attribute Details

.instance ⇒ Object (readonly)

Returns the value of attribute instance.



60
61
62
# File 'lib/mnemosyne/instrumenter.rb', line 60

def instance
  @instance
end

Class Method Details

.current_trace ⇒ Object



94
95
96
97
# File 'lib/mnemosyne/instrumenter.rb', line 94

def current_trace
  return unless (instrumenter = instance)
  instrumenter.current_trace
end

.start!(config = nil) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/mnemosyne/instrumenter.rb', line 62

def start!(config = nil)
  return @instance if @instance

  MUTEX.synchronize do
    return @instance if @instance

    client = Client.new(config)

    @instance = new(config: config, client: client)
  end
rescue => err
  ::Mnemosyne::Logging.logger.warn(Mnemosyne) do
    "Unable to start instrumenter: #{err}"
  end

  raise
end

.trace(*args) ⇒ Object



89
90
91
92
# File 'lib/mnemosyne/instrumenter.rb', line 89

def trace(*args)
  return unless (instrumenter = instance)
  instrumenter.trace(*args)
end

.with(instrumenter) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/mnemosyne/instrumenter.rb', line 80

def with(instrumenter)
  old = instance
  @instance = instrumenter

  yield(instrumenter)
ensure
  @instance = old
end

Instance Method Details

#current_trace ⇒ Object



21
22
23
# File 'lib/mnemosyne/instrumenter.rb', line 21

def current_trace
  Thread.current[IDENT]
end

#current_trace=(trace) ⇒ Object



25
26
27
# File 'lib/mnemosyne/instrumenter.rb', line 25

def current_trace=(trace)
  Thread.current[IDENT] = trace
end

#release(trace) ⇒ Object



53
54
55
56
57
# File 'lib/mnemosyne/instrumenter.rb', line 53

def release(trace)
  return unless current_trace.equal?(trace)

  self.current_trace = nil
end

#submit(trace) ⇒ Object



47
48
49
50
51
# File 'lib/mnemosyne/instrumenter.rb', line 47

def submit(trace)
  logger.debug(Mnemosyne) { "Submit trace #{trace.uuid}" }

  @client.call trace
end

#trace(name, **kwargs) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/mnemosyne/instrumenter.rb', line 29

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