-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Changes from all commits
1c1404f
3f7d437
689b5d9
d13c3b7
dc78efb
75645a3
8e2ff1b
dc0f9bd
3d94f64
27ad11a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: enhancement | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) | ||
component: pdata/pprofile | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Introduce AddAttribute helper method to modify the content of attributable records | ||
|
||
# One or more tracking issues or pull requests related to the change | ||
issues: [12206] | ||
|
||
# (Optional) One or more lines of additional information to render under the primary note. | ||
# These lines will be padded with 2 spaces and then inserted directly into the document. | ||
# Use pipe (|) for multiline entries. | ||
subtext: | ||
|
||
# Optional: The change log or logs in which this entry should be included. | ||
# e.g. '[user]' or '[user, api]' | ||
# Include 'user' if the change is relevant to end users. | ||
# Include 'api' if there is a change to a library API. | ||
# Default: '[user]' | ||
change_logs: [api] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,10 @@ | |
package pprofile // import "go.opentelemetry.io/collector/pdata/pprofile" | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"math" | ||
|
||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
) | ||
|
||
|
@@ -26,3 +30,41 @@ | |
|
||
return m | ||
} | ||
|
||
// 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 { | ||
for i := range table.Len() { | ||
a := table.At(i) | ||
|
||
if a.Key() == key && value == a.Value().AsRaw() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I know that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dmathieu Happy to merge after you reply to this :) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
if i >= math.MaxInt32 { | ||
return fmt.Errorf("Attribute %s=%#v has too high an index to be added to AttributeIndices", key, value) | ||
} | ||
|
||
for j := range record.AttributeIndices().Len() { | ||
v := record.AttributeIndices().At(j) | ||
if v == int32(i) { //nolint:gosec // overflow checked | ||
return nil | ||
} | ||
} | ||
|
||
record.AttributeIndices().Append(int32(i)) //nolint:gosec // overflow checked | ||
dmathieu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return nil | ||
} | ||
} | ||
|
||
if table.Len() >= math.MaxInt32 { | ||
dmathieu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return errors.New("AttributeTable can't take more attributes") | ||
} | ||
table.EnsureCapacity(table.Len() + 1) | ||
entry := table.AppendEmpty() | ||
entry.SetKey(key) | ||
if err := entry.Value().FromRaw(value); err != nil { | ||
return err | ||
} | ||
record.AttributeIndices().Append(int32(table.Len()) - 1) //nolint:gosec // overflow checked | ||
|
||
return nil | ||
} |
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.
How come the
attributable
type is not an exported type (of some package)?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.
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.
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. I have a feeling there is a more general solution out there, but this is pragmatic.
Part of #7455.