-
Notifications
You must be signed in to change notification settings - Fork 505
add validation in MetricCollector constructor and enhance related tests #3724
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
base: main
Are you sure you want to change the base?
add validation in MetricCollector constructor and enhance related tests #3724
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3724 +/- ##
==========================================
+ Coverage 90.00% 90.02% +0.02%
==========================================
Files 225 225
Lines 7099 7102 +3
==========================================
+ Hits 6389 6393 +4
+ Misses 710 709 -1
🚀 New features to boost your workflow:
|
|
OK, I understand you disabled exceptions. But, there is a lot of functions which may throw. Some of them even have noexcept, means it simply crashes. The first thing missing, if no function/even no constructor must throw, to add |
- make constructor noexcept - check parameters and log errors accordingly - catch possibly thrown exceptions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR improves error handling and robustness in the MetricCollector class by adding validation for null pointers and exception handling. The constructor now validates input parameters, and the Produce() method wraps metric collection in try-catch blocks to handle exceptions gracefully.
Key changes:
- Added null checks for
contextandmetric_readerparameters in constructor and methods - Added exception handling in
Produce()method'sForEachMetercallback - Modified test helper to return
shared_ptrinstead ofweak_ptrfor simpler test code - Added new test case to verify invalid argument handling
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 7 comments.
| File | Description |
|---|---|
sdk/src/metrics/state/metric_collector.cc |
Added null pointer validation in constructor, GetAggregationTemporality, ForceFlush, and Shutdown; added try-catch blocks in Produce() method |
sdk/include/opentelemetry/sdk/metrics/state/metric_collector.h |
Marked constructor as noexcept |
sdk/test/metrics/metric_collector_test.cc |
Changed test helper return type from weak_ptr to shared_ptr, removed noexcept from helper, added new test for invalid arguments, updated all test usages |
| OTEL_INTERNAL_LOG_ERROR("[MetricCollector::MetricCollector] - Error constructing MetricCollector." | ||
| << "The context is invalid"); | ||
| if (metric_reader_) | ||
| metric_reader_->SetMetricProducer(this); | ||
| else | ||
| OTEL_INTERNAL_LOG_ERROR("[MetricCollector::MetricCollector] - Error constructing MetricCollector." |
Copilot
AI
Oct 29, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing space between the error message and the stream insertion. Should be: \"Error constructing MetricCollector. \" (with trailing space) for proper formatting.
| OTEL_INTERNAL_LOG_ERROR("[MetricCollector::MetricCollector] - Error constructing MetricCollector." | |
| << "The context is invalid"); | |
| if (metric_reader_) | |
| metric_reader_->SetMetricProducer(this); | |
| else | |
| OTEL_INTERNAL_LOG_ERROR("[MetricCollector::MetricCollector] - Error constructing MetricCollector." | |
| OTEL_INTERNAL_LOG_ERROR("[MetricCollector::MetricCollector] - Error constructing MetricCollector. " | |
| << "The context is invalid"); | |
| if (metric_reader_) | |
| metric_reader_->SetMetricProducer(this); | |
| else | |
| OTEL_INTERNAL_LOG_ERROR("[MetricCollector::MetricCollector] - Error constructing MetricCollector. " |
| if (metric_reader_) | ||
| metric_reader_->SetMetricProducer(this); | ||
| else | ||
| OTEL_INTERNAL_LOG_ERROR("[MetricCollector::MetricCollector] - Error constructing MetricCollector." |
Copilot
AI
Oct 29, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing space between the error message and the stream insertion. Should be: \"Error constructing MetricCollector. \" (with trailing space) for proper formatting.
| OTEL_INTERNAL_LOG_ERROR("[MetricCollector::MetricCollector] - Error constructing MetricCollector." | |
| OTEL_INTERNAL_LOG_ERROR("[MetricCollector::MetricCollector] - Error constructing MetricCollector. " |
| { | ||
| if (!metric_reader_) | ||
| { | ||
| OTEL_INTERNAL_LOG_ERROR("[MetricCollector::GetAggregationTemporality] - Error getting aggregation temporality." |
Copilot
AI
Oct 29, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing space between the error message and the stream insertion. Should be: \"Error getting aggregation temporality. \" (with trailing space) for proper formatting.
| OTEL_INTERNAL_LOG_ERROR("[MetricCollector::GetAggregationTemporality] - Error getting aggregation temporality." | |
| OTEL_INTERNAL_LOG_ERROR("[MetricCollector::GetAggregationTemporality] - Error getting aggregation temporality. " |
| catch (const std::exception &e) | ||
| { | ||
| resource_metrics.scope_metric_data_.emplace_back(std::move(filtered_scope_metrics)); | ||
| OTEL_INTERNAL_LOG_ERROR("[MetricCollector::Collect] - Error during collecting." |
Copilot
AI
Oct 29, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing space between the error message and the stream insertion. Should be: \"Error during collecting. \" (with trailing space) for proper formatting.
| OTEL_INTERNAL_LOG_ERROR("[MetricCollector::Collect] - Error during collecting." | |
| OTEL_INTERNAL_LOG_ERROR("[MetricCollector::Collect] - Error during collecting. " |
| } | ||
| catch (...) | ||
| { | ||
| OTEL_INTERNAL_LOG_ERROR("[MetricCollector::Collect] - Error during collecting." |
Copilot
AI
Oct 29, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing space between the error message and the stream insertion. Should be: \"Error during collecting. \" (with trailing space) for proper formatting.
| OTEL_INTERNAL_LOG_ERROR("[MetricCollector::Collect] - Error during collecting." | |
| OTEL_INTERNAL_LOG_ERROR("[MetricCollector::Collect] - Error during collecting. " |
| { | ||
| if (!metric_reader_) | ||
| { | ||
| OTEL_INTERNAL_LOG_ERROR("[MetricCollector::ForceFlush] - Error accessing meter_reader_." |
Copilot
AI
Oct 29, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing space between the error message and the stream insertion. Should be: \"Error accessing meter_reader_. \" (with trailing space) for proper formatting.
| OTEL_INTERNAL_LOG_ERROR("[MetricCollector::ForceFlush] - Error accessing meter_reader_." | |
| OTEL_INTERNAL_LOG_ERROR("[MetricCollector::ForceFlush] - Error accessing meter_reader_. " |
| { | ||
| if (!metric_reader_) | ||
| { | ||
| OTEL_INTERNAL_LOG_ERROR("[MetricCollector::Shutdown] - Error accessing meter_reader_." |
Copilot
AI
Oct 29, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing space between the error message and the stream insertion. Should be: \"Error accessing meter_reader_. \" (with trailing space) for proper formatting.
| OTEL_INTERNAL_LOG_ERROR("[MetricCollector::Shutdown] - Error accessing meter_reader_." | |
| OTEL_INTERNAL_LOG_ERROR("[MetricCollector::Shutdown] - Error accessing meter_reader_. " |
Public-facing APIs: Return real or no-op objects, shouldn't be nullptr ideally. But, exceptions are not thrown. p.s btw sorry for adding copilot for review. didn't realize it's still draft. |
Can you ensure that? E.g. auto meter = prov->CreateMeter(...);
...->AddMeter(std::move(meter));Where should the validation be? CreateMeter() could return the NoopMeter in case new fails. Should that be done? And AddMeter() could get a nullptr, aeven in Release, a check just in Debug won't be enough. Currently its not checked on output nor input. Proposals to make the code safer:
|
Fixes # (issue)
an access violation in constructor of MetricProducer when giving nullptr metric_reader.
Changes
Need clarification first:
Is it allowed to throw exceptions in constructor at least? Currently I see a lot of classes allowing nullptr without checking early. This leads to objects having an invalid state, while member functions do nothing except spamming the log, that a required member is not set correctly.