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 =
".".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStatsdConnector

Returns a new instance of StatsdConnector.



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

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

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



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

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



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

def port
  @port
end

Instance Method Details

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



18
19
20
21
22
23
24
25
26
# File 'lib/puma/plugin/statsd.rb', line 18

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

  socket = UDPSocket.new
  socket.send(data, 0, host, port)
ensure
  socket.close
end