Class: OpenTelemetry::SDK::Metrics::Exemplar::SimpleFixedSizeExemplarReservoir

Inherits:
ExemplarReservoir
  • Object
show all
Defined in:
lib/opentelemetry/sdk/metrics/exemplar/simple_fixed_size_exemplar_reservoir.rb

Overview

SimpleFixedSizeExemplarReservoir uses a uniformly-weighted sampling algorithm based on the number of samples the reservoir has seen so far to determine if the offered measurements should be sampled

Constant Summary collapse

DEFAULT_SIZE =

Default to number of CPUs for better concurrent performance, fallback to 1

begin
  Etc.nprocessors
rescue StandardError
  1
end

Instance Method Summary collapse

Constructor Details

#initialize(max_size: nil) ⇒ SimpleFixedSizeExemplarReservoir

Returns a new instance of SimpleFixedSizeExemplarReservoir.



22
23
24
25
# File 'lib/opentelemetry/sdk/metrics/exemplar/simple_fixed_size_exemplar_reservoir.rb', line 22

def initialize(max_size: nil)
  @max_size = max_size || DEFAULT_SIZE
  reset
end

Instance Method Details

#collect(attributes: nil, aggregation_temporality: nil) ⇒ Object

Reset measurement counter on collection for delta temporality



37
38
39
40
41
42
43
44
45
# File 'lib/opentelemetry/sdk/metrics/exemplar/simple_fixed_size_exemplar_reservoir.rb', line 37

def collect(attributes: nil, aggregation_temporality: nil)
  exemplars = []
  @exemplar_buckets.each do |bucket|
    exemplars << bucket.collect(point_attributes: attributes)
  end
  reset if aggregation_temporality == :delta
  exemplars.compact!
  exemplars
end

#find_histogram_bucketObject



52
53
54
# File 'lib/opentelemetry/sdk/metrics/exemplar/simple_fixed_size_exemplar_reservoir.rb', line 52

def find_histogram_bucket
  @num_measurements_seen < @max_size ? @num_measurements_seen : rand(0..@num_measurements_seen - 1)
end

#offer(value: nil, timestamp: nil, attributes: nil, context: nil) ⇒ Object

Uses a uniformly-weighted sampling algorithm based on the number of samples the reservoir has seen Fill the buckets array first then randomly override existing bucket



29
30
31
32
33
34
# File 'lib/opentelemetry/sdk/metrics/exemplar/simple_fixed_size_exemplar_reservoir.rb', line 29

def offer(value: nil, timestamp: nil, attributes: nil, context: nil)
  bucket_index = find_histogram_bucket
  @exemplar_buckets[bucket_index].offer(value: value, time_unix_nano: timestamp, attributes: attributes, context: context) if bucket_index < @max_size
  @num_measurements_seen += 1
  nil
end

#resetObject



47
48
49
50
# File 'lib/opentelemetry/sdk/metrics/exemplar/simple_fixed_size_exemplar_reservoir.rb', line 47

def reset
  @exemplar_buckets = Array.new(@max_size) { ExemplarBucket.new }
  @num_measurements_seen = 0
end