Repository navigation
[SDK] custom hash and equality for attribute processor - #3643
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3643 +/- ##
==========================================
+ Coverage 90.08% 90.10% +0.03%
==========================================
Files 220 221 +1
Lines 7110 7129 +19
==========================================
+ Hits 6404 6423 +19
Misses 706 706
🚀 New features to boost your workflow:
|
|
|
There was a problem hiding this comment.
Pull Request Overview
This PR fixes a bug in the attributes processor where nostd::string_view keys were not being properly hashed and compared when used in std::unordered_map. The issue occurred because string_view.data() may not be null-terminated, causing incorrect lookups and comparisons.
- Implements custom
StringViewHashandStringViewEqualfunctors for safe string_view operations - Adds cross-platform heterogeneous lookup helper functions for compatibility
- Updates all unordered_map declarations to use the new custom hash and equality functors
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| sdk/include/opentelemetry/sdk/metrics/view/attributes_processor.h | Adds custom hash/equality functors and updates FilteringAttributesProcessor classes to use them |
| sdk/test/metrics/attributes_processor_test.cc | Updates test unordered_map declarations to use new custom functors |
| sdk/test/metrics/sum_aggregation_test.cc | Updates test unordered_map declarations to use new custom functors |
| return std::hash<opentelemetry::nostd::string_view>{}( | ||
| opentelemetry::nostd::string_view{sv.data(), sv.size()}); |
There was a problem hiding this comment.
Creating a new string_view from an existing string_view is redundant and may not address the original issue. The problem is that string_view may not be null-terminated, but this code still uses the same data pointer. Consider using std::string construction instead: return std::hash<std::string>{}(std::string{sv.data(), sv.size()});
| return std::hash<opentelemetry::nostd::string_view>{}( | |
| opentelemetry::nostd::string_view{sv.data(), sv.size()}); | |
| // Materialize to std::string to ensure safe hashing (string_view may not be null-terminated) | |
| return std::hash<std::string>{}(std::string{sv.data(), sv.size()}); |
marcalff
left a comment
There was a problem hiding this comment.
Please document where the new hash function comes from.
[SDK] custom hash and equality for attribute processor (open-telemetry#3643)
Fixes #3635
nostd::string_view and access the value with key.data() but this may not be null terminated. So we need a custom hash and equality for the view container
Changes
Please provide a brief description of the changes here.
For significant contributions please make sure you have completed the following items:
CHANGELOG.mdupdated for non-trivial changes