Class: OpenTelemetry::SDK::Metrics::Aggregation::ExponentialHistogram::Buckets

Inherits:
Object
  • Object
show all
Defined in:
lib/opentelemetry/sdk/metrics/aggregation/exponential_histogram/buckets.rb

Overview

Buckets is the fundamental building block of exponential histogram that store bucket/boundary value

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ Buckets

Returns a new instance of Buckets.



16
17
18
19
20
21
# File 'lib/opentelemetry/sdk/metrics/aggregation/exponential_histogram/buckets.rb', line 16

def initialize
  @counts = [0]
  @index_base = 0
  @index_start = 0
  @index_end = 0
end

Instance Attribute Details

#index_base ⇒ Object

Returns the value of attribute index_base.



14
15
16
# File 'lib/opentelemetry/sdk/metrics/aggregation/exponential_histogram/buckets.rb', line 14

def index_base
  @index_base
end

#index_end ⇒ Object

Returns the value of attribute index_end.



14
15
16
# File 'lib/opentelemetry/sdk/metrics/aggregation/exponential_histogram/buckets.rb', line 14

def index_end
  @index_end
end

#index_start ⇒ Object

Returns the value of attribute index_start.



14
15
16
# File 'lib/opentelemetry/sdk/metrics/aggregation/exponential_histogram/buckets.rb', line 14

def index_start
  @index_start
end

Instance Method Details

#copy_empty ⇒ Object

Returns a copy of these buckets with all counts reset to zero.



115
116
117
118
119
120
121
122
# File 'lib/opentelemetry/sdk/metrics/aggregation/exponential_histogram/buckets.rb', line 115

def copy_empty
  new_buckets = self.class.new
  new_buckets.instance_variable_set(:@counts, Array.new(@counts.size, 0))
  new_buckets.instance_variable_set(:@index_base, @index_base)
  new_buckets.instance_variable_set(:@index_start, @index_start)
  new_buckets.instance_variable_set(:@index_end, @index_end)
  new_buckets
end

#downscale(amount) ⇒ Object

Halves the bucket resolution amount times, merging adjacent buckets.



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
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/opentelemetry/sdk/metrics/aggregation/exponential_histogram/buckets.rb', line 69

def downscale(amount)
  bias = @index_base - @index_start

  if bias != 0
    @index_base = @index_start
    @counts.reverse!
    @counts = @counts[0...bias].reverse + @counts[bias..].reverse
  end

  size = 1 + @index_end - @index_start
  each = 1 << amount
  inpos = 0
  outpos = 0
  pos = @index_start

  while pos <= @index_end
    mod = pos % each
    mod += each if mod < 0

    inds = mod

    while inds < each && inpos < size
      if outpos != inpos
        @counts[outpos] += @counts[inpos]
        @counts[inpos] = 0
      end

      inpos += 1
      pos   += 1
      inds  += 1
    end

    outpos += 1
  end

  @index_start >>= amount
  @index_end >>= amount
  @index_base = @index_start
end

#get_bucket(key) ⇒ Object

Returns the count stored at the given absolute bucket index.



59
60
61
62
63
64
65
66
# File 'lib/opentelemetry/sdk/metrics/aggregation/exponential_histogram/buckets.rb', line 59

def get_bucket(key)
  bias = @index_base - @index_start

  key += @counts.size if key < bias
  key -= bias

  @counts[key]
end

#grow(needed, max_size) ⇒ Object

grow simply expand the @counts size



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/opentelemetry/sdk/metrics/aggregation/exponential_histogram/buckets.rb', line 24

def grow(needed, max_size)
  size = @counts.size
  bias = @index_base - @index_start
  old_positive_limit = size - bias

  new_size = [2**Math.log2(needed).ceil, max_size].min
  new_positive_limit = new_size - bias

  tmp = Array.new(new_size, 0)
  tmp[new_positive_limit..-1] = @counts[old_positive_limit..]
  tmp[0...old_positive_limit] = @counts[0...old_positive_limit]
  @counts = tmp
end

#increment_bucket(bucket_index, increment = 1) ⇒ Object

Increments the count at bucket_index by increment.



110
111
112
# File 'lib/opentelemetry/sdk/metrics/aggregation/exponential_histogram/buckets.rb', line 110

def increment_bucket(bucket_index, increment = 1)
  @counts[bucket_index] += increment
end

#length ⇒ Object

Returns the number of populated buckets, from index_start to index_end.



51
52
53
54
55
56
# File 'lib/opentelemetry/sdk/metrics/aggregation/exponential_histogram/buckets.rb', line 51

def length
  return 0 if @counts.empty?
  return 0 if @index_end == @index_start && counts[0] == 0

  @index_end - @index_start + 1
end

#offset ⇒ Object

Returns the lower index bound of the populated buckets.



39
40
41
# File 'lib/opentelemetry/sdk/metrics/aggregation/exponential_histogram/buckets.rb', line 39

def offset
  @index_start
end

#offset_counts ⇒ Object Also known as: counts

Returns the bucket counts, rotated so index 0 aligns with #offset.



44
45
46
47
# File 'lib/opentelemetry/sdk/metrics/aggregation/exponential_histogram/buckets.rb', line 44

def offset_counts
  bias = @index_base - @index_start
  @counts[-bias..] + @counts[0...-bias]
end