Skip to content

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

Merged
merged 3 commits into from
May 18, 2025

Conversation

hendriknielaender
Copy link
Contributor

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.

  • Modified Attributes.HashContext.hash to sort attributes by key before hashing
  • Added a fast path for small numbers of attributes (≤8) to avoid allocation

Testing

Added a new test case InstrumentationScope hash should be consistent regardless of attribute order that verifies:

  • Hash values are equal for the same attributes in different orders
  • The eql method correctly identifies equivalent attribute sets

closes #48

Copy link
Collaborator

@inge4pres inge4pres left a 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") });
Copy link
Collaborator

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.

Copy link
Contributor Author

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" {
Copy link
Collaborator

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 🙏🏼

Comment on lines 130 to 131
// If there are few attributes, just hash them directly to avoid allocation
if (attrs.len <= 8) {
Copy link
Collaborator

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?

Copy link
Contributor Author

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

@inge4pres inge4pres self-requested a review May 18, 2025 12:24
Copy link
Collaborator

@inge4pres inge4pres left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well done! 👍🏼
Thanks 🙏🏼

@inge4pres inge4pres merged commit 1ab1b0a into zig-o11y:main May 18, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Scope: is the order of attributes producing a different scope?
2 participants