Class: MetOnTheMiddle::Tracker

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/met_on_the_middle/tracker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Tracker

Returns a new instance of Tracker.



9
10
11
# File 'lib/met_on_the_middle/tracker.rb', line 9

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/met_on_the_middle/tracker.rb', line 7

def config
  @config
end

Instance Method Details

#actual_requestObject



70
71
72
# File 'lib/met_on_the_middle/tracker.rb', line 70

def actual_request
  @_actual_request
end

#check_workerObject

check to see if we should start a worker for this process. if you are using this externally, use #start! instead as this method may change



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/met_on_the_middle/tracker.rb', line 16

def check_worker
  return if @worker # already running
  return unless start_worker?
  logger.debug {"config: #{config.dump}"}
  @pid = $$
  logger.debug {">> starting up worker for pid #{@pid}..."}

  @worker = Worker.new(timer: config.event_mode)
  @worker.run_periodically(config.flush_interval) do
    flush
  end

end

#collectorObject

primary collector object used by this tracker



31
32
33
# File 'lib/met_on_the_middle/tracker.rb', line 31

def collector
  @collector ||= MetOnTheMiddle::Collector.new(@config.sender)
end

#flushObject

send all current data to Metrics



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/met_on_the_middle/tracker.rb', line 37

def flush
  logger.debug {"flushing pid #{@pid} (#{Time.now}).."}
  start = Time.now
  # thread safety is handled internally for stores
  # queue = build_flush_queue(collector)
  # queue.submit unless queue.empty?
  unless collector.empty?
    logger.debug {"logica di invio dati - #{collector.inspect}"}

    collector.submit

    logger.debug { "#### #{collector.size} - GarbageCollector - #{GC.stat.inspect}" }

  end

  logger.info {"flushed pid #{@pid} in #{(Time.now - start)*1000.to_f}ms"}
rescue Exception => error
  logger.error {"submission failed permanently: #{error}"}
end

#request_blockObject

Si occupa di ragruppare per richiesta



76
77
78
79
80
# File 'lib/met_on_the_middle/tracker.rb', line 76

def request_block
  @_actual_request = Request.new
  yield
  collector.add @_actual_request
end

#should_start?Boolean

given current state, should the tracker start a reporter thread?

Returns:

  • (Boolean)


58
59
60
# File 'lib/met_on_the_middle/tracker.rb', line 58

def should_start?
  true
end

#start!Object

start worker thread, one per process. if this process has been forked from an one with an active worker thread we don’t need to worry about cleanup, the worker thread will not pass with the fork



66
67
68
# File 'lib/met_on_the_middle/tracker.rb', line 66

def start!
  check_worker if should_start?
end