-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[pkg/ottl]: Create ctxprofilecommon for common attribute handling #42107
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
Changes from 7 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
69c7ebd
[pkg/ottl]: Create ctxprofilecommon for common attribute handling in …
florianl 6d45863
Merge branch 'main' into ctxprofilecommon
florianl ec31f28
fixup: drop custom implementation
florianl ce99a24
fix helpers
florianl 6ff3878
Merge branch 'main' into ctxprofilecommon
florianl 4855129
Merge branch 'main' into ctxprofilecommon
florianl 102cb5f
Merge branch 'main' into ctxprofilecommon
florianl 7d7fd87
Merge branch 'main' into ctxprofilecommon
florianl c7affe2
fixup: add tests for AccessAttributesKey
florianl 769c651
fixup formatting
florianl 410b674
fix linter
florianl bf8eb59
Merge branch 'main' into ctxprofilecommon
florianl 44e915e
Remove context dependency example
edmocosta a317590
fix tests
florianl 2436210
Merge branch 'main' into ctxprofilecommon
florianl b9e1957
fixup: add comment from https://github.com/open-telemetry/opentelemet…
florianl 4bc73fa
Merge branch 'main' into ctxprofilecommon
florianl b7b69da
Merge branch 'main' into ctxprofilecommon
florianl 52b1cc4
Merge branch 'main' into ctxprofilecommon
florianl 54170d6
tests: verify that existing attributes are preserved
florianl 5ad0df8
Merge branch 'main' into ctxprofilecommon
edmocosta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # 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. filelogreceiver) | ||
| component: pkg/ottl | ||
|
|
||
| # A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
| note: Create ctxprofilecommon for common attribute handling in various profiling sub messages | ||
|
|
||
| # Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
| issues: [42107] | ||
|
|
||
| # (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: | ||
|
|
||
| # If your change doesn't affect end users or the exported elements of any package, | ||
| # you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
| # 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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package ctxprofilecommon // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxprofilecommon" | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "go.opentelemetry.io/collector/pdata/pcommon" | ||
| "go.opentelemetry.io/collector/pdata/pprofile" | ||
|
|
||
| "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" | ||
| "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxutil" | ||
| ) | ||
|
|
||
| type ProfileAttributeContext interface { | ||
| AttributeIndices() pcommon.Int32Slice | ||
| GetProfilesDictionary() pprofile.ProfilesDictionary | ||
| } | ||
|
|
||
| func AccessAttributes[K ProfileAttributeContext]() ottl.StandardGetSetter[K] { | ||
| return ottl.StandardGetSetter[K]{ | ||
| Getter: func(_ context.Context, tCtx K) (any, error) { | ||
| return pprofile.FromAttributeIndices(tCtx.GetProfilesDictionary().AttributeTable(), tCtx), nil | ||
| }, | ||
| Setter: func(_ context.Context, tCtx K, val any) error { | ||
| m, err := ctxutil.GetMap(val) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| tCtx.AttributeIndices().FromRaw([]int32{}) | ||
| for k, v := range m.All() { | ||
| if err := pprofile.PutAttribute(tCtx.GetProfilesDictionary().AttributeTable(), tCtx, k, v); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| return nil | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func AccessAttributesKey[K ProfileAttributeContext](key []ottl.Key[K]) ottl.StandardGetSetter[K] { | ||
| return ottl.StandardGetSetter[K]{ | ||
| Getter: func(ctx context.Context, tCtx K) (any, error) { | ||
| return ctxutil.GetMapValue[K](ctx, tCtx, | ||
| pprofile.FromAttributeIndices(tCtx.GetProfilesDictionary().AttributeTable(), tCtx), key) | ||
| }, | ||
| Setter: func(ctx context.Context, tCtx K, val any) error { | ||
| newKey, err := ctxutil.GetMapKeyName(ctx, tCtx, key[0]) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| v := getAttributeValue(tCtx, *newKey) | ||
| if err := ctxutil.SetIndexableValue[K](ctx, tCtx, v, val, key[1:]); err != nil { | ||
| return err | ||
| } | ||
| return pprofile.PutAttribute(tCtx.GetProfilesDictionary().AttributeTable(), tCtx, *newKey, v) | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func getAttributeValue[K ProfileAttributeContext](tCtx K, key string) pcommon.Value { | ||
| // Find the index of the attribute in the profile's attribute indices | ||
| // and return the corresponding value from the attribute table. | ||
| table := tCtx.GetProfilesDictionary().AttributeTable() | ||
| indices := tCtx.AttributeIndices().AsRaw() | ||
|
|
||
| for _, tableIndex := range indices { | ||
| attr := table.At(int(tableIndex)) | ||
| if attr.Key() == key { | ||
| v := pcommon.NewValueEmpty() | ||
| attr.Value().CopyTo(v) | ||
| return v | ||
| } | ||
| } | ||
|
|
||
| return pcommon.NewValueEmpty() | ||
| } |
117 changes: 117 additions & 0 deletions
117
pkg/ottl/contexts/internal/ctxprofilecommon/attributes_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package ctxprofilecommon // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxprofilecommon" | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "go.opentelemetry.io/collector/pdata/pcommon" | ||
| "go.opentelemetry.io/collector/pdata/pprofile" | ||
| ) | ||
|
|
||
| // Mock implementations for AttributeContext and dependencies | ||
|
|
||
| type mockAttributeContext struct { | ||
| indices pcommon.Int32Slice | ||
| dictionary pprofile.ProfilesDictionary | ||
| } | ||
|
|
||
| func (m *mockAttributeContext) AttributeIndices() pcommon.Int32Slice { | ||
| return m.indices | ||
| } | ||
|
|
||
| func (m *mockAttributeContext) GetProfilesDictionary() pprofile.ProfilesDictionary { | ||
| return m.dictionary | ||
| } | ||
|
|
||
| func TestAccessAttributes_Getter(t *testing.T) { | ||
| dict := pprofile.NewProfilesDictionary() | ||
| attrTable := dict.AttributeTable() | ||
| attr1 := attrTable.AppendEmpty() | ||
| attr1.SetKey("foo") | ||
| attr1.Value().SetStr("bar") | ||
| attr2 := attrTable.AppendEmpty() | ||
| attr2.SetKey("baz") | ||
| attr2.Value().SetInt(42) | ||
|
|
||
| indices := pcommon.NewInt32Slice() | ||
| indices.Append(0) | ||
| indices.Append(1) | ||
|
|
||
| ctx := &mockAttributeContext{ | ||
| indices: indices, | ||
| dictionary: dict, | ||
| } | ||
|
|
||
| getSetter := AccessAttributes[*mockAttributeContext]() | ||
|
|
||
| got, err := getSetter.Getter(t.Context(), ctx) | ||
| assert.NoError(t, err) | ||
|
|
||
| m, ok := got.(pcommon.Map) | ||
| assert.True(t, ok) | ||
|
|
||
| fooValue, ok := m.Get("foo") | ||
| assert.True(t, ok) | ||
| assert.Equal(t, "bar", fooValue.Str()) | ||
|
|
||
| bazValue, ok := m.Get("baz") | ||
| assert.True(t, ok) | ||
| assert.Equal(t, int64(42), bazValue.Int()) | ||
| } | ||
|
|
||
| func TestAccessAttributes_Setter(t *testing.T) { | ||
florianl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| dict := pprofile.NewProfilesDictionary() | ||
| attrTable := dict.AttributeTable() | ||
| indices := pcommon.NewInt32Slice() | ||
|
|
||
| ctx := &mockAttributeContext{ | ||
| indices: indices, | ||
| dictionary: dict, | ||
| } | ||
|
|
||
| getSetter := AccessAttributes[*mockAttributeContext]() | ||
|
|
||
| // Prepare map to set | ||
| m := pcommon.NewMap() | ||
| m.PutStr("alpha", "beta") | ||
| m.PutInt("num", 123) | ||
|
|
||
| err := getSetter.Setter(t.Context(), ctx, m) | ||
| assert.NoError(t, err) | ||
|
|
||
| // Check that attributes were set in the table | ||
| foundAlpha := false | ||
| foundNum := false | ||
| for i := 0; i < attrTable.Len(); i++ { | ||
florianl marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| attr := attrTable.At(i) | ||
| if attr.Key() == "alpha" { | ||
| foundAlpha = true | ||
| assert.Equal(t, "beta", attr.Value().Str()) | ||
| } | ||
| if attr.Key() == "num" { | ||
| foundNum = true | ||
| assert.Equal(t, int64(123), attr.Value().Int()) | ||
| } | ||
| } | ||
| assert.True(t, foundAlpha) | ||
| assert.True(t, foundNum) | ||
| } | ||
|
|
||
| func TestAccessAttributes_Setter_InvalidValue(t *testing.T) { | ||
| dict := pprofile.NewProfilesDictionary() | ||
| indices := pcommon.NewInt32Slice() | ||
|
|
||
| ctx := &mockAttributeContext{ | ||
| indices: indices, | ||
| dictionary: dict, | ||
| } | ||
|
|
||
| getSetter := AccessAttributes[*mockAttributeContext]() | ||
|
|
||
| // Pass a value that is not a ctxutil.Map | ||
| err := getSetter.Setter(t.Context(), ctx, "not_a_map") | ||
| assert.Error(t, err) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.