Class: ActiveStatsD::Server
- Inherits:
-
Object
- Object
- ActiveStatsD::Server
- Defined in:
- lib/active_statsd/server.rb
Instance Method Summary collapse
-
#initialize(host:, port:, aggregation:, forward_host:, forward_port:) ⇒ Server
constructor
A new instance of Server.
- #start ⇒ Object
Constructor Details
#initialize(host:, port:, aggregation:, forward_host:, forward_port:) ⇒ Server
Returns a new instance of Server.
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/active_statsd/server.rb', line 6 def initialize(host:, port:, aggregation:, forward_host:, forward_port:) @host = host @port = port @aggregation = aggregation @forward_host = forward_host @forward_port = forward_port @counters = Hash.new(0) @mutex = Mutex.new @forward_socket = UDPSocket.new if forwarding_enabled? @running = false end |
Instance Method Details
#start ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/active_statsd/server.rb', line 18 def start return if @running # Don't attempt to start twice Thread.new do begin sockets = Socket.udp_server_sockets(@host, @port) rescue Errno::EADDRINUSE Rails.logger.warn "[ActiveStatsD] Server already running on #{@host}:#{@port}, skipping startup." next end @running = true Rails.logger.info "[ActiveStatsD] UDP StatsD listener started on #{@host}:#{@port} (aggregation=#{@aggregation})" start_aggregation_thread if aggregation_enabled? begin Socket.udp_server_loop_on(sockets) do |msg, _| Rails.logger.debug "[ActiveStatsD] UDP packet received: #{msg.strip}" (msg.strip) end rescue => e Rails.logger.error "[ActiveStatsD] Server error: #{e.class.name} - #{e.}\n#{e.backtrace.join("\n")}" ensure sockets.each(&:close) @running = false end end end |