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

BASE_SUPPORTED_METRIC_TYPES =
{ c: true, ms: true, g: true, s: true }
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.



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

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.



83
84
85
# File 'lib/statsd/instrument/backends/udp_backend.rb', line 83

def host
  @host
end

#implementationObject

Returns the value of attribute implementation.



83
84
85
# File 'lib/statsd/instrument/backends/udp_backend.rb', line 83

def implementation
  @implementation
end

#portObject

Returns the value of attribute port.



83
84
85
# File 'lib/statsd/instrument/backends/udp_backend.rb', line 83

def port
  @port
end

Instance Method Details

#collect_metric(metric) ⇒ Object



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

def collect_metric(metric)
  unless @packet_factory.class::SUPPORTED_METRIC_TYPES[metric.type]
    StatsD.logger.warn("[StatsD] Metric type #{metric.type.inspect} is 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
158
159
# File 'lib/statsd/instrument/backends/udp_backend.rb', line 155

def invalidate_socket
  synchronize do
    @socket = nil
  end
end

#server=(connection_string) ⇒ Object



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

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

#socketObject



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

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

#write_packet(command) ⇒ Object



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

def write_packet(command)
  synchronize do
    socket.send(command, 0) > 0
  end
rescue ThreadError
  # 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 => e
  StatsD.logger.error("[StatsD] #{e.class.name}: #{e.message}")
  invalidate_socket
end