refactor(bigtable): decouple metric cloning from OperationContext - #16355
refactor(bigtable): decouple metric cloning from OperationContext#16355scotthart wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request refactors metric cloning by moving the cloning logic out of OperationContext and introducing a standalone CloneMetrics helper function. This simplifies the OperationContext constructor to directly accept a vector of already-cloned metrics, and allows the removal of the CloningMetric test helper class across multiple test files. Feedback on the changes suggests replacing the assert in CloneMetrics with an explicit nullptr check to prevent potential null pointer dereferences in release builds where assertions are disabled.
| for (auto const& m : metrics) { | ||
| // We should never add a nullptr Metric to the list. | ||
| assert(m != nullptr); | ||
| auto clone = m->clone(resource_labels, data_labels); | ||
| v.emplace_back(std::move(clone)); | ||
| } |
There was a problem hiding this comment.
In release builds, assert is compiled out, which means a null pointer in metrics would lead to a null pointer dereference and undefined behavior (crash) when calling m->clone(...). To ensure robustness and adhere to defensive programming practices, we should explicitly check for nullptr before dereferencing.
for (auto const& m : metrics) {
if (m == nullptr) continue;
v.emplace_back(m->clone(resource_labels, data_labels));
}References
- Prefer defensive code, such as explicit
ok()checks, even if they seem redundant based on the current implementation of a framework, as the framework's contract may change in the future.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #16355 +/- ##
==========================================
- Coverage 92.24% 92.23% -0.01%
==========================================
Files 2227 2227
Lines 209594 209531 -63
==========================================
- Hits 193335 193268 -67
- Misses 16259 16263 +4 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This PR decouples ResourceLabels and DataLabels from the OperationContext constructor. This increases the cohesiveness and genericity of the OperationContext class. This will be useful for adding Client Schema metrics to Bigtable and required for metrics support across other libraries.