Class: MultiStatsd::Backend::Base Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/multi-statsd/backends/base.rb

Overview

This class is abstract.

Subclass and override #flush to implement a custom Backend class.

Direct Known Subclasses

Hottie, Stdout

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = {})
  @name, @options = name, options
  @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

#countersObject (readonly)

Returns the value of attribute counters.



12
13
14
# File 'lib/multi-statsd/backends/base.rb', line 12

def counters
  @counters
end

#gaugesObject (readonly)

Returns the value of attribute gauges.



12
13
14
# File 'lib/multi-statsd/backends/base.rb', line 12

def gauges
  @gauges
end

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/multi-statsd/backends/base.rb', line 12

def name
  @name
end

#timersObject (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

#flushObject

Each backend must implement this method to flush its data

Raises:

  • (NotImplementedError)


38
39
40
# File 'lib/multi-statsd/backends/base.rb', line 38

def flush
  raise NotImplementedError
end

#flush_intervalInteger

Returns the flush interval.

Returns:

  • (Integer)

    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

Parameters:

  • key (String)

    from the statsd record

Returns:

  • (String)

    formatted 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_initObject

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_statsArray

Reset and return the generated data in a mutex to ensure none are lost

Returns:

  • (Array)

    An array consisting of [counters, timers, gauges]



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

Parameters:

  • msg (String)

    string of data in statsd format

Returns:

  • (true)


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