[Metrics SDK] Fix hash calculation for nostd::string #2999
[Metrics SDK] Fix hash calculation for nostd::string #2999lalitb merged 22 commits intoopen-telemetry:mainfrom
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. |
| { | ||
| return true; | ||
| } | ||
| GetHash(seed, key.data()); |
There was a problem hiding this comment.
In the GetHash function, could we assert that arg should not be a pointer?
There was a problem hiding this comment.
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.
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.
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.
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
keyandvalueas 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.mdupdated for non-trivial changes