Class: StatsD::Instrument::Backends::UDPBackend
- Inherits:
-
StatsD::Instrument::Backend
- Object
- StatsD::Instrument::Backend
- StatsD::Instrument::Backends::UDPBackend
- Includes:
- MonitorMixin
- Defined in:
- lib/statsd/instrument/backends/udp_backend.rb
Constant Summary collapse
- DEFAULT_IMPLEMENTATION =
:statsd
Instance Attribute Summary collapse
-
#host ⇒ Object
Returns the value of attribute host.
-
#implementation ⇒ Object
Returns the value of attribute implementation.
-
#port ⇒ Object
Returns the value of attribute port.
Instance Method Summary collapse
- #collect_metric(metric) ⇒ Object
- #generate_packet(metric) ⇒ Object
- #implementation_supports_metric_type?(type) ⇒ Boolean
-
#initialize(server = nil, implementation = nil) ⇒ UDPBackend
constructor
A new instance of UDPBackend.
- #invalidate_socket ⇒ Object
- #server=(connection_string) ⇒ Object
- #socket ⇒ Object
- #tags_supported? ⇒ Boolean
- #write_packet(command) ⇒ Object
Constructor Details
#initialize(server = nil, implementation = nil) ⇒ UDPBackend
Returns a new instance of UDPBackend.
13 14 15 16 17 |
# File 'lib/statsd/instrument/backends/udp_backend.rb', line 13 def initialize(server = nil, implementation = nil) super() self.server = server || "localhost:8125" self.implementation = (implementation || DEFAULT_IMPLEMENTATION).to_sym end |
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
10 11 12 |
# File 'lib/statsd/instrument/backends/udp_backend.rb', line 10 def host @host end |
#implementation ⇒ Object
Returns the value of attribute implementation.
11 12 13 |
# File 'lib/statsd/instrument/backends/udp_backend.rb', line 11 def implementation @implementation end |
#port ⇒ Object
Returns the value of attribute port.
10 11 12 |
# File 'lib/statsd/instrument/backends/udp_backend.rb', line 10 def port @port end |
Instance Method Details
#collect_metric(metric) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/statsd/instrument/backends/udp_backend.rb', line 19 def collect_metric(metric) unless implementation_supports_metric_type?(metric.type) 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(generate_packet(metric)) end |
#generate_packet(metric) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/statsd/instrument/backends/udp_backend.rb', line 64 def generate_packet(metric) command = "#{metric.name}:#{metric.value}|#{metric.type}" command << "|@#{metric.sample_rate}" if metric.sample_rate < 1 || (implementation == :statsite && metric.sample_rate > 1) if metric. if command << "|##{metric..join(',')}" else StatsD.logger.warn("[StatsD] Tags are only supported on Datadog implementation.") end end command << "\n" if implementation == :statsite command end |
#implementation_supports_metric_type?(type) ⇒ Boolean
32 33 34 35 36 37 38 |
# File 'lib/statsd/instrument/backends/udp_backend.rb', line 32 def implementation_supports_metric_type?(type) case type when :h; implementation == :datadog when :kv; implementation == :statsite else true end end |
#invalidate_socket ⇒ Object
96 97 98 |
# File 'lib/statsd/instrument/backends/udp_backend.rb', line 96 def invalidate_socket @socket = nil end |
#server=(connection_string) ⇒ Object
40 41 42 43 44 |
# File 'lib/statsd/instrument/backends/udp_backend.rb', line 40 def server=(connection_string) self.host, port = connection_string.split(':', 2) self.port = port.to_i invalidate_socket end |
#socket ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/statsd/instrument/backends/udp_backend.rb', line 56 def socket if @socket.nil? @socket = UDPSocket.new @socket.connect(host, port) end @socket end |
#tags_supported? ⇒ Boolean
79 80 81 |
# File 'lib/statsd/instrument/backends/udp_backend.rb', line 79 def implementation == :datadog end |
#write_packet(command) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/statsd/instrument/backends/udp_backend.rb', line 83 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 => e StatsD.logger.error "[StatsD] #{e.class.name}: #{e.}" end |