Class: StatsD::Instrument::Backends::UDPBackend
- Inherits:
-
StatsD::Instrument::Backend
- Object
- StatsD::Instrument::Backend
- StatsD::Instrument::Backends::UDPBackend
- 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.
9 10 11 12 |
# File 'lib/statsd/instrument/backends/udp_backend.rb', line 9 def initialize(server = nil, implementation = nil) self.server = server || "localhost:8125" self.implementation = (implementation || DEFAULT_IMPLEMENTATION).to_sym end |
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
6 7 8 |
# File 'lib/statsd/instrument/backends/udp_backend.rb', line 6 def host @host end |
#implementation ⇒ Object
Returns the value of attribute implementation.
7 8 9 |
# File 'lib/statsd/instrument/backends/udp_backend.rb', line 7 def implementation @implementation end |
#port ⇒ Object
Returns the value of attribute port.
6 7 8 |
# File 'lib/statsd/instrument/backends/udp_backend.rb', line 6 def port @port end |
Instance Method Details
#collect_metric(metric) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/statsd/instrument/backends/udp_backend.rb', line 14 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
59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/statsd/instrument/backends/udp_backend.rb', line 59 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
27 28 29 30 31 32 33 |
# File 'lib/statsd/instrument/backends/udp_backend.rb', line 27 def implementation_supports_metric_type?(type) case type when :h; implementation == :datadog when :kv; implementation == :statsite else true end end |
#invalidate_socket ⇒ Object
84 85 86 |
# File 'lib/statsd/instrument/backends/udp_backend.rb', line 84 def invalidate_socket @socket = nil end |
#server=(connection_string) ⇒ Object
35 36 37 38 39 |
# File 'lib/statsd/instrument/backends/udp_backend.rb', line 35 def server=(connection_string) self.host, port = connection_string.split(':', 2) self.port = port.to_i invalidate_socket end |
#socket ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/statsd/instrument/backends/udp_backend.rb', line 51 def socket if @socket.nil? @socket = UDPSocket.new @socket.connect(host, port) end @socket end |
#tags_supported? ⇒ Boolean
74 75 76 |
# File 'lib/statsd/instrument/backends/udp_backend.rb', line 74 def implementation == :datadog end |