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

Merged
merged 10 commits into from
Mar 5, 2025

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 91.98%. Comparing base (a9bca17) to head (27ad11a).
Report is 21 commits behind head on main.

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

❌ Your patch status 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.00%   91.98%   -0.03%     
==========================================
  Files         469      469              
  Lines       25301    25328      +27     
==========================================
+ Hits        23279    23297      +18     
- Misses       1614     1620       +6     
- Partials      408      411       +3     

☔ 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.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks. I have a feeling there is a more general solution out there, but this is pragmatic.

Part of #7455.

for i := range table.Len() {
a := table.At(i)

if a.Key() == key && value == a.Value().AsRaw() {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: I know that AsRaw() could make a copy of the data. This is a reminder that it would be nice if you could write a.Value().Equal(value) and avoid the allocation.

Copy link
Member

Choose a reason for hiding this comment

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

@dmathieu Happy to merge after you reply to this :)

Copy link
Member Author

Choose a reason for hiding this comment

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

I have opened #12561 to address this in another PR, since it requires changed in pcommon.

// 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.

Thanks. I have a feeling there is a more general solution out there, but this is pragmatic.

Part of #7455.

@mx-psi mx-psi added this pull request to the merge queue Mar 5, 2025
Merged via the queue into open-telemetry:main with commit 72a8471 Mar 5, 2025
54 of 55 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.

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