class Librato::Rack::Tracker

Attributes

config[R]
on_heroku[RW]

Public Class Methods

new(config) click to toggle source
# File lib/librato/rack/tracker.rb, line 14
def initialize(config)
  @config = config
  collector.prefix = config.prefix
  config.register_listener(collector)
end

Public Instance Methods

check_worker() click to toggle source

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

# 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
collector() click to toggle source

primary collector object used by this tracker

# File lib/librato/rack/tracker.rb, line 39
def collector
  @collector ||= Librato::Collector.new(tags: tags)
end
deprecate(message) click to toggle source

log a deprecation message

# File lib/librato/rack/tracker.rb, line 44
def deprecate(message)
  log :warn, "DEPRECATION: #{message}"
end
flush() click to toggle source

send all current data to Metrics

# 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
queued() click to toggle source

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

# File lib/librato/rack/tracker.rb, line 63
def queued
  build_flush_queue(collector, true).queued
end
should_start?() click to toggle source

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

# 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!() click to toggle source

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

# File lib/librato/rack/tracker.rb, line 90
def start!
  check_worker if should_start?
end
suite_enabled?(suite) click to toggle source
# File lib/librato/rack/tracker.rb, line 99
def suite_enabled?(suite)
  config.metric_suites.include?(suite.to_sym)
end
update_log_target(target) click to toggle source

change output stream for logging

# File lib/librato/rack/tracker.rb, line 95
def update_log_target(target)
  logger.outlet = target
end

Private Instance Methods

build_flush_queue(collector, preserve=false) click to toggle source
# File lib/librato/rack/tracker.rb, line 122
def build_flush_queue(collector, preserve=false)
  queue = ValidatingQueue.new( client: client,
    prefix: config.prefix, skip_measurement_times: true )
  [collector.counters, collector.aggregate].each do |cache|
    cache.flush_to(queue, preserve: preserve)
  end
  if suite_enabled?(:rack)
    queue.add 'rack.processes' => { value: 1, tags: tags }
  end
  trace_queued(queue.queued) #if should_log?(:trace)
  queue
end
client() click to toggle source

access to client instance

# File lib/librato/rack/tracker.rb, line 106
def client
  @client ||= prepare_client
end
custom_adapter() click to toggle source

use custom faraday adapter if running in evented context

# File lib/librato/rack/tracker.rb, line 111
def custom_adapter
  case config.event_mode
  when :eventmachine
    :em_http
  when :synchrony
    :em_synchrony
  else
    nil
  end
end
logger() click to toggle source
# File lib/librato/rack/tracker.rb, line 141
def logger
  return @logger if @logger
  @logger = Logger.new(config.log_target)
  @logger.log_level = config.log_level
  @logger.prefix    = config.log_prefix
  @logger
end
prepare_client() click to toggle source
# File lib/librato/rack/tracker.rb, line 149
def prepare_client
  client = Librato::Metrics::Client.new
  client.authenticate config.user, config.token
  client.api_endpoint = config.api_endpoint
  client.proxy = config.proxy
  client.custom_user_agent = user_agent
  if custom_adapter
    client.faraday_adapter = custom_adapter
  end
  client
end
ruby_engine() click to toggle source
# File lib/librato/rack/tracker.rb, line 161
def ruby_engine
  return RUBY_ENGINE if Object.constants.include?(:RUBY_ENGINE)
  RUBY_DESCRIPTION.split[0]
end
start_worker?() click to toggle source

should we spin up a worker? wrap this in a process check so we only actually check once per process. this allows us to check again if the process forks.

# File lib/librato/rack/tracker.rb, line 173
def start_worker?
  if @pid_checked == $$
    false
  else
    @pid_checked = $$
    should_start?
  end
end
tags() click to toggle source
# File lib/librato/rack/tracker.rb, line 166
def tags
  @tags ||= config.has_tags? ? config.tags : { host: Socket.gethostname.downcase }
end
trace_queued(queued) click to toggle source

trace metrics being sent

# File lib/librato/rack/tracker.rb, line 136
def trace_queued(queued)
  require 'pp'
  log(:trace) { "Queued: " + queued.pretty_inspect }
end
user_agent() click to toggle source
# File lib/librato/rack/tracker.rb, line 182
def user_agent
  ua_chunks = [version_string]
  ua_chunks << "(#{ruby_engine}; #{RUBY_VERSION}p#{RUBY_PATCHLEVEL}; #{RUBY_PLATFORM})"
  ua_chunks.join(' ')
end
version_string() click to toggle source
# File lib/librato/rack/tracker.rb, line 188
def version_string
  "librato-rack/#{Librato::Rack::VERSION}"
end