-
Notifications
You must be signed in to change notification settings - Fork 4
fix: add sort before hashing attributes #51
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
Conversation
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.
Thanks a lot for this contribution 🙏🏼 loved the added test
I left a couple of questions, addressing one of which will fix the failing tests in CI.
src/scope.zig
Outdated
test "InstrumentationScope hash should be consistent regardless of attribute order" { | ||
const allocator = std.testing.allocator; | ||
|
||
const attrs1 = try Attributes.from(allocator, .{ @as([]const u8, "key1"), @as(u64, 42), @as([]const u8, "key2"), true, @as([]const u8, "key3"), @as([]const u8, "value3") }); |
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.
Do we need type coercion with @as
for the keys?
In other tests using Attributes
we don't have to use 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.
True, adjusted this one to be similar to the other test cases.
@@ -91,3 +91,21 @@ test "InstrumentationScope should equal correctly" { | |||
|
|||
try std.testing.expect(hashContext.eql(first, second)); | |||
} | |||
|
|||
test "InstrumentationScope hash should be consistent regardless of attribute order" { |
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.
Thanks for adding the test 🙏🏼
src/attributes.zig
Outdated
// If there are few attributes, just hash them directly to avoid allocation | ||
if (attrs.len <= 8) { |
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.
This is an interesting approach.
Out of curiosity: why did you choose 8?
Is there a way to keep the whole sorting on the stack and never allocate?
The length of attributes is already known via self.attributes.len
, so even reserving space on the stack for a sorted list should be doable?
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.
Oh yeah that was my first iteration of this one, and wasn't really familiar with the opentelemetry sdk specs. So i was just setting this to a low number.
Adjusted the implementation to handle this on the stack, and using now the max default as described here: https://opentelemetry.io/docs/specs/otel/common/#configurable-parameters
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.
Well done! 👍🏼
Thanks 🙏🏼
Previously, the hashing mechanism did not account for the order of attributes, leading to inconsistent hash values for the same set of attributes in different orders. This could cause issues when using
InstrumentationScope
as a key in hash maps, as equivalent scopes with different attribute orders would produce different hash values.Attributes.HashContext.hash
to sort attributes by key before hashingTesting
Added a new test case
InstrumentationScope hash should be consistent regardless of attribute order
that verifies:closes #48