-
Notifications
You must be signed in to change notification settings - Fork 440
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
[Metrics SDK] Fix hash calculation for nostd::string #2999
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #2999 +/- ##
==========================================
+ Coverage 87.12% 87.66% +0.55%
==========================================
Files 200 190 -10
Lines 6109 5858 -251
==========================================
- Hits 5322 5135 -187
+ Misses 787 723 -64
|
…pp into fix-overflow-hash
Is there a way to make sure that
Maybe instantiate the GenHash template explicitly for all string types that matter. |
@@ -71,7 +71,7 @@ inline size_t GetHashForAttributeMap( | |||
{ | |||
return true; | |||
} | |||
GetHash(seed, key.data()); |
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.
In the GetHash
function, could we assert that arg
should not be a pointer?
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.
We can add it, but since this is an internal API, it might not be necessary as long as we ensure it's used correctly.
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.
Thinking again, we do need to support the GetHash
function with char pointer, as the AttributeValue enum contains it.
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.
If we do need supporter pointer, perhaps create a dedicated specialization for it? Then a difference hash function could be applied to pointers.
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.
yes, it is done in the latest commit.
I believe we need template specialization for |
bazel-asan is failing. Will check that- //sdk/test/metrics:attributes_hashmap_test FAILED in 0.5s |
LGTM, thanks for the fix. |
Fixes #2997
Changes
Step 1:
Added test to validate the hash creation issue, where the Hash is being calculated on the address of key , and not it's value as below:
TEST(AttributesHashMap, HashWithKeyValueIterable)
Most existing tests directly use
key
andvalue
as literals, and these literals share a common address from the read-only data segment. This masked the issue, and it is also the most common use case where metric attributes are statically created and used during measurement recording.CI failure - https://github.com/open-telemetry/opentelemetry-cpp/actions/runs/9870060037/job/27255004557?pr=2999
Step 2:
The fix is to replace the hash calculation:
GetHash(seed, key.data());
with
GetHash(seed, key);
and so directly use the custom hash implementation for nostd::string_view. Though it comes with a cost, as this custom hash-implementation creates an intermediate string. This needs to be revisited.
For significant contributions please make sure you have completed the following items:
CHANGELOG.md
updated for non-trivial changes