Skip to content
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

introduce pprofile.AddAttribute helper #12390

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

dmathieu
Copy link
Member

@dmathieu dmathieu commented Feb 14, 2025

Description

This introduces the pprofile.AddAttribute helper method so profile extensions can modify attributes.
Closes #12206

BenchmarkAddAttribute
BenchmarkAddAttribute/with_a_new_string_attribute
BenchmarkAddAttribute/with_a_new_string_attribute-10            30461164                39.55 ns/op           32 B/op          2 allocs/op
BenchmarkAddAttribute/with_an_existing_attribute
BenchmarkAddAttribute/with_an_existing_attribute-10             30714298                39.44 ns/op           32 B/op          2 allocs/op
BenchmarkAddAttribute/with_a_duplicate_attribute
BenchmarkAddAttribute/with_a_duplicate_attribute-10             29132547                39.47 ns/op           32 B/op          2 allocs/op
BenchmarkAddAttribute/with_a_hundred_attributes_to_loop_through
BenchmarkAddAttribute/with_a_hundred_attributes_to_loop_through-10               6762793               176.8 ns/op            32 B/op          2 allocs/op

Those benchmarks kind of have an N+1 issue, where the method will take longer the more attributes there are.
However, since the attribute table is a map, we can't go around with looping through it.

I also had to use FromRaw and AsRaw in there, to properly handle any values.

This currently doesn't support removing entries. But doing so would also have a non-trivial performance impact, since we'd have to loop through the AttributeIndices slice.

Copy link

codecov bot commented Feb 14, 2025

Codecov Report

Attention: Patch coverage is 66.66667% with 9 lines in your changes missing coverage. Please review.

Project coverage is 92.07%. Comparing base (dae5d19) to head (8e2ff1b).
Report is 12 commits behind head on main.

Files with missing lines Patch % Lines
pdata/pprofile/attributes.go 66.66% 6 Missing and 3 partials ⚠️

❌ Your patch check has failed because the patch coverage (66.66%) is below the target coverage (95.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #12390      +/-   ##
==========================================
- Coverage   92.19%   92.07%   -0.13%     
==========================================
  Files         465      465              
  Lines       25285    25200      -85     
==========================================
- Hits        23311    23202     -109     
- Misses       1575     1598      +23     
- Partials      399      400       +1     

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

@dmathieu dmathieu marked this pull request as ready for review February 14, 2025 09:47
@dmathieu dmathieu requested review from mx-psi and a team as code owners February 14, 2025 09:47
@dmathieu
Copy link
Member Author

@bogdandrutu I'm happy to hear your input on improvements here, especially with the loop through all attributes issue, and the use of FromRaw and AsRaw to be able to have an any value.

// AddAttribute updates an AttributeTable and a record's AttributeIndices to
// add a new attribute.
// The record can by any struct that implements an `AttributeIndices` method.
func AddAttribute(table AttributeTableSlice, record attributable, key string, value any) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

How come the attributable type is not an exported type (of some package)?

Copy link
Member Author

Choose a reason for hiding this comment

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

attributable is there to allow the behavior on any of the generated types that have attribute indices.
Location, Mapping, Sample.
Folks will only ever need to manipulate the generated struct. They don't need the interface.

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.

[pdata/pprofile] Add helper method to allow modifying a struct's attributes
3 participants