Class: OpenTelemetry::SDK::Metrics::Aggregation::ExponentialHistogram::LogarithmMapping

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

Overview

LogarithmMapping for mapping when scale > 0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scale) ⇒ LogarithmMapping

Returns a new instance of LogarithmMapping.



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

def initialize(scale)
  @scale = scale
  @scale_factor = Log2eScaleFactor::LOG2E_SCALE_BUCKETS[scale] # scale_factor is used for mapping the index
  @min_normal_lower_boundary_index = IEEE754::MIN_NORMAL_EXPONENT << @scale
  @max_normal_lower_boundary_index = ((IEEE754::MAX_NORMAL_EXPONENT + 1) << @scale) - 1
end

Instance Attribute Details

#scaleObject (readonly)

Returns the value of attribute scale.



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

def scale
  @scale
end

Instance Method Details

#get_lower_boundary(inds) ⇒ Object

for testing



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/opentelemetry/sdk/metrics/aggregation/exponential_histogram/logarithm_mapping.rb', line 35

def get_lower_boundary(inds)
  if inds >= @max_normal_lower_boundary_index
    return 2 * Math.exp((inds - (1 << @scale)) / @scale_factor) if inds == @max_normal_lower_boundary_index

    raise StandardError, 'mapping overflow'
  end

  if inds <= @min_normal_lower_boundary_index
    return IEEE754::MIN_NORMAL_VALUE if inds == @min_normal_lower_boundary_index
    return Math.exp((inds + (1 << @scale)) / @scale_factor) / 2 if inds == @min_normal_lower_boundary_index - 1

    raise StandardError, 'mapping underflow'
  end

  Math.exp(inds / @scale_factor)
end

#map_to_index(value) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/opentelemetry/sdk/metrics/aggregation/exponential_histogram/logarithm_mapping.rb', line 23

def map_to_index(value)
  return @min_normal_lower_boundary_index - 1 if value <= IEEE754::MIN_NORMAL_VALUE

  if IEEE754.get_ieee_754_mantissa(value) == 0
    exponent = IEEE754.get_ieee_754_exponent(value)
    return (exponent << @scale) - 1
  end

  [(Math.log(value) * @scale_factor).floor, @max_normal_lower_boundary_index].min
end