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

Inherits:
Object
  • Object
show all
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.



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

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 = {}
end

Instance Attribute Details

#exemplar_reservoirObject (readonly)

Returns the value of attribute exemplar_reservoir.



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

def exemplar_reservoir
  @exemplar_reservoir
end

Instance Method Details

#aggregation_temporalityObject



74
75
76
# File 'lib/opentelemetry/sdk/metrics/aggregation/sum.rb', line 74

def aggregation_temporality
  @aggregation_temporality.temporality
end

#collect(start_time, end_time, data_points) ⇒ Object



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

def collect(start_time, end_time, 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 = @exemplar_reservoir_storage[ndp.attributes]
      ndp.exemplars = reservoir&.collect(attributes: ndp.attributes, aggregation_temporality: @aggregation_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 = @exemplar_reservoir_storage[ndp.attributes]
      ndp.exemplars = reservoir&.collect(attributes: ndp.attributes, aggregation_temporality: @aggregation_temporality)
      ndp.dup
    end
  end
end

#monotonic?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/opentelemetry/sdk/metrics/aggregation/sum.rb', line 70

def monotonic?
  @monotonic
end

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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/opentelemetry/sdk/metrics/aggregation/sum.rb', line 54

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, exemplar_offer: exemplar_offer)
  nil
end