Skip to content

Commit e344c2c

Browse files
feat: add low memory to TEMPORALITY_PREFERENCE (#1886)
* feat: add low memory to TEMPORALITY_PREFERENCE * add aggregation_temporality test case * add low_memory --------- Co-authored-by: Kayla Reopelle <[email protected]>
1 parent f5bec99 commit e344c2c

File tree

9 files changed

+334
-12
lines changed

9 files changed

+334
-12
lines changed

metrics_sdk/lib/opentelemetry/sdk/metrics/aggregation/aggregation_temporality.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,31 @@ def delta
2828
def cumulative
2929
new(CUMULATIVE)
3030
end
31+
32+
# | Preference Value | Counter | Async Counter | Histogram | UpDownCounter | Async UpDownCounter |
33+
# |------------------|------------|------------------|----------- |---------------|-------------------- |
34+
# | **Cumulative** | Cumulative | Cumulative | Cumulative | Cumulative | Cumulative |
35+
# | **Delta** | Delta | Delta | Delta | Cumulative | Cumulative |
36+
# | **LowMemory** | Delta | Cumulative | Delta | Cumulative | Cumulative |
37+
def determine_temporality(aggregation_temporality: nil, instrument_kind: nil, default: nil)
38+
# aggregation_temporality can't be nil because it always has default value in symbol
39+
if aggregation_temporality.is_a?(::Symbol)
40+
aggregation_temporality == :delta ? delta : cumulative
41+
42+
elsif aggregation_temporality.is_a?(::String)
43+
case aggregation_temporality
44+
when 'LOWMEMORY', 'lowmemory', 'low_memory'
45+
instrument_kind == :observable_counter ? cumulative : delta
46+
when 'DELTA', 'delta'
47+
delta
48+
when 'CUMULATIVE', 'cumulative'
49+
cumulative
50+
else
51+
default == :delta ? delta : cumulative
52+
end
53+
54+
end
55+
end
3156
end
3257

3358
attr_reader :temporality

metrics_sdk/lib/opentelemetry/sdk/metrics/aggregation/explicit_bucket_histogram.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def initialize(
2323
boundaries: DEFAULT_BOUNDARIES,
2424
record_min_max: true
2525
)
26-
@aggregation_temporality = aggregation_temporality.to_sym == :delta ? AggregationTemporality.delta : AggregationTemporality.cumulative
26+
@aggregation_temporality = AggregationTemporality.determine_temporality(aggregation_temporality: aggregation_temporality, default: :cumulative)
2727
@boundaries = boundaries && !boundaries.empty? ? boundaries.sort : nil
2828
@record_min_max = record_min_max
2929
end

metrics_sdk/lib/opentelemetry/sdk/metrics/aggregation/exponential_bucket_histogram.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ module Metrics
1616
module Aggregation
1717
# Contains the implementation of the {https://opentelemetry.io/docs/specs/otel/metrics/data-model/#exponentialhistogram ExponentialBucketHistogram} aggregation
1818
class ExponentialBucketHistogram # rubocop:disable Metrics/ClassLength
19-
attr_reader :aggregation_temporality
20-
2119
# relate to min max scale: https://opentelemetry.io/docs/specs/otel/metrics/sdk/#support-a-minimum-and-maximum-scale
2220
DEFAULT_SIZE = 160
2321
DEFAULT_SCALE = 20
@@ -34,7 +32,7 @@ def initialize(
3432
record_min_max: true,
3533
zero_threshold: 0
3634
)
37-
@aggregation_temporality = aggregation_temporality.to_sym
35+
@aggregation_temporality = AggregationTemporality.determine_temporality(aggregation_temporality: aggregation_temporality, default: :delta)
3836
@record_min_max = record_min_max
3937
@min = Float::INFINITY
4038
@max = -Float::INFINITY
@@ -49,7 +47,7 @@ def initialize(
4947
end
5048

5149
def collect(start_time, end_time, data_points)
52-
if @aggregation_temporality == :delta
50+
if @aggregation_temporality.delta?
5351
# Set timestamps and 'move' data point values to result.
5452
hdps = data_points.values.map! do |hdp|
5553
hdp.start_time_unix_nano = start_time
@@ -168,6 +166,10 @@ def update(amount, attributes, data_points)
168166
end
169167
# rubocop:enable Metrics/MethodLength
170168

169+
def aggregation_temporality
170+
@aggregation_temporality.temporality
171+
end
172+
171173
private
172174

173175
def grow_buckets(span, buckets)

metrics_sdk/lib/opentelemetry/sdk/metrics/aggregation/sum.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ module Aggregation
1111
# Contains the implementation of the Sum aggregation
1212
# https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#sum-aggregation
1313
class Sum
14-
def initialize(aggregation_temporality: ENV.fetch('OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE', :cumulative), monotonic: false)
15-
@aggregation_temporality = aggregation_temporality.to_sym == :delta ? AggregationTemporality.delta : AggregationTemporality.cumulative
14+
def initialize(aggregation_temporality: ENV.fetch('OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE', :cumulative), monotonic: false, instrument_kind: nil)
15+
@aggregation_temporality = AggregationTemporality.determine_temporality(aggregation_temporality: aggregation_temporality, instrument_kind: instrument_kind, default: :cumulative)
1616
@monotonic = monotonic
1717
end
1818

metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/observable_counter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def observe(timeout: nil, attributes: {})
3232
private
3333

3434
def default_aggregation
35-
OpenTelemetry::SDK::Metrics::Aggregation::Sum.new(monotonic: true)
35+
OpenTelemetry::SDK::Metrics::Aggregation::Sum.new(monotonic: true, instrument_kind: instrument_kind)
3636
end
3737
end
3838
end

metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/observable_up_down_counter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def observe(timeout: nil, attributes: {})
3232
private
3333

3434
def default_aggregation
35-
OpenTelemetry::SDK::Metrics::Aggregation::Sum.new(aggregation_temporality: :delta, monotonic: false)
35+
OpenTelemetry::SDK::Metrics::Aggregation::Sum.new(aggregation_temporality: :cumulative, monotonic: false)
3636
end
3737
end
3838
end

metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/up_down_counter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def add(amount, attributes: {})
3535
private
3636

3737
def default_aggregation
38-
OpenTelemetry::SDK::Metrics::Aggregation::Sum.new(monotonic: false)
38+
OpenTelemetry::SDK::Metrics::Aggregation::Sum.new(aggregation_temporality: :cumulative, monotonic: false)
3939
end
4040
end
4141
end

0 commit comments

Comments
 (0)