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.



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

def initialize
  @host = ENV.fetch(ENV_NAME, nil)
  @port = ENV.fetch("STATSD_PORT", 8125)
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

#enabled?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/puma/plugin/statsd.rb', line 19

def enabled?
  !!host
end

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



23
24
25
26
27
28
29
30
31
# File 'lib/puma/plugin/statsd.rb', line 23

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