class Librato::Rack::Configuration

Holds configuration for Librato::Rack middleware to use. Acquires some settings by default from environment variables, but this allows easy setting and overrides.

@example

config = Librato::Rack::Configuration.new
config.user  = 'mimo@librato.com'
config.token = 'mytoken'

Constants

DEFAULT_SUITES
EVENT_MODES

Attributes

api_endpoint[RW]
autorun[RW]
deprecations[R]
disable_rack_metrics[RW]
flush_interval[RW]
log_level[RW]
log_prefix[RW]
log_target[RW]
prefix[R]
proxy[RW]
suites[RW]
tags[RW]
token[RW]
tracker[RW]
user[RW]

Public Class Methods

new() click to toggle source
# File lib/librato/rack/configuration.rb, line 23
def initialize
  # set up defaults
  self.tracker = nil
  self.api_endpoint = Librato::Metrics.api_endpoint
  self.flush_interval = 60
  self.log_prefix = '[librato-rack] '
  @listeners = []
  @deprecations = []

  load_configuration
end

Public Instance Methods

dump() click to toggle source
# File lib/librato/rack/configuration.rb, line 79
def dump
  fields = {}
  %w{flush_interval log_level prefix suites tags token user}.each do |field|
    fields[field.to_sym] = self.send(field)
  end
  fields[:metric_suites] = metric_suites.fields
  fields
end
event_mode() click to toggle source
# File lib/librato/rack/configuration.rb, line 35
def event_mode
  @event_mode
end
event_mode=(mode) click to toggle source

set event_mode, valid options are EVENT_MODES or nil (the default) if not running in an evented context

# File lib/librato/rack/configuration.rb, line 41
def event_mode=(mode)
  mode = mode.to_sym if mode
  # reject unless acceptable mode, allow for turning event_mode off
  if [*EVENT_MODES, nil].include?(mode)
    @event_mode = mode
  else
    # TODO log warning
  end
end
has_tags?() click to toggle source
# File lib/librato/rack/configuration.rb, line 51
def has_tags?
  @tags && !@tags.empty?
end
load_configuration() click to toggle source

check environment variables and capture current state for configuration

# File lib/librato/rack/configuration.rb, line 57
def load_configuration
  self.user = ENV['LIBRATO_USER']
  self.token = ENV['LIBRATO_TOKEN']
  self.autorun = detect_autorun
  self.prefix = ENV['LIBRATO_PREFIX']
  self.tags = build_tags
  self.log_level = ENV['LIBRATO_LOG_LEVEL'] || :info
  self.proxy = ENV['LIBRATO_PROXY'] || ENV['https_proxy'] || ENV['http_proxy']
  self.event_mode = ENV['LIBRATO_EVENT_MODE']
  self.suites = ENV['LIBRATO_SUITES'] || ''
  check_deprecations
end
metric_suites() click to toggle source
# File lib/librato/rack/configuration.rb, line 88
def metric_suites
  @metric_suites ||= case suites.downcase.strip
                     when 'all'
                       SuitesAll.new
                     when 'none'
                       SuitesNone.new
                     else
                       Suites.new(suites, default_suites)
                     end
end
prefix=(prefix) click to toggle source
# File lib/librato/rack/configuration.rb, line 70
def prefix=(prefix)
  @prefix = prefix
  @listeners.each { |l| l.prefix = prefix }
end
register_listener(listener) click to toggle source
# File lib/librato/rack/configuration.rb, line 75
def register_listener(listener)
  @listeners << listener
end

Private Instance Methods

build_tags() click to toggle source
# File lib/librato/rack/configuration.rb, line 126
def build_tags
  tags = {}
  tags.tap do
    if ENV["LIBRATO_TAGS"]
      ENV["LIBRATO_TAGS"].split(",")
        .map { |pairs| pairs.split("=") }
          .map { |k,v| tags[k.to_sym] = v }

      if tags.all? {|k,v| k.nil? || v.nil? }
        raise InvalidTagConfiguration, "Invalid tag configuration format. Example: foo=bar,baz=qux"
      end
    end
  end
end
check_deprecations() click to toggle source
# File lib/librato/rack/configuration.rb, line 105
def check_deprecations
  if self.disable_rack_metrics
    deprecate "disable_rack_metrics configuration option will be removed in a future release, please use config.suites = 'none' instead."
  end
end
default_suites() click to toggle source
# File lib/librato/rack/configuration.rb, line 101
def default_suites
  DEFAULT_SUITES
end
deprecate(message) click to toggle source
# File lib/librato/rack/configuration.rb, line 111
def deprecate(message)
  @deprecations << message
end
detect_autorun() click to toggle source
# File lib/librato/rack/configuration.rb, line 115
def detect_autorun
  case ENV['LIBRATO_AUTORUN']
  when '0', 'FALSE'
    false
  when '1', 'TRUE'
    true
  else
    nil
  end
end