Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: adding is_monotonic flag to sum #1793

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,8 @@ def as_otlp_metrics(metrics)
aggregation_temporality: as_otlp_aggregation_temporality(metrics.aggregation_temporality),
data_points: metrics.data_points.map do |ndp|
number_data_point(ndp)
end
end,
is_monotonic: metrics.is_monotonic
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,9 @@
counter = meter.create_counter('test_counter', unit: 'smidgen', description: 'a small amount of something')
counter.add(5, attributes: { 'foo' => 'bar' })

up_down_counter = meter.create_up_down_counter('test_up_down_counter', unit: 'smidgen', description: 'a small amount of something')
up_down_counter.add(5, attributes: { 'foo' => 'bar' })

histogram = meter.create_histogram('test_histogram', unit: 'smidgen', description: 'a small amount of something')
histogram.record(10, attributes: { 'oof' => 'rab' })

Expand Down Expand Up @@ -623,6 +626,27 @@
exemplars: nil
)
],
is_monotonic: true,
aggregation_temporality: Opentelemetry::Proto::Metrics::V1::AggregationTemporality::AGGREGATION_TEMPORALITY_DELTA
)
),
Opentelemetry::Proto::Metrics::V1::Metric.new(
name: 'test_up_down_counter',
description: 'a small amount of something',
unit: 'smidgen',
sum: Opentelemetry::Proto::Metrics::V1::Sum.new(
data_points: [
Opentelemetry::Proto::Metrics::V1::NumberDataPoint.new(
attributes: [
Opentelemetry::Proto::Common::V1::KeyValue.new(key: 'foo', value: Opentelemetry::Proto::Common::V1::AnyValue.new(string_value: 'bar'))
],
as_int: 5,
start_time_unix_nano: 1_699_593_427_329_946_585,
time_unix_nano: 1_699_593_427_329_946_586,
exemplars: nil
)
],
is_monotonic: false,
aggregation_temporality: Opentelemetry::Proto::Metrics::V1::AggregationTemporality::AGGREGATION_TEMPORALITY_DELTA
)
),
Expand Down
1 change: 1 addition & 0 deletions metrics_sdk/.tool-versions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@garoazinha, would you mind removing this file? I think it snuck in on the last commit.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ruby 3.3.0
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ module Aggregation
class Sum
attr_reader :aggregation_temporality

def initialize(aggregation_temporality: ENV.fetch('OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE', :delta))
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

def collect(start_time, end_time, data_points)
Expand All @@ -38,7 +39,13 @@ def collect(start_time, end_time, data_points)
end
end

def monotonic?
@monotonic
end

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

ndp = data_points[attributes] || data_points[attributes] = NumberDataPoint.new(
attributes,
nil,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def add(increment, attributes: {})
private

def default_aggregation
OpenTelemetry::SDK::Metrics::Aggregation::Sum.new
OpenTelemetry::SDK::Metrics::Aggregation::Sum.new(monotonic: true)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def add(amount, attributes: {})
private

def default_aggregation
OpenTelemetry::SDK::Metrics::Aggregation::Sum.new
OpenTelemetry::SDK::Metrics::Aggregation::Sum.new(monotonic: false)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ module State
: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
:time_unix_nano, # Integer nanoseconds since Epoch
:is_monotonic) # Boolean
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ def update(value, attributes)

def aggregate_metric_data(start_time, end_time, aggregation: nil)
aggregator = aggregation || @default_aggregation
is_monotonic = aggregator.respond_to?(:monotonic?) ? aggregator.monotonic? : nil

MetricData.new(
@name,
@description,
Expand All @@ -77,7 +79,8 @@ def aggregate_metric_data(start_time, end_time, aggregation: nil)
aggregator.collect(start_time, end_time, @data_points),
aggregator.aggregation_temporality,
start_time,
end_time
end_time,
is_monotonic
)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

describe OpenTelemetry::SDK::Metrics::Aggregation::Sum do
let(:data_points) { {} }
let(:sum_aggregation) { OpenTelemetry::SDK::Metrics::Aggregation::Sum.new(aggregation_temporality: aggregation_temporality) }
let(:sum_aggregation) { OpenTelemetry::SDK::Metrics::Aggregation::Sum.new(aggregation_temporality: aggregation_temporality, monotonic: monotonic) }
let(:aggregation_temporality) { :delta }
let(:monotonic) { false }

# Time in nano
let(:start_time) { (Time.now.to_r * 1_000_000_000).to_i }
Expand Down Expand Up @@ -58,6 +59,14 @@
_(ndps[1].attributes).must_equal('foo' => 'bar')
end

it 'aggregates and collects negative values' do
sum_aggregation.update(1, {}, data_points)
sum_aggregation.update(-2, {}, data_points)

ndps = sum_aggregation.collect(start_time, end_time, data_points)
_(ndps[0].value).must_equal(-1)
end

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may want more test case once the logic for mixing up temporality and monotonic is there

it 'does not aggregate between collects' do
sum_aggregation.update(1, {}, data_points)
sum_aggregation.update(2, {}, data_points)
Expand Down Expand Up @@ -94,4 +103,17 @@
_(ndps[0].value).must_equal(4)
end
end

describe 'when sum type is monotonic' do
let(:aggregation_temporality) { :not_delta }
let(:monotonic) { true }

it 'does not allow negative values to accumulate' do
sum_aggregation.update(1, {}, data_points)
sum_aggregation.update(-2, {}, data_points)
ndps = sum_aggregation.collect(start_time, end_time, data_points)

_(ndps[0].value).must_equal(1)
end
end
end
Loading