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

#initializeBuckets

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_baseObject

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_endObject

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_startObject

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

#downscale(amount) ⇒ Object



65
66
67
68
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
# File 'lib/opentelemetry/sdk/metrics/aggregation/exponential_histogram/buckets.rb', line 65

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



56
57
58
59
60
61
62
63
# File 'lib/opentelemetry/sdk/metrics/aggregation/exponential_histogram/buckets.rb', line 56

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
37
# 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



105
106
107
# File 'lib/opentelemetry/sdk/metrics/aggregation/exponential_histogram/buckets.rb', line 105

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

#lengthObject



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

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

  @index_end - @index_start + 1
end

#offsetObject



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

def offset
  @index_start
end

#offset_countsObject Also known as: counts



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

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