Class: StatsD::Instrument::Environment
- Inherits:
-
Object
- Object
- StatsD::Instrument::Environment
- Defined in:
- lib/statsd/instrument/environment.rb
Overview
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.
Instance Attribute Summary collapse
-
#env ⇒ Object
readonly
Returns the value of attribute env.
Class Method Summary collapse
-
.default_backend ⇒ StatsD::Instrument::Backend
Instantiates a default backend for the current environment.
-
.environment ⇒ String
Detects the current environment, either by asking Rails, or by inspecting environment variables.
- .from_env ⇒ Object
-
.setup ⇒ void
Sets default values for sample rate and logger.
Instance Method Summary collapse
- #client ⇒ Object
- #default_client ⇒ Object
- #default_datagram_builder_class_for_implementation ⇒ Object
- #default_sink_for_environment ⇒ Object
-
#environment ⇒ String
Detects the current environment, either by asking Rails, or by inspecting environment variables.
-
#initialize(env) ⇒ Environment
constructor
A new instance of Environment.
- #statsd_addr ⇒ Object
- #statsd_default_tags ⇒ Object
- #statsd_implementation ⇒ Object
- #statsd_prefix ⇒ Object
- #statsd_sample_rate ⇒ Object
Constructor Details
#initialize(env) ⇒ Environment
Returns a new instance of Environment.
57 58 59 |
# File 'lib/statsd/instrument/environment.rb', line 57 def initialize(env) @env = env end |
Instance Attribute Details
#env ⇒ Object (readonly)
Returns the value of attribute env.
55 56 57 |
# File 'lib/statsd/instrument/environment.rb', line 55 def env @env end |
Class Method Details
.default_backend ⇒ StatsD::Instrument::Backend
Instantiates a default backend for the current environment.
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/statsd/instrument/environment.rb', line 26 def default_backend case environment when 'production', 'staging' StatsD::Instrument::Backends::UDPBackend.new(from_env.statsd_addr, from_env.statsd_implementation) when 'test' StatsD::Instrument::Backends::NullBackend.new else StatsD::Instrument::Backends::LoggerBackend.new(StatsD.logger) end end |
.environment ⇒ String
Detects the current environment, either by asking Rails, or by inspecting environment variables.
- Within a Rails application, Rails.env is 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
18 19 20 |
# File 'lib/statsd/instrument/environment.rb', line 18 def environment from_env.environment end |
.from_env ⇒ Object
7 8 9 |
# File 'lib/statsd/instrument/environment.rb', line 7 def from_env @from_env ||= StatsD::Instrument::Environment.new(ENV) end |
.setup ⇒ void
This method returns an undefined value.
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 Railtie.
47 48 49 50 51 52 |
# File 'lib/statsd/instrument/environment.rb', line 47 def setup StatsD.prefix = from_env.statsd_prefix StatsD. = from_env. StatsD.default_sample_rate = from_env.statsd_sample_rate StatsD.logger = Logger.new($stderr) end |
Instance Method Details
#client ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/statsd/instrument/environment.rb', line 98 def client if env.key?('STATSD_USE_NEW_CLIENT') default_client else StatsD::Instrument::LegacyClient.singleton end end |
#default_client ⇒ Object
106 107 108 109 110 111 112 113 114 |
# File 'lib/statsd/instrument/environment.rb', line 106 def default_client @default_client ||= StatsD::Instrument::Client.new( sink: default_sink_for_environment, datagram_builder_class: default_datagram_builder_class_for_implementation, default_sample_rate: statsd_sample_rate, prefix: statsd_prefix, default_tags: , ) end |
#default_datagram_builder_class_for_implementation ⇒ Object
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/statsd/instrument/environment.rb', line 116 def default_datagram_builder_class_for_implementation case statsd_implementation when 'statsd' StatsD::Instrument::StatsDDatagramBuilder when 'datadog', 'dogstatsd' StatsD::Instrument::DogStatsDDatagramBuilder else raise NotImplementedError, "No implementation for #{statsd_implementation}" end end |
#default_sink_for_environment ⇒ Object
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/statsd/instrument/environment.rb', line 127 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 |
#environment ⇒ String
Detects the current environment, either by asking Rails, or by inspecting environment variables.
- Within a Rails application, Rails.env is 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
68 69 70 71 72 73 74 75 76 |
# File 'lib/statsd/instrument/environment.rb', line 68 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 |
#statsd_addr ⇒ Object
90 91 92 |
# File 'lib/statsd/instrument/environment.rb', line 90 def statsd_addr env.fetch('STATSD_ADDR', 'localhost:8125') end |
#statsd_default_tags ⇒ Object
94 95 96 |
# File 'lib/statsd/instrument/environment.rb', line 94 def env.key?('STATSD_DEFAULT_TAGS') ? env.fetch('STATSD_DEFAULT_TAGS').split(',') : nil end |
#statsd_implementation ⇒ Object
78 79 80 |
# File 'lib/statsd/instrument/environment.rb', line 78 def statsd_implementation env.fetch('STATSD_IMPLEMENTATION', 'datadog') end |
#statsd_prefix ⇒ Object
86 87 88 |
# File 'lib/statsd/instrument/environment.rb', line 86 def statsd_prefix env.fetch('STATSD_PREFIX', nil) end |
#statsd_sample_rate ⇒ Object
82 83 84 |
# File 'lib/statsd/instrument/environment.rb', line 82 def statsd_sample_rate env.fetch('STATSD_SAMPLE_RATE', 1.0).to_f end |