Class: StatsdConnector

Inherits:
Object
  • Object
show all
Defined in:
lib/puma/plugin/statsd.rb

Constant Summary collapse

ENV_NAME =
"STATSD_HOST"
STATSD_TYPES =
{ count: 'c', gauge: 'g' }
METRIC_DELIMETER =
"."

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStatsdConnector

Returns a new instance of StatsdConnector.



14
15
16
17
18
# File 'lib/puma/plugin/statsd.rb', line 14

def initialize
  @host = ENV.fetch(ENV_NAME, "127.0.0.1")
  @port = ENV.fetch("STATSD_PORT", 8125)
  @socket_path = ENV.fetch("STATSD_SOCKET_PATH", nil)
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



12
13
14
# File 'lib/puma/plugin/statsd.rb', line 12

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



12
13
14
# File 'lib/puma/plugin/statsd.rb', line 12

def port
  @port
end

Instance Method Details

#send(metric_name:, value:, type:, tags: nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/puma/plugin/statsd.rb', line 20

def send(metric_name:, value:, type:, tags: nil)
  data = "#{metric_name}:#{value}|#{STATSD_TYPES.fetch(type)}"
  data = "#{data}|##{tags}" unless tags.nil?

  if @socket_path
    socket = Socket.new(Socket::AF_UNIX, Socket::SOCK_DGRAM)
    socket.connect(Socket.pack_sockaddr_un(@socket_path))
    socket.sendmsg_nonblock(data)
  else
    socket = UDPSocket.new
    socket.send(data, 0, host, port)
  end
ensure
  socket.close
end