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 5 commits into
base: main
Choose a base branch
from

Conversation

garoazinha
Copy link

@garoazinha garoazinha commented Jan 15, 2025

The is_monotonic flag for sum aggregation otlp model is required by the specs

The SDK should reject negative inputs to monotonic cumulative metrics, but it is not required to.

Implementations such as python and go appear to not enforce monotonicity, but the C++ and js implementations do.
js impl
C++ impl

Issue: #1783

Copy link

linux-foundation-easycla bot commented Jan 15, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@garoazinha garoazinha marked this pull request as ready for review January 15, 2025 16:57
@garoazinha garoazinha changed the title [wip] fix: adding is_monotonic flag to sum fix: adding is_monotonic flag to sum Jan 15, 2025
@@ -47,6 +48,8 @@ def update(increment, attributes, data_points)
nil
)

return if is_monotonic && increment < 0
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this suppose to be instance variable @is_monotonic?

monotonic also need to check with aggregation_temporality

For delta monotonic sums, this means the reader SHOULD expect non-negative values.
For cumulative monotonic sums, this means the reader SHOULD expect values that are not less than the previous value.

We also need to move this logic before ndp = data_points[attributes] || data_points[attributes] = NumberDataPoint.new; because update doesn't return anything; it will update direct on data_points hash.

Copy link
Author

Choose a reason for hiding this comment

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

I think checking if increment is negative fulfills the specs for cumulative sums. for delta sums it might be the case that we need to check if the sum (value + increment) is negative? only checking if increment is negative ensures the sum value is never negative but might reject some valid increments. is my logic correct?

Copy link
Contributor

@xuan-cao-swi xuan-cao-swi Jan 21, 2025

Choose a reason for hiding this comment

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

I think checking if increment is negative fulfills the specs for cumulative sums.

Yes, I think your logic is correct (also checked other implementation as your referenced).

The specification is somewhat confusing, but In this case of metrics, this means the sum is nominally increasing should reduce confusion. (Now I think the specification is actually wrong regarding "for delta" and "for cum," and they should be swapped.)

for delta sums it might be the case that we need to check if the sum (value + increment) is negative?

For delta sum, it's kind tricky because the sum will be zeroed depends on when the instrument's value got exported. I see other implementations don't care about it. I would suggest follow their approach.

@@ -42,7 +42,7 @@ def add(increment, attributes: {})
private

def default_aggregation
OpenTelemetry::SDK::Metrics::Aggregation::Sum.new
OpenTelemetry::SDK::Metrics::Aggregation::Sum.new(is_monotonic: true)
Copy link
Contributor

Choose a reason for hiding this comment

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

I was going to ask that is there any spec to define the default for monotonic, but found the spec that explain it.

@@ -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?(:is_monotonic) && aggregator.is_monotonic
Copy link
Contributor

Choose a reason for hiding this comment

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

This will return false if aggregator doesn't respond to is_monotonic. Should we use nil instead of false?

is_monotonic = aggregator.respond_to?(:is_monotonic) ? aggregator.is_monotonic : nil

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants