Module: StatsdServer::Proto::V1

Defined in:
lib/statsdserver/proto/v1.rb

Class Method Summary collapse

Class Method Details

.parse(data, stats) ⇒ Object



6
7
8
9
10
# File 'lib/statsdserver/proto/v1.rb', line 6

def self.parse(data, stats)
  data.split("\n").each do |update|
    self.parse_update(update, stats)
  end
end

.parse_update(update, stats) ⇒ Object

def parse



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/statsdserver/proto/v1.rb', line 12

def self.parse_update(update, stats)
  bits = update.split(":")
  # TODO: optimize into single regexp & compile?
  key = bits.shift.gsub(/\s+/, "_") \
          .gsub(/\//, "-") \
          .gsub(/[^a-zA-Z_\-0-9\.]/, "")
  bits << "1" if bits.length == 0
  bits.each do |bit|
    fields = bit.split("|")
    if fields.length != 2
      raise ParseError, "invalid update: #{bit}"
    end

    if fields[1] == "ms" or fields[1] == "t" # timer update
      if fields[0].index(",")
        fields[0].split(",").each do |value_str|
          value = Float(value_str) rescue nil
          stats.timers[key] << value if value
        end
      else
        value = Float(fields[0]) rescue nil
        if value.nil?
          raise ParseError, "invalid timer value for #{key}: #{fields[0]}"
        end
        stats.timers[key] << value
      end

    elsif fields[1] == "c" # counter update
      count_str, sample_rate_str = fields[0].split("@", 2)

      if sample_rate_str
        sample_rate = Float(sample_rate_str) rescue nil
        if sample_rate.nil?
          raise ParseError, "invalid sample_rate for #{key}: " +
                            "#{sample_rate_str}"
        end
      else
        sample_rate = 1
      end

      count = Integer(count_str) rescue nil
      if count.nil?
        raise ParseError, "invalid count for #{key}: #{count_str}"
      end

      stats.counters[key] += count.to_i * (1 / sample_rate.to_f)

    elsif fields[1] == "g" # gauge update
      value = Float(fields[0]) rescue nil
      if value.nil?
        raise ParseError, "invalid gauge value for #{key}: #{fields[0]}"
      end

      stats.gauges[key] = value

    else
      raise ParseError,
            "invalid update: #{update}: unknown type #{fields[1]}"
    end
  end
end