Skip to content

Commit

Permalink
feat: Add aggregation_temporality to MetricData (#1554)
Browse files Browse the repository at this point in the history
* feat: Add aggregation_temporality to MetricData

Currently, @aggregation_temporality is set for Sum and
ExplicitBucketHistorgram, but is not passed to MetricData,
so it cannot reach the exporter. Now, @aggregation_temporality is
included in MetricData and can be set using
OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE.

* Update comment for rubocop

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

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

* feat: Add attr_reader for aggregation_temporality

---------

Co-authored-by: Robert <[email protected]>
  • Loading branch information
kaylareopelle and robertlaurin authored Feb 6, 2024
1 parent c7ef408 commit 3fde77b
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ class ExplicitBucketHistogram
DEFAULT_BOUNDARIES = [0, 5, 10, 25, 50, 75, 100, 250, 500, 1000].freeze
private_constant :DEFAULT_BOUNDARIES

attr_reader :aggregation_temporality

# The default value for boundaries represents the following buckets:
# (-inf, 0], (0, 5.0], (5.0, 10.0], (10.0, 25.0], (25.0, 50.0],
# (50.0, 75.0], (75.0, 100.0], (100.0, 250.0], (250.0, 500.0],
# (500.0, 1000.0], (1000.0, +inf)
def initialize(
aggregation_temporality: :delta,
aggregation_temporality: ENV.fetch('OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE', :delta), # TODO: the default should be :cumulative, see issue #1555
boundaries: DEFAULT_BOUNDARIES,
record_min_max: true
)
Expand Down
5 changes: 4 additions & 1 deletion metrics_sdk/lib/opentelemetry/sdk/metrics/aggregation/sum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ module Aggregation
# Contains the implementation of the Sum aggregation
# https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#sum-aggregation
class Sum
def initialize(aggregation_temporality: :delta)
attr_reader :aggregation_temporality

def initialize(aggregation_temporality: ENV.fetch('OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE', :delta))
# TODO: the default should be :cumulative, see issue #1555
@aggregation_temporality = aggregation_temporality
@data_points = {}
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module State
:resource, # OpenTelemetry::SDK::Resources::Resource
:instrumentation_scope, # OpenTelemetry::SDK::InstrumentationScope
:data_points, # Hash{Hash{String => String, Numeric, Boolean, Array<String, Numeric, Boolean>} => Numeric}
:aggregation_temporality, # Symbol
:start_time_unix_nano, # Integer nanoseconds since Epoch
:time_unix_nano) # Integer nanoseconds since Epoch
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def collect(start_time, end_time)
@meter_provider.resource,
@instrumentation_scope,
@aggregation.collect(start_time, end_time),
@aggregation.aggregation_temporality,
start_time,
end_time
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@

_(last_snapshot[0].data_points[3].value).must_equal(4)
_(last_snapshot[0].data_points[3].attributes).must_equal('d' => 'e')

_(last_snapshot[0].aggregation_temporality).must_equal(:delta)
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@
_(last_snapshot[0].instrumentation_scope.name).must_equal('test')
_(last_snapshot[0].data_points[0].value).must_equal(1)
_(last_snapshot[0].data_points[0].attributes).must_equal('foo' => 'bar')
_(last_snapshot[0].aggregation_temporality).must_equal(:delta)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@
_(last_snapshot[0].data_points[0].max).must_equal(6)
_(last_snapshot[0].data_points[0].bucket_counts).must_equal([0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0])
_(last_snapshot[0].data_points[0].attributes).must_equal('foo' => 'bar')
_(last_snapshot[0].aggregation_temporality).must_equal(:delta)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@
_(last_snapshot[0].instrumentation_scope.name).must_equal('test')
_(last_snapshot[0].data_points[0].attributes).must_equal('foo' => 'bar')
_(last_snapshot[0].data_points[0].value).must_equal(-1)
_(last_snapshot[0].aggregation_temporality).must_equal(:delta)
end
end

0 comments on commit 3fde77b

Please sign in to comment.