Class: Fozzie::Adapter::Statsd
- Inherits:
-
Object
- Object
- Fozzie::Adapter::Statsd
- Defined in:
- lib/fozzie/adapter/statsd.rb
Direct Known Subclasses
Constant Summary collapse
- RESERVED_CHARS_REGEX =
/[\:\|\@\s]/
- RESERVED_CHARS_REPLACEMENT =
'_'
- DELIMETER =
'.'
- SAFE_SEPARATOR =
'-'
- TYPES =
{ :gauge => 'g', :count => 'c', :timing => 'ms' }
- BULK_DELIMETER =
"\n"
Instance Method Summary collapse
- #delimeter ⇒ Object
- #format_bucket(stat) ⇒ Object
- #format_value(val, type, sample_rate) ⇒ Object
- #host_ip ⇒ Object
- #host_port ⇒ Object
-
#register(*stats) ⇒ Object
Send the statistic to the server.
- #safe_separator ⇒ Object
-
#sampled(sample_rate) ⇒ Object
If the statistic is sampled, generate a condition to check if it’s good to send.
- #sampled?(sample_rate) ⇒ Boolean
-
#send_to_socket(message) ⇒ Object
Send data to the server via the socket.
-
#socket ⇒ Object
The Socket we want to use to send data.
Instance Method Details
#delimeter ⇒ Object
84 85 86 |
# File 'lib/fozzie/adapter/statsd.rb', line 84 def delimeter DELIMETER end |
#format_bucket(stat) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/fozzie/adapter/statsd.rb', line 31 def format_bucket(stat) bucket = [stat].flatten.compact.collect(&:to_s).join(DELIMETER).downcase bucket = bucket.gsub('::', DELIMETER).gsub(RESERVED_CHARS_REGEX, RESERVED_CHARS_REPLACEMENT) bucket = [Fozzie.c.data_prefix, bucket].compact.join(DELIMETER) bucket end |
#format_value(val, type, sample_rate) ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/fozzie/adapter/statsd.rb', line 39 def format_value(val, type, sample_rate) converted_type = TYPES[type.to_sym] converted_type ||= TYPES[:gauge] value = [val, converted_type].join('|') value << '@%s' % sample_rate.to_s if sample_rate < 1 value end |
#host_ip ⇒ Object
76 77 78 |
# File 'lib/fozzie/adapter/statsd.rb', line 76 def host_ip @host_ip ||= Resolv.getaddress(Fozzie.c.host) end |
#host_port ⇒ Object
80 81 82 |
# File 'lib/fozzie/adapter/statsd.rb', line 80 def host_port @host_port ||= Fozzie.c.port end |
#register(*stats) ⇒ Object
Send the statistic to the server
Creates the Statsd key from the given values, and sends to socket (depending on sample rate)
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/fozzie/adapter/statsd.rb', line 18 def register(*stats) metrics = stats.flatten.map do |stat| next if sampled?(stat[:sample_rate]) bucket = format_bucket(stat[:bucket]) value = format_value(stat[:value], stat[:type], stat[:sample_rate]) [bucket, value].join(':') end.compact.join(BULK_DELIMETER) send_to_socket(metrics) end |
#safe_separator ⇒ Object
88 89 90 |
# File 'lib/fozzie/adapter/statsd.rb', line 88 def safe_separator SAFE_SEPARATOR end |
#sampled(sample_rate) ⇒ Object
If the statistic is sampled, generate a condition to check if it’s good to send
50 51 52 |
# File 'lib/fozzie/adapter/statsd.rb', line 50 def sampled(sample_rate) yield unless sampled?(sample_rate) end |
#sampled?(sample_rate) ⇒ Boolean
54 55 56 |
# File 'lib/fozzie/adapter/statsd.rb', line 54 def sampled?(sample_rate) sample_rate < 1 and rand > sample_rate end |
#send_to_socket(message) ⇒ Object
Send data to the server via the socket
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/fozzie/adapter/statsd.rb', line 59 def send_to_socket() Fozzie.logger.debug {"Statsd: #{}"} if Fozzie.logger Timeout.timeout(Fozzie.c.timeout) { res = socket.send(, 0, host_ip, host_port) Fozzie.logger.debug {"Statsd sent: #{res}"} if Fozzie.logger (res.to_i == .length) } rescue => exc Fozzie.logger.debug {"Statsd Failure: #{exc.}\n#{exc.backtrace}"} if Fozzie.logger false end |
#socket ⇒ Object
The Socket we want to use to send data
72 73 74 |
# File 'lib/fozzie/adapter/statsd.rb', line 72 def socket @socket ||= ::UDPSocket.new end |