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
- .current ⇒ Object
-
.environment ⇒ Object
deprecated
Deprecated.
For backwards compatibility only. Use #environment through Environment.current instead.
-
.setup ⇒ void
Sets default values for sample rate and logger.
Instance Method Summary collapse
- #aggregation_interval ⇒ Object
- #aggregation_max_context_size ⇒ Object
- #client ⇒ Object
- #default_sink_for_environment ⇒ Object
-
#environment ⇒ String
Detects the current environment, either by asking Rails, or by inspecting environment variables.
- #experimental_aggregation_enabled? ⇒ Boolean
-
#initialize(env) ⇒ Environment
constructor
A new instance of Environment.
- #statsd_addr ⇒ Object
- #statsd_batch_statistics_interval ⇒ Object
- #statsd_batching? ⇒ Boolean
- #statsd_buffer_capacity ⇒ Object
- #statsd_default_tags ⇒ Object
- #statsd_implementation ⇒ Object
- #statsd_max_packet_size ⇒ Object
- #statsd_prefix ⇒ Object
- #statsd_sample_rate ⇒ Object
- #statsd_socket_path ⇒ Object
- #statsd_uds_send? ⇒ Boolean
Constructor Details
#initialize(env) ⇒ Environment
Returns a new instance of Environment.
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/statsd/instrument/environment.rb', line 36 def initialize(env) @env = env if env.key?("STATSD_FLUSH_INTERVAL") value = env["STATSD_FLUSH_INTERVAL"] if Float(value) == 0.0 warn("STATSD_FLUSH_INTERVAL=#{value} is deprecated, please set STATSD_BUFFER_CAPACITY=0 instead.") else warn("STATSD_FLUSH_INTERVAL=#{value} is deprecated and has no effect, please remove it.") end end end |
Instance Attribute Details
#env ⇒ Object (readonly)
Returns the value of attribute env.
34 35 36 |
# File 'lib/statsd/instrument/environment.rb', line 34 def env @env end |
Class Method Details
.current ⇒ Object
9 10 11 |
# File 'lib/statsd/instrument/environment.rb', line 9 def current @current ||= StatsD::Instrument::Environment.new(ENV) end |
.environment ⇒ Object
For backwards compatibility only. Use #environment through current instead.
15 16 17 |
# File 'lib/statsd/instrument/environment.rb', line 15 def environment current.environment 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.
29 30 31 |
# File 'lib/statsd/instrument/environment.rb', line 29 def setup StatsD.logger = Logger.new($stderr) end |
Instance Method Details
#aggregation_interval ⇒ Object
124 125 126 |
# File 'lib/statsd/instrument/environment.rb', line 124 def aggregation_interval Float(env.fetch("STATSD_AGGREGATION_INTERVAL", 2.0)) end |
#aggregation_max_context_size ⇒ Object
128 129 130 131 132 133 |
# File 'lib/statsd/instrument/environment.rb', line 128 def aggregation_max_context_size Integer(env.fetch( "STATSD_AGGREGATION_MAX_CONTEXT_SIZE", StatsD::Instrument::Aggregator::DEFAULT_MAX_CONTEXT_SIZE, )) end |
#client ⇒ Object
135 136 137 |
# File 'lib/statsd/instrument/environment.rb', line 135 def client StatsD::Instrument::Client.from_env(self) end |
#default_sink_for_environment ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/statsd/instrument/environment.rb', line 139 def default_sink_for_environment case environment when "production", "staging" connection = if statsd_uds_send? StatsD::Instrument::UdsConnection.new( statsd_socket_path, max_packet_size: statsd_max_packet_size, ) else host, port = statsd_addr.split(":") StatsD::Instrument::UdpConnection.new( host, port.to_i, max_packet_size: statsd_max_packet_size, ) end sink = StatsD::Instrument::Sink.new(connection) if statsd_batching? current_send_buffer_size = connection.send_buffer_size if current_send_buffer_size < statsd_max_packet_size StatsD.logger.warn do "[StatsD::Instrument::Environment] Send buffer size #{current_send_buffer_size} differs from " \ "max packet size #{statsd_max_packet_size}. Using send buffer size as max packet size." end end return StatsD::Instrument::BatchedSink.new( sink, buffer_capacity: statsd_buffer_capacity, max_packet_size: [current_send_buffer_size, statsd_max_packet_size].min, statistics_interval: statsd_batch_statistics_interval, ) end sink 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.
- It will prefer the value set in ENV['STATSD_ENV']
- 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
59 60 61 62 63 64 65 66 67 |
# File 'lib/statsd/instrument/environment.rb', line 59 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 |
#experimental_aggregation_enabled? ⇒ Boolean
120 121 122 |
# File 'lib/statsd/instrument/environment.rb', line 120 def experimental_aggregation_enabled? env.key?("STATSD_ENABLE_AGGREGATION") end |
#statsd_addr ⇒ Object
81 82 83 |
# File 'lib/statsd/instrument/environment.rb', line 81 def statsd_addr env.fetch("STATSD_ADDR", "localhost:8125") end |
#statsd_batch_statistics_interval ⇒ Object
113 114 115 116 117 118 |
# File 'lib/statsd/instrument/environment.rb', line 113 def statsd_batch_statistics_interval Integer(env.fetch( "STATSD_BATCH_STATISTICS_INTERVAL", StatsD::Instrument::BatchedSink::DEFAULT_STATISTICS_INTERVAL, )) end |
#statsd_batching? ⇒ Boolean
97 98 99 |
# File 'lib/statsd/instrument/environment.rb', line 97 def statsd_batching? statsd_buffer_capacity > 0 && Float(env.fetch("STATSD_FLUSH_INTERVAL", 1.0)) > 0.0 end |
#statsd_buffer_capacity ⇒ Object
93 94 95 |
# File 'lib/statsd/instrument/environment.rb', line 93 def statsd_buffer_capacity Integer(env.fetch("STATSD_BUFFER_CAPACITY", StatsD::Instrument::BatchedSink::DEFAULT_BUFFER_CAPACITY)) end |
#statsd_default_tags ⇒ Object
89 90 91 |
# File 'lib/statsd/instrument/environment.rb', line 89 def env.key?("STATSD_DEFAULT_TAGS") ? env.fetch("STATSD_DEFAULT_TAGS").split(",") : nil end |
#statsd_implementation ⇒ Object
69 70 71 |
# File 'lib/statsd/instrument/environment.rb', line 69 def statsd_implementation env.fetch("STATSD_IMPLEMENTATION", "datadog") end |
#statsd_max_packet_size ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/statsd/instrument/environment.rb', line 105 def statsd_max_packet_size if statsd_uds_send? Integer(env.fetch("STATSD_MAX_PACKET_SIZE", StatsD::Instrument::UdsConnection::DEFAULT_MAX_PACKET_SIZE)) else Integer(env.fetch("STATSD_MAX_PACKET_SIZE", StatsD::Instrument::UdpConnection::DEFAULT_MAX_PACKET_SIZE)) end end |
#statsd_prefix ⇒ Object
77 78 79 |
# File 'lib/statsd/instrument/environment.rb', line 77 def statsd_prefix env.fetch("STATSD_PREFIX", nil) end |
#statsd_sample_rate ⇒ Object
73 74 75 |
# File 'lib/statsd/instrument/environment.rb', line 73 def statsd_sample_rate env.fetch("STATSD_SAMPLE_RATE", 1.0).to_f end |
#statsd_socket_path ⇒ Object
85 86 87 |
# File 'lib/statsd/instrument/environment.rb', line 85 def statsd_socket_path env.fetch("STATSD_SOCKET_PATH", "") end |
#statsd_uds_send? ⇒ Boolean
101 102 103 |
# File 'lib/statsd/instrument/environment.rb', line 101 def statsd_uds_send? !statsd_socket_path.empty? end |