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', :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



87
88
89
# File 'lib/opentelemetry/sdk/metrics/aggregation/sum.rb', line 87

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)


54
55
56
# File 'lib/opentelemetry/sdk/metrics/aggregation/sum.rb', line 54

def monotonic?
  @monotonic
end

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



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/opentelemetry/sdk/metrics/aggregation/sum.rb', line 58

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

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

  reservoir = @exemplar_reservoir_storage[attributes]
  unless reservoir
    reservoir = @exemplar_reservoir.dup
    reservoir.reset
    @exemplar_reservoir_storage[attributes] = reservoir
  end

  if exemplar_offer
    reservoir.offer(value: increment,
                    timestamp: OpenTelemetry::Common::Utilities.time_in_nanoseconds,
                    attributes: attributes,
                    context: OpenTelemetry::Context.current)
  end

  ndp.value += increment
  nil
end