Skip to content

Commit 5ada394

Browse files
fix: add kind enum type for aggregation_temporality that only allow two types (#1769)
* fix: add kind enum type for aggregation_temporality that only allow two types * update the doc for aggregation_temporality * force drop aggregation temp to nil * test case update --------- Co-authored-by: Kayla Reopelle <[email protected]>
1 parent e71e1bc commit 5ada394

File tree

19 files changed

+247
-44
lines changed

19 files changed

+247
-44
lines changed

exporter/otlp-metrics/test/opentelemetry/exporter/otlp/metrics/metrics_exporter_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@
652652
)
653653
],
654654
is_monotonic: true,
655-
aggregation_temporality: Opentelemetry::Proto::Metrics::V1::AggregationTemporality::AGGREGATION_TEMPORALITY_DELTA
655+
aggregation_temporality: Opentelemetry::Proto::Metrics::V1::AggregationTemporality::AGGREGATION_TEMPORALITY_CUMULATIVE
656656
)
657657
),
658658
Opentelemetry::Proto::Metrics::V1::Metric.new(
@@ -672,7 +672,7 @@
672672
)
673673
],
674674
is_monotonic: false,
675-
aggregation_temporality: Opentelemetry::Proto::Metrics::V1::AggregationTemporality::AGGREGATION_TEMPORALITY_DELTA
675+
aggregation_temporality: Opentelemetry::Proto::Metrics::V1::AggregationTemporality::AGGREGATION_TEMPORALITY_CUMULATIVE
676676
)
677677
),
678678
Opentelemetry::Proto::Metrics::V1::Metric.new(
@@ -696,7 +696,7 @@
696696
max: 10
697697
)
698698
],
699-
aggregation_temporality: Opentelemetry::Proto::Metrics::V1::AggregationTemporality::AGGREGATION_TEMPORALITY_DELTA
699+
aggregation_temporality: Opentelemetry::Proto::Metrics::V1::AggregationTemporality::AGGREGATION_TEMPORALITY_CUMULATIVE
700700
)
701701
),
702702
Opentelemetry::Proto::Metrics::V1::Metric.new(

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module Aggregation
1515
end
1616
end
1717

18+
require 'opentelemetry/sdk/metrics/aggregation/aggregation_temporality'
1819
require 'opentelemetry/sdk/metrics/aggregation/number_data_point'
1920
require 'opentelemetry/sdk/metrics/aggregation/histogram_data_point'
2021
require 'opentelemetry/sdk/metrics/aggregation/explicit_bucket_histogram'
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright The OpenTelemetry Authors
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
7+
module OpenTelemetry
8+
module SDK
9+
module Metrics
10+
module Aggregation
11+
# AggregationTemporality represents the temporality of
12+
# data point ({NumberDataPoint} and {HistogramDataPoint}) in {Metrics}.
13+
# It determine whether the data point will be cleared for each metrics pull/export.
14+
class AggregationTemporality
15+
class << self
16+
private :new
17+
18+
# Returns a newly created {AggregationTemporality} with temporality == DELTA
19+
#
20+
# @return [AggregationTemporality]
21+
def delta
22+
new(DELTA)
23+
end
24+
25+
# Returns a newly created {AggregationTemporality} with temporality == CUMULATIVE
26+
#
27+
# @return [AggregationTemporality]
28+
def cumulative
29+
new(CUMULATIVE)
30+
end
31+
end
32+
33+
attr_reader :temporality
34+
35+
# @api private
36+
# The constructor is private and only for use internally by the class.
37+
# Users should use the {delta} and {cumulative} factory methods to obtain
38+
# a {AggregationTemporality} instance.
39+
#
40+
# @param [Integer] temporality One of the status codes below
41+
def initialize(temporality)
42+
@temporality = temporality
43+
end
44+
45+
def delta?
46+
@temporality == :delta
47+
end
48+
49+
def cumulative?
50+
@temporality == :cumulative
51+
end
52+
53+
# delta: data point will be cleared after each metrics pull/export.
54+
DELTA = :delta
55+
56+
# cumulative: data point will NOT be cleared after metrics pull/export.
57+
CUMULATIVE = :cumulative
58+
end
59+
end
60+
end
61+
end
62+
end

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@ module Metrics
1010
module Aggregation
1111
# Contains the implementation of the Drop aggregation
1212
class Drop
13-
attr_reader :aggregation_temporality
14-
15-
def initialize(aggregation_temporality: :delta)
16-
@aggregation_temporality = aggregation_temporality
13+
def initialize
14+
@aggregation_temporality = nil
1715
end
1816

1917
def collect(start_time, end_time, data_points)
@@ -30,6 +28,10 @@ def update(increment, attributes, data_points)
3028
)
3129
nil
3230
end
31+
32+
def aggregation_temporality
33+
nil
34+
end
3335
end
3436
end
3537
end

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,22 @@ class ExplicitBucketHistogram
1414
DEFAULT_BOUNDARIES = [0, 5, 10, 25, 50, 75, 100, 250, 500, 1000].freeze
1515
private_constant :DEFAULT_BOUNDARIES
1616

17-
attr_reader :aggregation_temporality
18-
1917
# The default value for boundaries represents the following buckets:
2018
# (-inf, 0], (0, 5.0], (5.0, 10.0], (10.0, 25.0], (25.0, 50.0],
2119
# (50.0, 75.0], (75.0, 100.0], (100.0, 250.0], (250.0, 500.0],
2220
# (500.0, 1000.0], (1000.0, +inf)
2321
def initialize(
24-
aggregation_temporality: ENV.fetch('OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE', :delta), # TODO: the default should be :cumulative, see issue #1555
22+
aggregation_temporality: ENV.fetch('OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE', :cumulative),
2523
boundaries: DEFAULT_BOUNDARIES,
2624
record_min_max: true
2725
)
28-
@aggregation_temporality = aggregation_temporality.to_sym
26+
@aggregation_temporality = aggregation_temporality.to_sym == :delta ? AggregationTemporality.delta : AggregationTemporality.cumulative
2927
@boundaries = boundaries && !boundaries.empty? ? boundaries.sort : nil
3028
@record_min_max = record_min_max
3129
end
3230

3331
def collect(start_time, end_time, data_points)
34-
if @aggregation_temporality == :delta
32+
if @aggregation_temporality.delta?
3533
# Set timestamps and 'move' data point values to result.
3634
hdps = data_points.values.map! do |hdp|
3735
hdp.start_time_unix_nano = start_time
@@ -87,6 +85,10 @@ def update(amount, attributes, data_points)
8785
nil
8886
end
8987

88+
def aggregation_temporality
89+
@aggregation_temporality.temporality
90+
end
91+
9092
private
9193

9294
def empty_bucket_counts

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@ module Metrics
1010
module Aggregation
1111
# Contains the implementation of the LastValue aggregation
1212
class LastValue
13-
attr_reader :aggregation_temporality
14-
1513
def initialize(aggregation_temporality: :delta)
16-
@aggregation_temporality = aggregation_temporality
14+
@aggregation_temporality = aggregation_temporality == :cumulative ? AggregationTemporality.cumulative : AggregationTemporality.delta
1715
end
1816

1917
def collect(start_time, end_time, data_points)
20-
if @aggregation_temporality == :delta
18+
if @aggregation_temporality.delta?
2119
# Set timestamps and 'move' data point values to result.
2220
ndps = data_points.values.map! do |ndp|
2321
ndp.start_time_unix_nano = start_time
@@ -46,6 +44,10 @@ def update(increment, attributes, data_points)
4644
)
4745
nil
4846
end
47+
48+
def aggregation_temporality
49+
@aggregation_temporality.temporality
50+
end
4951
end
5052
end
5153
end

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,13 @@ 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-
attr_reader :aggregation_temporality
15-
16-
def initialize(aggregation_temporality: ENV.fetch('OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE', :delta), monotonic: false)
17-
# TODO: the default should be :cumulative, see issue #1555
18-
@aggregation_temporality = aggregation_temporality.to_sym
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
1916
@monotonic = monotonic
2017
end
2118

2219
def collect(start_time, end_time, data_points)
23-
if @aggregation_temporality == :delta
20+
if @aggregation_temporality.delta?
2421
# Set timestamps and 'move' data point values to result.
2522
ndps = data_points.values.map! do |ndp|
2623
ndp.start_time_unix_nano = start_time
@@ -57,6 +54,10 @@ def update(increment, attributes, data_points)
5754
ndp.value += increment
5855
nil
5956
end
57+
58+
def aggregation_temporality
59+
@aggregation_temporality.temporality
60+
end
6061
end
6162
end
6263
end

metrics_sdk/test/integration/in_memory_metric_pull_exporter_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
_(last_snapshot[0].data_points[3].value).must_equal(4)
4848
_(last_snapshot[0].data_points[3].attributes).must_equal('d' => 'e')
4949

50-
_(last_snapshot[0].aggregation_temporality).must_equal(:delta)
50+
_(last_snapshot[0].aggregation_temporality).must_equal(:cumulative)
5151
end
5252
end
5353
end

metrics_sdk/test/integration/periodic_metric_reader_test.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,15 @@
99

1010
describe OpenTelemetry::SDK do
1111
describe '#periodic_metric_reader' do
12-
before { reset_metrics_sdk }
12+
before do
13+
reset_metrics_sdk
14+
@original_temp = ENV['OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE']
15+
ENV['OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE'] = 'delta'
16+
end
17+
18+
after do
19+
ENV['OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE'] = @original_temp
20+
end
1321

1422
# OTLP cannot export a metric without data points
1523
it 'does not export metrics without data points' do

metrics_sdk/test/opentelemetry/sdk/metrics/aggregation/drop_test.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,19 @@
66

77
require 'test_helper'
88

9-
describe OpenTelemetry::SDK::Metrics::Aggregation::LastValue do
9+
describe OpenTelemetry::SDK::Metrics::Aggregation::Drop do
1010
let(:data_points) { {} }
11-
let(:drop_aggregation) { OpenTelemetry::SDK::Metrics::Aggregation::Drop.new(aggregation_temporality:) }
11+
let(:drop_aggregation) { OpenTelemetry::SDK::Metrics::Aggregation::Drop.new }
1212
let(:aggregation_temporality) { :delta }
1313

1414
# Time in nano
1515
let(:start_time) { (Time.now.to_r * 1_000_000_000).to_i }
1616
let(:end_time) { ((Time.now + 60).to_r * 1_000_000_000).to_i }
1717

18+
describe '#initialize' do
19+
# drop aggregation doesn't care about aggregation_temporality since all data will be dropped
20+
end
21+
1822
it 'sets the timestamps' do
1923
drop_aggregation.update(0, {}, data_points)
2024
ndp = drop_aggregation.collect(start_time, end_time, data_points)[0]

0 commit comments

Comments
 (0)