Class: FFWD::Processor::HistogramProcessor

Inherits:
Object
  • Object
show all
Includes:
Logging, FFWD::Processor, Reporter
Defined in:
lib/ffwd/processor/histogram.rb

Overview

Implements histogram statistics over a tumbling time window.

Histogram received metrics continiuosly and regularly flushes out the following statistics.

<key>.min - Min value collected. <key>.max - Max value collected. <key>.mean - Mean value collected. <key>.p50 - The 50th percentile value collected. <key>.p75 - The 75th percentile value collected. <key>.p95 - The 95th percentile value collected. <key>.p99 - The 99th percentile value collected. <key>.p999 - The 99.9th percentile value collected.

Constant Summary collapse

DEFAULT_MISSING =
0
DEFAULT_PERCENTILES =
{
  :p50 => {:q => 0.50, :info => "50th"},
  :p75 => {:q => 0.75, :info => "75th"},
  :p95 => {:q => 0.95, :info => "95th"},
  :p99 => {:q => 0.99, :info => "99th"},
  :p999 => {:q => 0.999, :info => "99.9th"},
}

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Reporter

included, #increment, map_meta, #report!, #reporter_data

Methods included from Logging

included, #log

Methods included from FFWD::Processor

category, included, load_discovered, load_processors, #name, registry

Methods included from Lifecycle

#depend_on, #start, #started?, #starting, #starting_hooks, #stop, #stopped?, #stopping, #stopping_hooks

Constructor Details

#initialize(emitter, config = {}) ⇒ HistogramProcessor

Options:

:window - Define at what period the cache is flushed and generates metrics. :cache_limit - Limit the amount of cache entries (by key). :bucket_limit - Limit the amount of limits for each cache entry. :precision - Precision of emitted metrics. :percentiles - Configuration hash of percentile metrics. Structure:

{:p10 => {:info => "Some description", :q => 0.1}, ...}


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/ffwd/processor/histogram.rb', line 80

def initialize emitter, config={}
  @emitter = emitter

  @window = config[:window]
  @cache_limit = config[:cache_limit]
  @bucket_limit = config[:bucket_limit]
  @precision = config[:precision]
  @missing = config[:missing]
  @percentiles = config[:percentiles]

  # Dropped values that would have gone into a bucket.
  @cache = {}

  starting do
    log.info "Started"
    log.info "  config: #{config.inspect}"
  end

  stopping do
    log.info "Stopping histogram processor"
    @timer.cancel if @timer
    @timer = nil
    digest!
  end
end

Class Method Details

.prepare(config) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/ffwd/processor/histogram.rb', line 59

def self.prepare config
  config[:window] ||= 10
  config[:cache_limit] ||= 1000
  config[:bucket_limit] ||= 10000
  config[:precision] ||= 3
  config[:missing] ||= DEFAULT_MISSING
  config[:percentiles] ||= DEFAULT_PERCENTILES
  config
end

Instance Method Details

#calculate(bucket) {|"max", "Max", max| ... } ⇒ Object

Yields:

  • ("max", "Max", max)


138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/ffwd/processor/histogram.rb', line 138

def calculate bucket
  total = bucket.size

  map = {}

  @percentiles.each do |k, v|
    index = (total * v[:q]).ceil - 1

    if (c = map[index]).nil?
      info = "#{v[:info]} percentile"
      c = map[index] = {:info => info, :values => []}
    end

    c[:values] << {:name => k, :value => nil}
  end

  max = nil
  min = nil
  sum = 0.0
  mean = nil

  bucket.sort.each_with_index do |t, index|
    max = t if max.nil? or t > max
    min = t if min.nil? or t < min
    sum += t

    unless (c = map[index]).nil?
      c[:values].each{|d| d[:value] = t}
    end
  end

  mean = sum / total

  unless @precision.nil?
    max = max.round(@precision)
    min = min.round(@precision)
    sum = sum.round(@precision)
    mean = mean.round(@precision)

    map.each do |index, c|
      c[:values].each{|d| d[:value] = d[:value].round(@precision)}
    end
  end

  yield "max", "Max", max
  yield "min", "Min", min
  yield "sum", "Sum", sum
  yield "mean", "Mean", mean

  map.each do |index, c|
    c[:values].each do |d|
      yield d[:name], c[:info], d[:value]
    end
  end
end

#check_timerObject



106
107
108
109
110
111
112
113
114
115
# File 'lib/ffwd/processor/histogram.rb', line 106

def check_timer
  return if @timer

  log.debug "Starting timer"

  @timer = EM::Timer.new(@window) do
    @timer = nil
    digest!
  end
end

#digest!Object

Digest the cache.



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/ffwd/processor/histogram.rb', line 118

def digest!
  if @cache.empty?
    return
  end

  ms = FFWD.timing do
    @cache.each do |key, bucket|
      calculate(bucket) do |p, info, value|
        @emitter.metric.emit(
          :key => "#{key}.#{p}", :source => key,
          :value => value, :description => "#{info} of #{key}")
      end
    end

    @cache = {}
  end

  log.debug "Digest took #{ms}ms"
end

#process(m) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/ffwd/processor/histogram.rb', line 194

def process m
  key = m[:key]
  value = m[:value] || @missing

  if (bucket = @cache[key]).nil?
    return increment :dropped if @cache.size >= @cache_limit
    @cache[key] = bucket = []
  end

  return increment :bucket_dropped if bucket.size >= @bucket_limit
  return increment :dropped if stopped?
  increment :received

  bucket << value
  check_timer
end