Skip to content

Fix bug preventing accurate reporting of dropped attribute count #7142

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 2 commits into from
Feb 26, 2025

Conversation

jack-berg
Copy link
Member

Fixes #7135

@jack-berg jack-berg requested a review from a team as a code owner February 24, 2025 17:09
@@ -47,10 +46,10 @@ public static AttributesMap create(long capacity, int lengthLimit) {
/** Add the attribute key value pair, applying capacity and length limits. */
public <T> void put(AttributeKey<T> key, T value) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Because AttributeMap extends HashMap, a caller was accidentally bypassing this method and calling Map#put(T, V) directly, which was preventing the application of attribute limits.

We protect against this by delegating to HashMap instead of extending.

@@ -495,7 +496,10 @@ public ReadWriteSpan recordException(Throwable exception, Attributes additionalA
attributes.put(EXCEPTION_STACKTRACE, stackTrace);
}

additionalAttributes.forEach(attributes::put);
Copy link
Member Author

Choose a reason for hiding this comment

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

This was the problematic call which was bypassing attribute limits.

Copy link
Member

@trask trask left a comment

Choose a reason for hiding this comment

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

(I suspect extending may have been intentional as a performance optimization to avoid the 4 bytes allocated to the delegate field)

Copy link

codecov bot commented Feb 24, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.87%. Comparing base (b5daeb0) to head (115f39e).
Report is 7 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #7142      +/-   ##
============================================
+ Coverage     89.85%   89.87%   +0.02%     
- Complexity     6614     6624      +10     
============================================
  Files           740      740              
  Lines         19991    20007      +16     
  Branches       1964     1968       +4     
============================================
+ Hits          17962    17982      +20     
+ Misses         1439     1438       -1     
+ Partials        590      587       -3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jack-berg
Copy link
Member Author

I suspect extending may have been intentional as a performance optimization to avoid the 4 bytes allocated to the delegate field

The thought crossed my mind. We could keep extending, and override the implementation of all unsupported map methods with versions that throw exceptions.

@jack-berg
Copy link
Member Author

We could keep extending, and override the implementation of all unsupported map methods with versions that throw exceptions.

This doesn't really work. A bunch of stuff breaks when you override the base map methods with versions that throw. You very quickly get into territory of asking "why is this even a map", and the only answer is "performance".

The choices are:

  • Fix this case where we bypass the limits, but continue to extend HashMap and possibly encounter similar issues in the future. Slightly less memory allocated for basically every span.
  • Delegate to HashMap instead of extending, which makes it easy to verify that no caller is bypassing the limits. Slightly more memory allocated for basically every span.

@jack-berg
Copy link
Member Author

The choices are:

  • Fix this case where we bypass the limits, but continue to extend HashMap and possibly encounter similar issues in the future. Slightly less memory allocated for basically every span.
  • Delegate to HashMap instead of extending, which makes it easy to verify that no caller is bypassing the limits. Slightly more memory allocated for basically every span.

I pushed a commit which reverts to extending HashMap. I added some javadoc calling attention to the potential pitfalls, and adjusted the put method signature @Override the super method, making it less error prone.

Given that its an internal class and changing to a delegation model would increase memory allocation, continuing to extend HashMap and fixing all internal callers seems to be the conservative approach.

@jack-berg jack-berg merged commit 3d3bff5 into open-telemetry:main Feb 26, 2025
27 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.

droppedAttributesCount is sometimes negative, but the spec requires uint32
2 participants