class Spectator::Histogram::PercentileDistributionSummary

Distribution summary that buckets the counts to allow for estimating percentiles. This distribution summary type will track the data distribution for the summary by maintaining a set of counters. The distribution can then be used on the server side to estimate percentiles while still allowing for arbitrary slicing and dicing based on dimensions.

Percentile distribution summaries are expensive compared to basic distribution summaries from the registry. In particular they have a higher storage cost, worst case ~300x, to maintain the data distribution. Be diligent about any additional dimensions added to percentile distribution summaries and ensure they have a small bounded cardinality. In addition it is highly recommended to set a threshold (using the min and max parameters in the constructor) whenever possible to greatly restrict the worst case overhead.

Public Class Methods

new(registry, name, tags = nil, min = 0, max = MAX_VALUE) click to toggle source
# File lib/spectator/histogram/percentiles.rb, line 512
def initialize(registry, name, tags = nil, min = 0, max = MAX_VALUE)
  @registry = registry
  @id = Spectator::MeterId.new(name, tags)
  @min = min
  @max = max
  @ds = registry.distribution_summary_with_id(@id)
  @counters = PercentileBuckets.counters(registry, @id, 'D')
end

Public Instance Methods

count() click to toggle source
# File lib/spectator/histogram/percentiles.rb, line 538
def count
  @ds.count
end
percentile(perc) click to toggle source

return the given percentile

# File lib/spectator/histogram/percentiles.rb, line 529
def percentile(perc)
  counts = @counters.map(&:count)
  PercentileBuckets.percentile(counts, perc)
end
record(amount) click to toggle source
# File lib/spectator/histogram/percentiles.rb, line 521
def record(amount)
  @ds.record(amount)
  restricted = restrict(amount)
  idx = PercentileBuckets.index_of(restricted)
  @counters[idx].increment
end
total_amount() click to toggle source
# File lib/spectator/histogram/percentiles.rb, line 534
def total_amount
  @ds.total_amount
end

Private Instance Methods

restrict(nanos) click to toggle source
# File lib/spectator/histogram/percentiles.rb, line 544
def restrict(nanos)
  nanos = @max if nanos > @max
  nanos = @min if nanos < @min
  nanos.floor
end