Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .chloggen/ctxprofilecommon.yaml
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]
68 changes: 6 additions & 62 deletions pkg/ottl/contexts/internal/ctxprofile/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxcommon"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxerror"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxprofilecommon"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxutil"
)

Expand Down Expand Up @@ -59,10 +60,13 @@ func PathGetSetter[K Context](path ottl.Path[K]) (ottl.GetSetter[K], error) {
case "original_payload":
return accessOriginalPayload[K](), nil
case "attributes":
attributable := func(ctx K) (pprofile.ProfilesDictionary, ctxprofilecommon.ProfileAttributable) {
return ctx.GetProfilesDictionary(), ctx.GetProfile()
}
if path.Keys() == nil {
return accessAttributes[K](), nil
return ctxprofilecommon.AccessAttributes[K](attributable), nil
}
return accessAttributesKey(path.Keys()), nil
return ctxprofilecommon.AccessAttributesKey[K](path.Keys(), attributable), nil
default:
return nil, ctxerror.New(path.Name(), path.String(), Name, DocRef)
}
Expand Down Expand Up @@ -282,63 +286,3 @@ func accessOriginalPayload[K Context]() ottl.StandardGetSetter[K] {
},
}
}

func accessAttributes[K Context]() ottl.StandardGetSetter[K] {
return ottl.StandardGetSetter[K]{
Getter: func(_ context.Context, tCtx K) (any, error) {
return pprofile.FromAttributeIndices(tCtx.GetProfilesDictionary().AttributeTable(), tCtx.GetProfile(), tCtx.GetProfilesDictionary()), nil
},
Setter: func(_ context.Context, tCtx K, val any) error {
m, err := ctxutil.GetMap(val)
if err != nil {
return err
}
tCtx.GetProfile().AttributeIndices().FromRaw([]int32{})
for k, v := range m.All() {
if err := pprofile.PutAttribute(tCtx.GetProfilesDictionary().AttributeTable(), tCtx.GetProfile(), tCtx.GetProfilesDictionary(), k, v); err != nil {
return err
}
}
return nil
},
}
}

func accessAttributesKey[K Context](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.GetProfile(), tCtx.GetProfilesDictionary()), 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)
err = ctxutil.SetIndexableValue[K](ctx, tCtx, v, val, key[1:])
if err != nil {
return err
}
return pprofile.PutAttribute(tCtx.GetProfilesDictionary().AttributeTable(), tCtx.GetProfile(), tCtx.GetProfilesDictionary(), *newKey, v)
},
}
}

func getAttributeValue[K Context](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()
strTable := tCtx.GetProfilesDictionary().StringTable()

for _, tableIndex := range tCtx.GetProfile().AttributeIndices().All() {
attr := table.At(int(tableIndex))
if strTable.At(int(attr.KeyStrindex())) == key {
// Copy the value because OTTL expects to do inplace updates for the values.
v := pcommon.NewValueEmpty()
attr.Value().CopyTo(v)
return v
}
}

return pcommon.NewValueEmpty()
}
9 changes: 8 additions & 1 deletion pkg/ottl/contexts/internal/ctxprofile/profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,15 @@ func (p *profileContext) GetProfile() pprofile.Profile {
return p.profile
}

func (p *profileContext) AttributeIndices() pcommon.Int32Slice {
return p.profile.AttributeIndices()
}

func newProfileContext(profile pprofile.Profile, dictionary pprofile.ProfilesDictionary) *profileContext {
return &profileContext{profile: profile, dictionary: dictionary}
return &profileContext{
profile: profile,
dictionary: dictionary,
}
}

func createValueType() pprofile.ValueType {
Expand Down
85 changes: 85 additions & 0 deletions pkg/ottl/contexts/internal/ctxprofilecommon/attributes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// 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 ProfileAttributable interface {
AttributeIndices() pcommon.Int32Slice
}

type attributeSource[K any] = func(ctx K) (pprofile.ProfilesDictionary, ProfileAttributable)

func AccessAttributes[K any](source attributeSource[K]) ottl.StandardGetSetter[K] {
return ottl.StandardGetSetter[K]{
Getter: func(_ context.Context, tCtx K) (any, error) {
dict, attributable := source(tCtx)
return pprofile.FromAttributeIndices(dict.AttributeTable(), attributable, dict), nil
},
Setter: func(_ context.Context, tCtx K, val any) error {
m, err := ctxutil.GetMap(val)
if err != nil {
return err
}

dict, attributable := source(tCtx)
attributable.AttributeIndices().FromRaw([]int32{})
for k, v := range m.All() {
if err := pprofile.PutAttribute(dict.AttributeTable(), attributable, dict, k, v); err != nil {
return err
}
}
return nil
},
}
}

func AccessAttributesKey[K any](key []ottl.Key[K], source attributeSource[K]) ottl.StandardGetSetter[K] {
return ottl.StandardGetSetter[K]{
Getter: func(ctx context.Context, tCtx K) (any, error) {
dict, attributable := source(tCtx)
return ctxutil.GetMapValue[K](ctx, tCtx, pprofile.FromAttributeIndices(dict.AttributeTable(), attributable, dict), key)
},
Setter: func(ctx context.Context, tCtx K, val any) error {
newKey, err := ctxutil.GetMapKeyName(ctx, tCtx, key[0])
if err != nil {
return err
}

dict, attributable := source(tCtx)
v := getAttributeValue(dict, attributable.AttributeIndices(), *newKey)
if err := ctxutil.SetIndexableValue[K](ctx, tCtx, v, val, key[1:]); err != nil {
return err
}

return pprofile.PutAttribute(dict.AttributeTable(), attributable, dict, *newKey, v)
},
}
}

func getAttributeValue(dict pprofile.ProfilesDictionary, indices pcommon.Int32Slice, key string) pcommon.Value {
strTable := dict.StringTable()
kvuTable := dict.AttributeTable()

for _, tableIndex := range indices.All() {
attr := kvuTable.At(int(tableIndex))
attrKey := strTable.At(int(attr.KeyStrindex()))
if attrKey == key {
// Copy the value because OTTL expects to do inplace updates for the values.
v := pcommon.NewValueEmpty()
attr.Value().CopyTo(v)
return v
}
}

return pcommon.NewValueEmpty()
}
Loading