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(":")
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" 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" 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" 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
|