class StatsD::Instrument::Environment
The environment module is used to detect, and initialize the environment in which this library is active. It will use different default values based on the environment.
Attributes
Public Class Methods
# File lib/statsd/instrument/environment.rb, line 6 def current @current ||= StatsD::Instrument::Environment.new(ENV) end
Instantiates a default backend for the current environment.
@return [StatsD::Instrument::Backend] @see environment
# File lib/statsd/instrument/environment.rb, line 25 def default_backend case environment when 'production', 'staging' StatsD::Instrument::Backends::UDPBackend.new(current.statsd_addr, current.statsd_implementation) when 'test' StatsD::Instrument::Backends::NullBackend.new else StatsD::Instrument::Backends::LoggerBackend.new(StatsD.logger) end end
Detects the current environment, either by asking Rails, or by inspecting environment variables.
-
Within a Rails application,
Rails.envis used. -
It will check the following environment variables in order:
RAILS_ENV,RACK_ENV,ENV. -
If none of these are set, it will return
development
@return [String] The detected environment.
# File lib/statsd/instrument/environment.rb, line 17 def environment current.environment end
# File lib/statsd/instrument/environment.rb, line 56 def initialize(env) @env = env end
Sets default values for sample rate and logger.
-
Default sample rate is set to the value in the STATSD_SAMPLE_RATE environment variable, or 1.0 otherwise. See {StatsD#default_sample_rate}
-
{StatsD#logger} is set to a logger that send output to stderr.
If you are including this library inside a Rails environment, additional initialization will be done as part of the {StatsD::Instrument::Railtie}.
@return [void]
# File lib/statsd/instrument/environment.rb, line 46 def setup StatsD.prefix = current.statsd_prefix StatsD.default_tags = current.statsd_default_tags StatsD.default_sample_rate = current.statsd_sample_rate StatsD.logger = Logger.new($stderr) end
Public Instance Methods
# File lib/statsd/instrument/environment.rb, line 97 def client if env.key?('STATSD_USE_NEW_CLIENT') StatsD::Instrument::Client.from_env(self) else StatsD::Instrument::LegacyClient.singleton end end
# File lib/statsd/instrument/environment.rb, line 105 def default_sink_for_environment case environment when 'production', 'staging' StatsD::Instrument::UDPSink.for_addr(statsd_addr) when 'test' StatsD::Instrument::NullSink.new else StatsD::Instrument::LogSink.new(StatsD.logger) end end
Detects the current environment, either by asking Rails, or by inspecting environment variables.
-
Within a Rails application,
Rails.envis used. -
It will check the following environment variables in order:
RAILS_ENV,RACK_ENV,ENV. -
If none of these are set, it will return
development
@return [String] The detected environment.
# File lib/statsd/instrument/environment.rb, line 67 def environment if env['STATSD_ENV'] env['STATSD_ENV'] elsif defined?(Rails) && Rails.respond_to?(:env) Rails.env.to_s else env['RAILS_ENV'] || env['RACK_ENV'] || env['ENV'] || 'development' end end
# File lib/statsd/instrument/environment.rb, line 89 def statsd_addr env.fetch('STATSD_ADDR', 'localhost:8125') end
# File lib/statsd/instrument/environment.rb, line 77 def statsd_implementation env.fetch('STATSD_IMPLEMENTATION', 'datadog') end
# File lib/statsd/instrument/environment.rb, line 85 def statsd_prefix env.fetch('STATSD_PREFIX', nil) end
# File lib/statsd/instrument/environment.rb, line 81 def statsd_sample_rate env.fetch('STATSD_SAMPLE_RATE', 1.0).to_f end