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

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

Overview

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aggregation_temporality: ENV.fetch('OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE', :delta), monotonic: false) ⇒ Sum

Returns a new instance of Sum.



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

def initialize(aggregation_temporality: ENV.fetch('OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE', :delta), monotonic: false)
  # TODO: the default should be :cumulative, see issue #1555
  @aggregation_temporality = aggregation_temporality.to_sym
  @monotonic = monotonic
end

Instance Attribute Details

#aggregation_temporalityObject (readonly)

Returns the value of attribute aggregation_temporality.



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

def aggregation_temporality
  @aggregation_temporality
end

Instance Method Details

#collect(start_time, end_time, data_points) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/opentelemetry/sdk/metrics/aggregation/sum.rb', line 22

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
      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
      ndp.dup
    end
  end
end

#monotonic?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/opentelemetry/sdk/metrics/aggregation/sum.rb', line 42

def monotonic?
  @monotonic
end

#update(increment, attributes, data_points) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/opentelemetry/sdk/metrics/aggregation/sum.rb', line 46

def update(increment, attributes, data_points)
  return if @monotonic && increment < 0

  ndp = data_points[attributes] || data_points[attributes] = NumberDataPoint.new(
    attributes,
    nil,
    nil,
    0,
    nil
  )

  ndp.value += increment
  nil
end