Class: OpenTelemetry::SDK::Metrics::Aggregation::ExponentialBucketHistogram

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

Overview

Contains the implementation of the ExponentialBucketHistogram aggregation

Constant Summary collapse

MAX_SCALE =
20
MIN_SCALE =
-10
MAX_SIZE =
160

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aggregation_temporality: ENV.fetch('OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE', :delta), max_size: MAX_SIZE, max_scale: MAX_SCALE, record_min_max: true, zero_threshold: 0) ⇒ ExponentialBucketHistogram

The default boundaries are calculated based on default max_size and max_scale values



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/opentelemetry/sdk/metrics/aggregation/exponential_bucket_histogram.rb', line 27

def initialize(
  aggregation_temporality: ENV.fetch('OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE', :delta),
  max_size: MAX_SIZE,
  max_scale: MAX_SCALE,
  record_min_max: true,
  zero_threshold: 0
)
  @aggregation_temporality = aggregation_temporality
  @record_min_max = record_min_max
  @min            = Float::INFINITY
  @max            = -Float::INFINITY
  @sum            = 0
  @count          = 0
  @zero_threshold = zero_threshold
  @zero_count     = 0
  @size           = validate_size(max_size)
  @scale          = validate_scale(max_scale)

  @mapping = new_mapping(@scale)
end

Instance Attribute Details

#aggregation_temporalityObject (readonly)

rubocop:disable Metrics/ClassLength



19
20
21
# File 'lib/opentelemetry/sdk/metrics/aggregation/exponential_bucket_histogram.rb', line 19

def aggregation_temporality
  @aggregation_temporality
end

Instance Method Details

#collect(start_time, end_time, data_points) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/opentelemetry/sdk/metrics/aggregation/exponential_bucket_histogram.rb', line 48

def collect(start_time, end_time, data_points)
  if @aggregation_temporality == :delta
    # Set timestamps and 'move' data point values to result.
    hdps = data_points.values.map! do |hdp|
      hdp.start_time_unix_nano = start_time
      hdp.time_unix_nano = end_time
      hdp
    end
    data_points.clear
    hdps
  else
    # Update timestamps and take a snapshot.
    data_points.values.map! do |hdp|
      hdp.start_time_unix_nano ||= start_time # Start time of a data point is from the first observation.
      hdp.time_unix_nano = end_time
      hdp = hdp.dup
      hdp.positive = hdp.positive.dup
      hdp.negative = hdp.negative.dup
      hdp
    end
  end
end

#update(amount, attributes, data_points) ⇒ Object

rubocop:disable Metrics/MethodLength



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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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
# File 'lib/opentelemetry/sdk/metrics/aggregation/exponential_bucket_histogram.rb', line 72

def update(amount, attributes, data_points)
  # fetch or initialize the ExponentialHistogramDataPoint
  hdp = data_points.fetch(attributes) do
    if @record_min_max
      min = Float::INFINITY
      max = -Float::INFINITY
    end

    data_points[attributes] = ExponentialHistogramDataPoint.new(
      attributes,
      nil,                                                               # :start_time_unix_nano
      0,                                                                 # :time_unix_nano
      0,                                                                 # :count
      0,                                                                 # :sum
      @scale,                                                            # :scale
      @zero_count,                                                       # :zero_count
      ExponentialHistogram::Buckets.new,  # :positive
      ExponentialHistogram::Buckets.new,  # :negative
      0,                                                                 # :flags
      nil,                                                               # :exemplars
      min,                                                               # :min
      max,                                                               # :max
      @zero_threshold # :zero_threshold)
    )
  end

  # Start to populate the data point (esp. the buckets)
  if @record_min_max
    hdp.max = amount if amount > hdp.max
    hdp.min = amount if amount < hdp.min
  end

  hdp.sum += amount
  hdp.count += 1

  if amount.abs <= @zero_threshold
    hdp.zero_count += 1
    hdp.scale = 0 if hdp.count == hdp.zero_count # if always getting zero, then there is no point to keep doing the update
    return
  end

  # rescale, map to index, update the buckets here
  buckets = amount.positive? ? hdp.positive : hdp.negative
  amount = -amount if amount.negative?

  bucket_index = @mapping.map_to_index(amount)

  rescaling_needed = false
  low = high = 0

  if buckets.counts == [0] # special case of empty
    buckets.index_start = bucket_index
    buckets.index_end   = bucket_index
    buckets.index_base  = bucket_index

  elsif bucket_index < buckets.index_start && (buckets.index_end - bucket_index) >= @size
    rescaling_needed = true
    low = bucket_index
    high = buckets.index_end

  elsif bucket_index > buckets.index_end && (bucket_index - buckets.index_start) >= @size
    rescaling_needed = true
    low = buckets.index_start
    high = bucket_index
  end

  if rescaling_needed
    scale_change = get_scale_change(low, high)
    downscale(scale_change, hdp.positive, hdp.negative)
    new_scale = @mapping.scale - scale_change
    hdp.scale = new_scale
    @mapping = new_mapping(new_scale)
    bucket_index = @mapping.map_to_index(amount)

    OpenTelemetry.logger.debug "Rescaled with new scale #{new_scale} from #{low} and #{high}; bucket_index is updated to #{bucket_index}"
  end

  # adjust buckets based on the bucket_index
  if bucket_index < buckets.index_start
    span = buckets.index_end - bucket_index
    grow_buckets(span, buckets)
    buckets.index_start = bucket_index
  elsif bucket_index > buckets.index_end
    span = bucket_index - buckets.index_start
    grow_buckets(span, buckets)
    buckets.index_end = bucket_index
  end

  bucket_index -= buckets.index_base
  bucket_index += buckets.counts.size if bucket_index.negative?

  buckets.increment_bucket(bucket_index)
  nil
end