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

Add Aggregation as part of metrics SDK. #1178

Merged
merged 12 commits into from
Feb 5, 2022

Conversation

lalitb
Copy link
Member

@lalitb lalitb commented Jan 21, 2022

Fixes #1090

Changes

Splitting #1173 into smaller PRs for ease of review. This PR adds aggregation for Sum, LastValue, and Histogram.

Implementation is based on the specs here:
https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#aggregation

Please provide a brief description of the changes here.

For significant contributions please make sure you have completed the following items:

  • CHANGELOG.md updated for non-trivial changes
  • Unit tests have been added
  • Changes in public API reviewed

@lalitb lalitb requested a review from a team as a code owner January 21, 2022 00:44
@codecov
Copy link

codecov bot commented Jan 21, 2022

Codecov Report

Merging #1178 (80a710e) into main (b6a28df) will increase coverage by 0.10%.
The diff coverage is 95.70%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1178      +/-   ##
==========================================
+ Coverage   93.35%   93.44%   +0.10%     
==========================================
  Files         177      185       +8     
  Lines        6502     6720     +218     
==========================================
+ Hits         6069     6279     +210     
- Misses        433      441       +8     
Impacted Files Coverage Δ
...ry/sdk/metrics/aggregation/lastvalue_aggregation.h 0.00% <0.00%> (ø)
sdk/include/opentelemetry/sdk/metrics/view/view.h 63.64% <ø> (ø)
...lemetry/sdk/metrics/aggregation/drop_aggregation.h 25.00% <25.00%> (ø)
...ry/sdk/metrics/aggregation/histogram_aggregation.h 75.00% <75.00%> (ø)
...elemetry/sdk/metrics/aggregation/sum_aggregation.h 75.00% <75.00%> (ø)
...pentelemetry/sdk/metrics/aggregation/aggregation.h 100.00% <100.00%> (ø)
...k/src/metrics/aggregation/histogram_aggregation.cc 100.00% <100.00%> (ø)
...k/src/metrics/aggregation/lastvalue_aggregation.cc 100.00% <100.00%> (ø)
sdk/src/metrics/aggregation/sum_aggregation.cc 100.00% <100.00%> (ø)
sdk/test/metrics/aggregation_test.cc 100.00% <100.00%> (ø)
... and 2 more

public:
virtual void Aggregate(long value, const PointAttributes &attributes = {}) noexcept = 0;

virtual void Aggregate(double value, const PointAttributes &attributes = {}) noexcept = 0;
Copy link
Member Author

@lalitb lalitb Jan 21, 2022

Choose a reason for hiding this comment

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

Preferred overloaded methods over the template ( for double and long type) to avoid having header-only metrics sdk. The template use had a cascading effect here, forcing to make all inter-related classes as templates. Since this is not a customer-facing API, we have the flexibility to change it later.

name = "view_registry_test",
srcs = [
"view_registry_test.cc",
],
Copy link
Member

Choose a reason for hiding this comment

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

Would it be possible to add tags here?

Suggested change
],
],
tags = [
"metrics",
"test",
],

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks. Added it.

name = "aggregation_test",
srcs = [
"aggregation_test.cc",
],
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
],
],
tags = [
"metrics",
"test",
],

Copy link
Member Author

Choose a reason for hiding this comment

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

Added.


PointType LongLastValueAggregation::Collect() noexcept
{
if (!is_lastvalue_valid_)
Copy link
Member

Choose a reason for hiding this comment

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

Don't we need to protect is_lastvalue_valid_ here?

Copy link
Member Author

@lalitb lalitb Jan 21, 2022

Choose a reason for hiding this comment

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

Good point. I thought it should be fine to return an invalid value in given Collect() call, as long as the subsequent call would return a valid value. Worst case, we may miss a given metric (first few ones depending on collect interval) in a series of Collect() calls, but this way we can avoid a lock.

Copy link
Member

Choose a reason for hiding this comment

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

I'm afraid this would be a UB, and the lock for reading is unavoidable.

Copy link
Member Author

Choose a reason for hiding this comment

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

I thought UB should be fine as long as is_lastvalue_valid_ returns either true/false (was not expecting strong consistency here). As in one of the next few iterations, it would be true eventually. But have added the lock if it's causing confusion.

ASSERT_TRUE(nostd::holds_alternative<std::vector<long>>(histogram_data.boundaries_));
EXPECT_EQ(nostd::get<long>(histogram_data.sum_), 0);
EXPECT_EQ(histogram_data.count_, 0);
EXPECT_NO_THROW(aggr.Aggregate(12l, {})); // lies in third bucket (indexed at 0)
Copy link
Member

Choose a reason for hiding this comment

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

The comments are not correct. also in DoubleHistogramAggregation

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you mean 12 does lie in 3rd bucket? The default buckets here are: [-infinity to 0], [0 to 5], [5 to10], [10 to 25] , and so on (refer - https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#histogram-aggregation ). So 12 will lie in the third bucket.

Copy link
Member

Choose a reason for hiding this comment

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

No I mean indexed at 0 part, this should be 2 or I'm getting sth wrong here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, have only kept the bucket number now. Thanks for noticing it.

Copy link
Member

@esigo esigo left a comment

Choose a reason for hiding this comment

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

Thanks for the PR. LGTM

@esigo esigo self-requested a review January 26, 2022 18:07
{
namespace metrics
{
class InstrumentMonotonicityAwareAggregation
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems InstrumentMonotonicityAwareAggregation should be a subclass of Aggretation?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes or else we can also move this functionality within the Aggregation class. I thought of keeping it a separate class as not sure right now how to use it for Asynchronous aggregation. We can refactor this later once we have more complete implementation.

@lalitb lalitb mentioned this pull request Jan 28, 2022
3 tasks
@lalitb lalitb merged commit 44795b6 into open-telemetry:main Feb 5, 2022
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.

Metrics SDK: Add Aggregation
3 participants