Class: StatsD::Instrument::Backends::UDPBackend

Inherits:
StatsD::Instrument::Backend show all
Includes:
MonitorMixin
Defined in:
lib/statsd/instrument/backends/udp_backend.rb

Defined Under Namespace

Classes: DogStatsDProtocol, StatsDProtocol, StatsiteStatsDProtocol

Constant Summary collapse

DEFAULT_IMPLEMENTATION =
:statsd

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server = nil, implementation = nil) ⇒ UDPBackend

Returns a new instance of UDPBackend.



87
88
89
90
91
# File 'lib/statsd/instrument/backends/udp_backend.rb', line 87

def initialize(server = nil, implementation = nil)
  super()
  self.server = server || "localhost:8125"
  self.implementation = (implementation || DEFAULT_IMPLEMENTATION).to_sym
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



85
86
87
# File 'lib/statsd/instrument/backends/udp_backend.rb', line 85

def host
  @host
end

#implementationObject

Returns the value of attribute implementation.



85
86
87
# File 'lib/statsd/instrument/backends/udp_backend.rb', line 85

def implementation
  @implementation
end

#portObject

Returns the value of attribute port.



85
86
87
# File 'lib/statsd/instrument/backends/udp_backend.rb', line 85

def port
  @port
end

Instance Method Details

#collect_metric(metric) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/statsd/instrument/backends/udp_backend.rb', line 105

def collect_metric(metric)
  unless @packet_factory.supported?(metric)
    StatsD.logger.warn("[StatsD] Metric type #{metric.type.inspect} not supported on #{implementation} implementation.")
    return false
  end

  if metric.sample_rate < 1.0 && rand > metric.sample_rate
    return false
  end

  write_packet(@packet_factory.generate_packet(metric))
end

#invalidate_socketObject



155
156
157
# File 'lib/statsd/instrument/backends/udp_backend.rb', line 155

def invalidate_socket
  @socket = nil
end

#server=(connection_string) ⇒ Object



118
119
120
121
122
# File 'lib/statsd/instrument/backends/udp_backend.rb', line 118

def server=(connection_string)
  @host, @port = connection_string.split(':', 2)
  @port = @port.to_i
  invalidate_socket
end

#socketObject



134
135
136
137
138
139
140
# File 'lib/statsd/instrument/backends/udp_backend.rb', line 134

def socket
  if @socket.nil?
    @socket = UDPSocket.new
    @socket.connect(host, port)
  end
  @socket
end

#write_packet(command) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/statsd/instrument/backends/udp_backend.rb', line 142

def write_packet(command)
  synchronize do
    socket.send(command, 0) > 0
  end
rescue ThreadError => e
  # In cases where a TERM or KILL signal has been sent, and we send stats as
  # part of a signal handler, locks cannot be acquired, so we do our best
  # to try and send the command without a lock.
  socket.send(command, 0) > 0
rescue SocketError, IOError, SystemCallError, Errno::ECONNREFUSED => e
  StatsD.logger.error "[StatsD] #{e.class.name}: #{e.message}"
end