Class: MultiStatsd::Backend::Base Abstract
- Inherits:
-
Object
- Object
- MultiStatsd::Backend::Base
- Defined in:
- lib/multi-statsd/backends/base.rb
Overview
Subclass and override #flush to implement a custom Backend class.
Instance Attribute Summary collapse
-
#counters ⇒ Object
readonly
Returns the value of attribute counters.
-
#gauges ⇒ Object
readonly
Returns the value of attribute gauges.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#timers ⇒ Object
readonly
Returns the value of attribute timers.
Instance Method Summary collapse
-
#flush ⇒ Object
Each backend must implement this method to flush its data.
-
#flush_interval ⇒ Integer
The flush interval.
-
#format_key(key) ⇒ String
Format a given key.
-
#initialize(name, options = {}) ⇒ Base
constructor
A new instance of Base.
-
#post_init ⇒ Object
Override in subclasses to execute code after initialization (eg. database connection setup).
-
#reset_stats ⇒ Array
Reset and return the generated data in a mutex to ensure none are lost.
-
#write(msg) ⇒ true
Record data in statsd format Gauges - cpu:0.15|g Timers - api:12|ms Counters - bytes:123|c Counters with sampling - bytes:123|c|@0.1 Multiple values - api:12|ms:15|ms:8|ms.
Constructor Details
#initialize(name, options = {}) ⇒ Base
Returns a new instance of Base.
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/multi-statsd/backends/base.rb', line 14 def initialize(name, = {}) @name, @options = name, @timers, @gauges, @counters = {}, {}, Hash.new(0) @semaphore = Mutex.new @options['flush_interval'] ||= 15 post_init EventMachine::add_periodic_timer(@options['flush_interval']) do EM.defer { flush } end end |
Instance Attribute Details
#counters ⇒ Object (readonly)
Returns the value of attribute counters.
12 13 14 |
# File 'lib/multi-statsd/backends/base.rb', line 12 def counters @counters end |
#gauges ⇒ Object (readonly)
Returns the value of attribute gauges.
12 13 14 |
# File 'lib/multi-statsd/backends/base.rb', line 12 def gauges @gauges end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
12 13 14 |
# File 'lib/multi-statsd/backends/base.rb', line 12 def name @name end |
#timers ⇒ Object (readonly)
Returns the value of attribute timers.
12 13 14 |
# File 'lib/multi-statsd/backends/base.rb', line 12 def timers @timers end |
Instance Method Details
#flush ⇒ Object
Each backend must implement this method to flush its data
38 39 40 |
# File 'lib/multi-statsd/backends/base.rb', line 38 def flush raise NotImplementedError end |
#flush_interval ⇒ Integer
Returns the flush interval.
29 30 31 |
# File 'lib/multi-statsd/backends/base.rb', line 29 def flush_interval @options['flush_interval'] end |
#format_key(key) ⇒ String
Format a given key
100 101 102 |
# File 'lib/multi-statsd/backends/base.rb', line 100 def format_key(key) key.gsub(/\s+/, '_').gsub(/\//, '-').gsub(/[^a-zA-Z_\-0-9\.]/, '') end |
#post_init ⇒ Object
Override in subclasses to execute code after initialization (eg. database connection setup)
34 35 |
# File 'lib/multi-statsd/backends/base.rb', line 34 def post_init end |
#reset_stats ⇒ Array
Reset and return the generated data in a mutex to ensure none are lost
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/multi-statsd/backends/base.rb', line 44 def reset_stats @semaphore.synchronize do counters = @counters.dup timers = @timers.dup gauges = @gauges.dup @counters.clear @timers.clear [counters, timers, gauges] end end |
#write(msg) ⇒ true
Record data in statsd format
Gauges - cpu:0.15|g
Timers - api:12|ms
Counters - bytes:123|c
Counters with sampling - bytes:123|c|@0.1
Multiple values - api:12|ms:15|ms:8|ms
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/multi-statsd/backends/base.rb', line 63 def write(msg) msg.each_line do |row| # Fetch our key and records key, *records = row.split(":") # Clean up the key formatting key = format_key(key) # Iterate through each record and store the data records.each do |record| value, type, sample_rate = record.split('|') next unless value and type and value =~ /^(?:[\d\.-]+)$/ case type when "ms" @timers[key] ||= [] @timers[key].push(value.to_f) when "c" if sample_rate sample_rate = sample_rate.gsub(/[^\d\.]/, '').to_f sample_rate = 1 if sample_rate <= 0 @counters[key] += value.to_f * (1.0 / sample_rate) else @counters[key] += value.to_f end when "g" @gauges[key] = value.to_f end end end true end |