Class: Librato::Rack::Tracker

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Tracker

Returns a new instance of Tracker.



14
15
16
17
18
# File 'lib/librato/rack/tracker.rb', line 14

def initialize(config)
  @config = config
  collector.prefix = config.prefix
  config.register_listener(collector)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



11
12
13
# File 'lib/librato/rack/tracker.rb', line 11

def config
  @config
end

#on_herokuObject

Returns the value of attribute on_heroku.



12
13
14
# File 'lib/librato/rack/tracker.rb', line 12

def on_heroku
  @on_heroku
end

Instance Method Details

#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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/librato/rack/tracker.rb', line 23

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

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

  config.deprecations.each { |d| deprecate(d) }
end

#collectorObject

primary collector object used by this tracker



39
40
41
# File 'lib/librato/rack/tracker.rb', line 39

def collector
  @collector ||= Librato::Collector.new(tags: tags)
end

#deprecate(message) ⇒ Object

log a deprecation message



44
45
46
# File 'lib/librato/rack/tracker.rb', line 44

def deprecate(message)
  log :warn, "DEPRECATION: #{message}"
end

#flushObject

send all current data to Metrics



49
50
51
52
53
54
55
56
57
58
# File 'lib/librato/rack/tracker.rb', line 49

def flush
  log :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?
  log(:trace) { "flushed pid #{@pid} in #{(Time.now - start)*1000.to_f}ms" }
rescue Exception => error
  log :error, "submission failed permanently: #{error}"
end

#queuedObject

current local instrumentation to be sent on next flush this is for debugging, don’t call rapidly in production as it may introduce latency



63
64
65
# File 'lib/librato/rack/tracker.rb', line 63

def queued
  build_flush_queue(collector, true).queued
end

#should_start?Boolean

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

Returns:

  • (Boolean)


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/librato/rack/tracker.rb', line 68

def should_start?
  if !config.user || !config.token
    # don't show this unless we're debugging, expected behavior
    log :debug, 'halting: credentials not present.'
  elsif config.autorun == false
    log :debug, 'halting: LIBRATO_AUTORUN disabled startup'
  elsif tags.any? { |k,v| k.to_s !~ ValidatingQueue::TAGS_KEY_REGEX || v.to_s !~ ValidatingQueue::TAGS_VALUE_REGEX }
    log :warn, "halting: '#{tags}' are invalid tags."
  elsif tags.keys.length > ValidatingQueue::DEFAULT_TAGS_LIMIT
    log :warn, "halting: cannot exceed default tags limit of #{ValidatingQueue::DEFAULT_TAGS_LIMIT} tag names per measurement."
  elsif on_heroku && !config.has_tags?
    log :warn, 'halting: tags must be provided in configuration.'
  else
    return true
  end
  false
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



90
91
92
# File 'lib/librato/rack/tracker.rb', line 90

def start!
  check_worker if should_start?
end

#suite_enabled?(suite) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/librato/rack/tracker.rb', line 99

def suite_enabled?(suite)
  config.metric_suites.include?(suite.to_sym)
end

#update_log_target(target) ⇒ Object

change output stream for logging



95
96
97
# File 'lib/librato/rack/tracker.rb', line 95

def update_log_target(target)
  logger.outlet = target
end