Class: MultiStatsd::Backend::Hottie

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

Overview

Hottie backend Hottie is a redis-based short-term backend to enable real-time visibility into application behavior. It is specifically designed to enable real-time heatmap/histogram visualizations from timer data.

Instance Attribute Summary collapse

Attributes inherited from Base

#counters, #gauges, #name, #timers

Instance Method Summary collapse

Methods inherited from Base

#flush_interval, #format_key, #initialize, #reset_stats, #write

Constructor Details

This class inherits a constructor from MultiStatsd::Backend::Base

Instance Attribute Details

#samples_to_retainObject (readonly)

Returns the value of attribute samples_to_retain.



7
8
9
# File 'lib/multi-statsd/backends/hottie.rb', line 7

def samples_to_retain
  @samples_to_retain
end

#seconds_to_retainObject (readonly)

Returns the value of attribute seconds_to_retain.



7
8
9
# File 'lib/multi-statsd/backends/hottie.rb', line 7

def seconds_to_retain
  @seconds_to_retain
end

Instance Method Details

#flushFloat

Flush the data to redis in the format required for Hottie

Returns:

  • (Float)

    The number of seconds it took to aggregate/flush the data to redis



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
# File 'lib/multi-statsd/backends/hottie.rb', line 22

def flush
  counters, timers, gauges = reset_stats
  ts = Time.new.to_i
  time = ::Benchmark.realtime do
    @db.pipelined do
      if !gauges.empty?
        @db.hmset "gauges:#{ts}", *(gauges.map { |stat, gauge|  [stat, gauge] }.flatten)
        @db.expire "gauges:#{ts}", @seconds_to_retain + 5 # Retain a few extra seconds to avoid reporting race
        @db.sadd "gauges", gauges.keys
      end
      @db.lpush "gauge_samples", "gauges:#{ts}"
      @db.ltrim "gauge_samples", 0, @samples_to_retain - 1

      if !counters.empty?
        @db.hmset "counters:#{ts}", *(counters.map { |stat, counter|[stat, counter / @options['flush_interval']] }.flatten)
        @db.expire "counters:#{ts}", @seconds_to_retain + 5 # Retain a few extra seconds to avoid reporting race
        @db.sadd "counters", counters.keys
      end
      @db.lpush "counter_samples", "counters:#{ts}"
      @db.ltrim "counter_samples", 0, @samples_to_retain - 1

      if !timers.empty?
        timer_hash = Hash.new(0)
        @db.hmset "timers:#{ts}", *(timers.map { |stat, data|
          timer_hash.clear
          data.each { |d| timer_hash[d.round] += 1 }
          [stat, Marshal.dump(timer_hash)]
        })
        @db.expire "timers:#{ts}", @seconds_to_retain + 5 # Retain a few extra seconds to avoid reporting race
        @db.sadd "timers", timers.keys
      end
      @db.lpush "timer_samples", "timers:#{ts}"
      @db.ltrim "timer_samples", 0, @samples_to_retain - 1
    end
  end
  MultiStatsd.logger.debug "Hottie flushing took #{"%.3f" % (time * 1000)}ms"
  time
rescue Redis::CannotConnectError
  MultiStatsd.logger.warning "Unable to connect to redis, skipping flush"
end

#post_initObject

Initialize a connection to redis and configure our samples to retain



10
11
12
13
14
15
16
17
18
# File 'lib/multi-statsd/backends/hottie.rb', line 10

def post_init
  @db = Redis.new(
    :host => (@options['host'] || '127.0.0.1'),
    :port => (@options['port'] || 6379),
    :database => (@options['database'] || 1)
  )
  @seconds_to_retain = @options['seconds_to_retain'] || 60
  @samples_to_retain = (@seconds_to_retain / @options['flush_interval']).floor
end