Class: Fozzie::Adapter::Statsd

Inherits:
Object
  • Object
show all
Defined in:
lib/fozzie/adapter/statsd.rb

Direct Known Subclasses

Datadog

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

Instance Method Details

#delimeterObject



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_ipObject



76
77
78
# File 'lib/fozzie/adapter/statsd.rb', line 76

def host_ip
  @host_ip ||= Resolv.getaddress(Fozzie.c.host)
end

#host_portObject



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_separatorObject



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

Returns:

  • (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(message)
  Fozzie.logger.debug {"Statsd: #{message}"} if Fozzie.logger
  Timeout.timeout(Fozzie.c.timeout) {
    res = socket.send(message, 0, host_ip, host_port)
    Fozzie.logger.debug {"Statsd sent: #{res}"} if Fozzie.logger
    (res.to_i == message.length)
  }
rescue => exc
  Fozzie.logger.debug {"Statsd Failure: #{exc.message}\n#{exc.backtrace}"} if Fozzie.logger
  false
end

#socketObject

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