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 => {:percentage => 0.50, :info => "50th"},
  :p75 => {:percentage => 0.75, :info => "75th"},
  :p95 => {:percentage => 0.95, :info => "95th"},
  :p99 => {:percentage => 0.99, :info => "99th"},
  :p999 => {:percentage => 0.999, :info => "99.9th"},
}

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, opts = {}) ⇒ 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", :percentage => 0.1}, ...}


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/ffwd/processor/histogram.rb', line 70

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

  @window = opts[:window] || 10
  @cache_limit = opts[:cache_limit] || 1000
  @bucket_limit = opts[:bucket_limit] || 10000
  @precision = opts[:precision] || 3
  @missing = opts[:missing] || DEFAULT_MISSING
  @percentiles = opts[:percentiles] || DEFAULT_PERCENTILES

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

  starting do
    log.info "Starting histogram processor on a window of #{@window}s"
  end

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

Instance Method Details

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

Yields:

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


127
128
129
130
131
132
133
134
135
136
137
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
# File 'lib/ffwd/processor/histogram.rb', line 127

def calculate bucket
  total = bucket.size

  map = {}

  @percentiles.each do |k, v|
    index = (total * v[:percentage]).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



95
96
97
98
99
100
101
102
103
104
# File 'lib/ffwd/processor/histogram.rb', line 95

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.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/ffwd/processor/histogram.rb', line 107

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



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/ffwd/processor/histogram.rb', line 183

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