Class: OpenTelemetry::SDK::Metrics::Aggregation::ExplicitBucketHistogram

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

Overview

Constant Summary collapse

OVERFLOW_ATTRIBUTE_SET =
{ 'otel.metric.overflow' => true }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aggregation_temporality: ENV.fetch('OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE', :cumulative), boundaries: DEFAULT_BOUNDARIES, record_min_max: true, exemplar_reservoir: nil) ⇒ ExplicitBucketHistogram

The default value for boundaries represents the following buckets: (-inf, 0], (0, 5.0], (5.0, 10.0], (10.0, 25.0], (25.0, 50.0], (50.0, 75.0], (75.0, 100.0], (100.0, 250.0], (250.0, 500.0], (500.0, 1000.0], (1000.0, +inf)



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

def initialize(
  aggregation_temporality: ENV.fetch('OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE', :cumulative),
  boundaries: DEFAULT_BOUNDARIES,
  record_min_max: true,
  exemplar_reservoir: nil
)
  @aggregation_temporality = AggregationTemporality.determine_temporality(aggregation_temporality: aggregation_temporality, default: :cumulative)
  @boundaries = boundaries && !boundaries.empty? ? boundaries.sort : nil
  @record_min_max = record_min_max
  @exemplar_reservoir = exemplar_reservoir || Metrics::Exemplar::AlignedHistogramBucketExemplarReservoir.new(boundaries: @boundaries)
  @exemplar_reservoir_storage = new_stream_storage
end

Instance Attribute Details

#exemplar_reservoir ⇒ Object (readonly)

Returns the value of attribute exemplar_reservoir.



17
18
19
# File 'lib/opentelemetry/sdk/metrics/aggregation/explicit_bucket_histogram.rb', line 17

def exemplar_reservoir
  @exemplar_reservoir
end

Instance Method Details

#aggregation_temporality ⇒ Object

Returns the configured aggregation temporality (:delta or :cumulative).



81
82
83
# File 'lib/opentelemetry/sdk/metrics/aggregation/explicit_bucket_histogram.rb', line 81

def aggregation_temporality
  @aggregation_temporality.temporality
end

#collect(start_time, end_time, data_points) ⇒ Object

Returns the current histogram data points, clearing them for delta temporality.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/opentelemetry/sdk/metrics/aggregation/explicit_bucket_histogram.rb', line 40

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
      reservoir = @exemplar_reservoir_storage[data_points][hdp.attributes]
      hdp.exemplars = reservoir&.collect(attributes: hdp.attributes, aggregation_temporality: @aggregation_temporality.temporality)
      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
      reservoir = @exemplar_reservoir_storage[data_points][hdp.attributes]
      hdp.exemplars = reservoir&.collect(attributes: hdp.attributes, aggregation_temporality: @aggregation_temporality.temporality)
      hdp = hdp.dup
      hdp.bucket_counts = hdp.bucket_counts.dup
      hdp
    end
  end
end

#update(amount, attributes, data_points, cardinality_limit, exemplar_offer: false) ⇒ Object

Records amount into the histogram bucket for the given attributes.



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/opentelemetry/sdk/metrics/aggregation/explicit_bucket_histogram.rb', line 67

def update(amount, attributes, data_points, cardinality_limit, exemplar_offer: false)
  hdp = if data_points.key?(attributes)
          data_points[attributes]
        elsif data_points.size >= cardinality_limit - 1
          data_points[OVERFLOW_ATTRIBUTE_SET] || create_new_data_point(OVERFLOW_ATTRIBUTE_SET, data_points)
        else
          create_new_data_point(attributes, data_points)
        end

  update_histogram_data_point(hdp, amount, data_points, exemplar_offer: exemplar_offer)
  nil
end