Class: OpenTelemetry::SDK::Metrics::Aggregation::Sum

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

Overview

Contains the implementation of the Sum aggregation

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), monotonic: false, instrument_kind: nil, exemplar_reservoir: nil) ⇒ Sum

Returns a new instance of Sum.



22
23
24
25
26
27
28
29
30
# File 'lib/opentelemetry/sdk/metrics/aggregation/sum.rb', line 22

def initialize(aggregation_temporality: ENV.fetch('OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE', :cumulative),
               monotonic: false,
               instrument_kind: nil,
               exemplar_reservoir: nil)
  @aggregation_temporality = AggregationTemporality.determine_temporality(aggregation_temporality: aggregation_temporality, instrument_kind: instrument_kind, default: :cumulative)
  @monotonic = monotonic
  @exemplar_reservoir = exemplar_reservoir || DEFAULT_RESERVOIR
  @exemplar_reservoir_storage = new_stream_storage
end

Instance Attribute Details

#exemplar_reservoir ⇒ Object (readonly)

Returns the value of attribute exemplar_reservoir.



16
17
18
# File 'lib/opentelemetry/sdk/metrics/aggregation/sum.rb', line 16

def exemplar_reservoir
  @exemplar_reservoir
end

Instance Method Details

#aggregation_temporality ⇒ Object

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



82
83
84
# File 'lib/opentelemetry/sdk/metrics/aggregation/sum.rb', line 82

def aggregation_temporality
  @aggregation_temporality.temporality
end

#collect(start_time, end_time, data_points) ⇒ Object

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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/opentelemetry/sdk/metrics/aggregation/sum.rb', line 33

def collect(start_time, end_time, data_points)
  stream_exemplar_reservoir_storage = @exemplar_reservoir_storage[data_points]

  if @aggregation_temporality.delta?
    # Set timestamps and 'move' data point values to result.
    ndps = data_points.values.map! do |ndp|
      ndp.start_time_unix_nano = start_time
      ndp.time_unix_nano = end_time
      reservoir = stream_exemplar_reservoir_storage[ndp.attributes]
      ndp.exemplars = reservoir&.collect(attributes: ndp.attributes, aggregation_temporality: @aggregation_temporality.temporality)
      ndp
    end
    data_points.clear
    ndps
  else
    # Update timestamps and take a snapshot.
    data_points.values.map! do |ndp|
      ndp.start_time_unix_nano ||= start_time # Start time of a data point is from the first observation.
      ndp.time_unix_nano = end_time
      reservoir = stream_exemplar_reservoir_storage[ndp.attributes]
      ndp.exemplars = reservoir&.collect(attributes: ndp.attributes, aggregation_temporality: @aggregation_temporality.temporality)
      ndp.dup
    end
  end
end

#monotonic? ⇒ Boolean

Returns whether the sum is required to be non-decreasing.

Returns:

  • (Boolean)


77
78
79
# File 'lib/opentelemetry/sdk/metrics/aggregation/sum.rb', line 77

def monotonic?
  @monotonic
end

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

Adds increment to the sum for the given attributes.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/opentelemetry/sdk/metrics/aggregation/sum.rb', line 60

def update(increment, attributes, data_points, cardinality_limit, exemplar_offer: false)
  return if @monotonic && increment < 0

  # Check if we already have this attribute set
  ndp = 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_number_data_point(ndp, increment, data_points, exemplar_offer: exemplar_offer)
  nil
end