Class: Librato::Rack::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/librato/rack/configuration.rb

Overview

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

Examples:

config = Librato::Rack::Configuration.new
config.user  = '[email protected]'
config.token = 'mytoken'

Constant Summary collapse

EVENT_MODES =
[:eventmachine, :synchrony]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/librato/rack/configuration.rb', line 20

def initialize
  # set up defaults
  self.tracker = nil
  self.api_endpoint = Librato::Metrics.api_endpoint
  self.flush_interval = 60
  self.source_pids = false
  self.log_prefix = '[librato-rack] '
  @listeners = []
  @deprecations = []

  load_configuration
end

Instance Attribute Details

#api_endpointObject

Returns the value of attribute api_endpoint.



15
16
17
# File 'lib/librato/rack/configuration.rb', line 15

def api_endpoint
  @api_endpoint
end

#autorunObject

Returns the value of attribute autorun.



15
16
17
# File 'lib/librato/rack/configuration.rb', line 15

def autorun
  @autorun
end

#deprecationsObject (readonly)

Returns the value of attribute deprecations.



18
19
20
# File 'lib/librato/rack/configuration.rb', line 18

def deprecations
  @deprecations
end

#disable_rack_metricsObject

Returns the value of attribute disable_rack_metrics.



15
16
17
# File 'lib/librato/rack/configuration.rb', line 15

def disable_rack_metrics
  @disable_rack_metrics
end

#flush_intervalObject

Returns the value of attribute flush_interval.



15
16
17
# File 'lib/librato/rack/configuration.rb', line 15

def flush_interval
  @flush_interval
end

#log_levelObject

Returns the value of attribute log_level.



15
16
17
# File 'lib/librato/rack/configuration.rb', line 15

def log_level
  @log_level
end

#log_prefixObject

Returns the value of attribute log_prefix.



15
16
17
# File 'lib/librato/rack/configuration.rb', line 15

def log_prefix
  @log_prefix
end

#log_targetObject

Returns the value of attribute log_target.



15
16
17
# File 'lib/librato/rack/configuration.rb', line 15

def log_target
  @log_target
end

#prefixObject

Returns the value of attribute prefix.



18
19
20
# File 'lib/librato/rack/configuration.rb', line 18

def prefix
  @prefix
end

#sourceObject

Returns the value of attribute source.



18
19
20
# File 'lib/librato/rack/configuration.rb', line 18

def source
  @source
end

#source_pidsObject

Returns the value of attribute source_pids.



15
16
17
# File 'lib/librato/rack/configuration.rb', line 15

def source_pids
  @source_pids
end

#tokenObject

Returns the value of attribute token.



15
16
17
# File 'lib/librato/rack/configuration.rb', line 15

def token
  @token
end

#trackerObject

Returns the value of attribute tracker.



15
16
17
# File 'lib/librato/rack/configuration.rb', line 15

def tracker
  @tracker
end

#userObject

Returns the value of attribute user.



15
16
17
# File 'lib/librato/rack/configuration.rb', line 15

def user
  @user
end

Instance Method Details

#dumpObject



80
81
82
83
84
85
86
# File 'lib/librato/rack/configuration.rb', line 80

def dump
  fields = {}
  %w{user token log_level source prefix flush_interval source_pids}.each do |field|
    fields[field.to_sym] = self.send(field)
  end
  fields
end

#event_modeObject



33
34
35
# File 'lib/librato/rack/configuration.rb', line 33

def event_mode
  @event_mode
end

#event_mode=(mode) ⇒ Object

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



39
40
41
42
43
44
45
46
47
# File 'lib/librato/rack/configuration.rb', line 39

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

#explicit_source?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/librato/rack/configuration.rb', line 49

def explicit_source?
  !!@explicit_source
end

#load_configurationObject

check environment variables and capture current state for configuration



55
56
57
58
59
60
61
62
63
64
# File 'lib/librato/rack/configuration.rb', line 55

def load_configuration
  self.user = ENV['LIBRATO_USER'] || ENV['LIBRATO_METRICS_USER']
  self.token = ENV['LIBRATO_TOKEN'] || ENV['LIBRATO_METRICS_TOKEN']
  self.autorun = detect_autorun
  self.prefix = ENV['LIBRATO_PREFIX'] || ENV['LIBRATO_METRICS_PREFIX']
  self.source = ENV['LIBRATO_SOURCE'] || ENV['LIBRATO_METRICS_SOURCE']
  self.log_level = ENV['LIBRATO_LOG_LEVEL'] || :info
  self.event_mode = ENV['LIBRATO_EVENT_MODE']
  check_deprecations
end

#register_listener(listener) ⇒ Object



71
72
73
# File 'lib/librato/rack/configuration.rb', line 71

def register_listener(listener)
  @listeners << listener
end