-
Notifications
You must be signed in to change notification settings - Fork 500
[SDK] custom hash and equality for attribute processor #3643
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
[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:
|
|
sdk/include/opentelemetry/sdk/metrics/view/attributes_processor.h
Outdated
Show resolved
Hide resolved
sdk/include/opentelemetry/sdk/metrics/view/attributes_processor.h
Outdated
Show resolved
Hide resolved
sdk/include/opentelemetry/sdk/metrics/view/attributes_processor.h
Outdated
Show resolved
Hide resolved
sdk/include/opentelemetry/sdk/metrics/view/attributes_processor.h
Outdated
Show resolved
Hide resolved
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 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
StringViewHash
andStringViewEqual
functors 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()}); |
Copilot
AI
Sep 19, 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.
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()}); |
Copilot uses AI. Check for mistakes.
sdk/include/opentelemetry/sdk/metrics/view/attributes_processor.h
Outdated
Show resolved
Hide resolved
sdk/include/opentelemetry/sdk/metrics/view/attributes_processor.h
Outdated
Show resolved
Hide resolved
sdk/include/opentelemetry/sdk/metrics/view/attributes_processor.h
Outdated
Show resolved
Hide resolved
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.
LGTM
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.
Please document where the new hash function comes from.
sdk/include/opentelemetry/sdk/metrics/view/attributes_processor.h
Outdated
Show resolved
Hide resolved
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.
LGTM, see minor changes.
sdk/include/opentelemetry/sdk/metrics/view/attributes_processor.h
Outdated
Show resolved
Hide resolved
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.
LGTM, thanks for the fix.
[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.md
updated for non-trivial changes