Skip to content

[processor/transform] Add support for profiles signal #39036

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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/profiles-transformprocessor.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: transformprocessor

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add profiles support to transformprocessor.

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [39009]

# (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: [user]
1 change: 1 addition & 0 deletions pkg/ottl/context_inferrer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var defaultContextInferPriority = []string{
"metric",
"spanevent",
"span",
"profile",
"scope",
"instrumentation_scope",
"resource",
Expand Down
23 changes: 23 additions & 0 deletions pkg/ottl/context_inferrer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ func Test_NewPriorityContextInferrer_DefaultPriorityList(t *testing.T) {
"metric",
"spanevent",
"span",
"profile",
"scope",
"instrumentation_scope",
"resource",
Expand All @@ -316,6 +317,7 @@ func Test_NewPriorityContextInferrer_InferStatements_DefaultContextsOrder(t *tes
"datapoint": newDummyPriorityContextInferrerCandidate(true, true, []string{"scope", "instrumentation_scope", "resource"}),
"span": newDummyPriorityContextInferrerCandidate(true, true, []string{"spanevent", "scope", "instrumentation_scope", "resource"}),
"spanevent": newDummyPriorityContextInferrerCandidate(true, true, []string{"scope", "instrumentation_scope", "resource"}),
"profile": newDummyPriorityContextInferrerCandidate(true, true, []string{"profile", "scope", "instrumentation_scope", "resource"}),
"scope": newDummyPriorityContextInferrerCandidate(true, true, []string{"resource"}),
"instrumentation_scope": newDummyPriorityContextInferrerCandidate(true, true, []string{"resource"}),
"resource": newDummyPriorityContextInferrerCandidate(true, true, []string{}),
Expand Down Expand Up @@ -386,6 +388,16 @@ func Test_NewPriorityContextInferrer_InferStatements_DefaultContextsOrder(t *tes
statement: `set(span.name, "foo") where spanevent.name != nil and scope.name != nil and resource.attributes["foo"] != nil`,
expected: "spanevent",
},
{
name: "profile,instrumentation_scope,resource",
statement: `set(profile.name, "foo") where profile.name != nil and instrumentation_scope.name != nil and resource.attributes["foo"] != nil`,
expected: "profile",
},
{
name: "profile,scope,resource",
statement: `set(profile.name, "foo") where profile.name != nil and scope.name != nil and resource.attributes["foo"] != nil`,
expected: "profile",
},
{
name: "resource",
statement: `set(resource.attributes["bar"], "foo") where dummy.attributes["foo"] != nil`,
Expand All @@ -409,6 +421,7 @@ func Test_NewPriorityContextInferrer_InferConditions_DefaultContextsOrder(t *tes
"datapoint": newDummyPriorityContextInferrerCandidate(true, true, []string{"scope", "instrumentation_scope", "resource"}),
"span": newDummyPriorityContextInferrerCandidate(true, true, []string{"spanevent", "scope", "instrumentation_scope", "resource"}),
"spanevent": newDummyPriorityContextInferrerCandidate(true, true, []string{"scope", "instrumentation_scope", "resource"}),
"profile": newDummyPriorityContextInferrerCandidate(true, true, []string{"profile", "scope", "instrumentation_scope", "resource"}),
"scope": newDummyPriorityContextInferrerCandidate(true, true, []string{"resource"}),
"instrumentation_scope": newDummyPriorityContextInferrerCandidate(true, true, []string{"resource"}),
"resource": newDummyPriorityContextInferrerCandidate(true, true, []string{}),
Expand Down Expand Up @@ -479,6 +492,16 @@ func Test_NewPriorityContextInferrer_InferConditions_DefaultContextsOrder(t *tes
condition: `span.name != nil and spanevent.name != nil and scope.name != nil and resource.attributes["foo"] != nil`,
expected: "spanevent",
},
{
name: "profile,instrumentation_scope,resource",
condition: `profile.name != nil and profile.name != nil and instrumentation_scope.name != nil and resource.attributes["foo"] != nil`,
expected: "profile",
},
{
name: "profile,scope,resource",
condition: `profile.name != nil and profile.name != nil and scope.name != nil and resource.attributes["foo"] != nil`,
expected: "profile",
},
{
name: "resource",
condition: `resource.attributes["bar"] != nil and dummy.attributes["foo"] != nil`,
Expand Down
162 changes: 162 additions & 0 deletions pkg/ottl/contexts/internal/ctxprofile/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"context"
"encoding/hex"
"errors"
"fmt"
"math"
"time"

"go.opentelemetry.io/collector/pdata/pcommon"
Expand All @@ -26,6 +28,7 @@ func PathGetSetter[K ProfileContext](path ottl.Path[K]) (ottl.GetSetter[K], erro
if path == nil {
return nil, ctxerror.New("nil", "nil", Name, DocRef)
}
fmt.Printf("PathGetterSetter: %v\n", path.Name())
switch path.Name() {
case "sample_type":
return accessSampleType[K](), nil
Expand Down Expand Up @@ -80,6 +83,11 @@ func PathGetSetter[K ProfileContext](path ottl.Path[K]) (ottl.GetSetter[K], erro
return accessOriginalPayloadFormat[K](), nil
case "original_payload":
return accessOriginalPayload[K](), nil
case "attributes":
if path.Keys() == nil {
return accessAttributes[K](), nil
}
return accessAttributesKey(path.Keys()), nil
default:
return nil, ctxerror.New(path.Name(), path.String(), Name, DocRef)
}
Expand Down Expand Up @@ -419,3 +427,157 @@ func accessOriginalPayload[K ProfileContext]() ottl.StandardGetSetter[K] {
},
}
}

func accessAttributes[K ProfileContext]() ottl.StandardGetSetter[K] {
return ottl.StandardGetSetter[K]{
Getter: func(_ context.Context, tCtx K) (any, error) {
fmt.Printf("accessAttributes.Getter\n")
dumpMap("get map", pprofile.FromAttributeIndices(tCtx.GetProfile().AttributeTable(), tCtx.GetProfile()))
return pprofile.FromAttributeIndices(tCtx.GetProfile().AttributeTable(), tCtx.GetProfile()), nil
},
Setter: func(_ context.Context, tCtx K, val any) error {
fmt.Printf("accessAttributes.Setter\n")
m, ok := val.(pcommon.Map)
if !ok {
return fmt.Errorf("expected pcommon.Map, got %T", val)
}
dumpMap("set map", m)
tCtx.GetProfile().AttributeIndices().FromRaw([]int32{})
for k, v := range m.All() {
if err := PutAttribute(tCtx.GetProfile().AttributeTable(), tCtx.GetProfile(), 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) {
fmt.Printf("accessAttributesKey.Getter\n")
return ctxutil.GetMapValue[K](ctx, tCtx, pprofile.FromAttributeIndices(tCtx.GetProfile().AttributeTable(), tCtx.GetProfile()), key)
/*
table := tCtx.GetProfile().AttributeTable()
searchKey, err := key[0].String(ctx, tCtx)
fmt.Printf("accessAttributesKey.Getter: %v\n", searchKey)
if err != nil {
return nil, err
}
for idx := range tCtx.GetProfile().AttributeIndices().AsRaw() {
if idx >= table.Len() {
continue
}
attr := table.At(idx)
if attr.Key() == *searchKey {
fmt.Printf("return accessAttributesKey.Getter: %v\n", attr.Value().AsString())
return attr.Value(), nil
}
}
return nil, fmt.Errorf("key %s not found in attribute table", *searchKey)
*/
},
Setter: func(ctx context.Context, tCtx K, val any) error {
newKey, err := key[0].String(ctx, tCtx)
fmt.Printf("accessAttributesKey.Setter: %v val %v\n", *newKey, val)
if err != nil {
return err
}
fmt.Printf("accessAttributesKey.Setter: %v\n", *newKey)
v := pcommon.NewValueEmpty()
if err = v.FromRaw(val); err != nil {
return err
}
return PutAttribute(tCtx.GetProfile().AttributeTable(), tCtx.GetProfile(), *newKey, v)
},
}
}

type attributable interface {
AttributeIndices() pcommon.Int32Slice
}

var errTooManyTableEntries = errors.New("too many entries in AttributeTable")

// PutAttribute updates an AttributeTable and a record's AttributeIndices to
// add a new attribute.
// The record can be any struct that implements an `AttributeIndices` method.
func PutAttribute(table pprofile.AttributeTableSlice, record attributable, key string, value pcommon.Value) error {
for i := range record.AttributeIndices().Len() {
idx := int(record.AttributeIndices().At(i))
if idx < 0 || idx >= table.Len() {
return fmt.Errorf("index value %d out of range in AttributeIndices[%d]", idx, i)
}
attr := table.At(idx)
if attr.Key() == key {
if attr.Value().Equal(value) {
// Attribute already exists, nothing to do.
return nil
}

// If the attribute table already contains the key/value pair, just update the index.
for j := range table.Len() {
a := table.At(j)
if a.Key() == key && a.Value().Equal(value) {
if j > math.MaxInt32 {
return errTooManyTableEntries
}
record.AttributeIndices().SetAt(i, int32(j))
return nil
}
}

if table.Len() >= math.MaxInt32 {
return errTooManyTableEntries
}

// Add the key/value pair as a new attribute to the table...
entry := table.AppendEmpty()
entry.SetKey(key)
value.CopyTo(entry.Value())

// ...and update the existing index.
record.AttributeIndices().SetAt(i, int32(table.Len()-1))
return nil
}
}

if record.AttributeIndices().Len() >= math.MaxInt32 {
return errors.New("too many entries in AttributeIndices")
}

for j := range table.Len() {
a := table.At(j)
if a.Key() == key && a.Value().Equal(value) {
if j > math.MaxInt32 {
return errTooManyTableEntries
}
// Add the index of the existing attribute to the indices.
record.AttributeIndices().Append(int32(j))
return nil
}
}

if table.Len() >= math.MaxInt32 {
return errTooManyTableEntries
}

// Add the key/value pair as a new attribute to the table...
entry := table.AppendEmpty()
entry.SetKey(key)
value.CopyTo(entry.Value())

// ...and add a new index to the indices.
record.AttributeIndices().Append(int32(table.Len() - 1))
return nil
}

func dumpMap(prefix string, m pcommon.Map) {
fmt.Printf("%s: ", prefix)
m.Range(func(k string, v pcommon.Value) bool {
fmt.Printf("{%s, %v}", k, v.AsString())
return true
})
fmt.Printf("\n")
}
6 changes: 3 additions & 3 deletions pkg/ottl/ottlfuncs/func_delete_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

type DeleteKeyArguments[K any] struct {
Target ottl.PMapGetter[K]
Target ottl.PMapGetSetter[K]
Key string
}

Expand All @@ -29,13 +29,13 @@ func createDeleteKeyFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments
return deleteKey(args.Target, args.Key), nil
}

func deleteKey[K any](target ottl.PMapGetter[K], key string) ottl.ExprFunc[K] {
func deleteKey[K any](target ottl.PMapGetSetter[K], key string) ottl.ExprFunc[K] {
return func(ctx context.Context, tCtx K) (any, error) {
val, err := target.Get(ctx, tCtx)
if err != nil {
return nil, err
}
val.Remove(key)
return nil, nil
return nil, target.Set(ctx, tCtx, val)
}
}
32 changes: 23 additions & 9 deletions pkg/ottl/ottlfuncs/func_delete_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package ottlfuncs

import (
"context"
"errors"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -19,15 +20,22 @@ func Test_deleteKey(t *testing.T) {
input.PutInt("test2", 3)
input.PutBool("test3", true)

target := &ottl.StandardPMapGetter[pcommon.Map]{
Getter: func(_ context.Context, tCtx pcommon.Map) (any, error) {
target := &ottl.StandardPMapGetSetter[pcommon.Map]{
Getter: func(_ context.Context, tCtx pcommon.Map) (pcommon.Map, error) {
return tCtx, nil
},
Setter: func(_ context.Context, tCtx pcommon.Map, val any) error {
if v, ok := val.(pcommon.Map); ok {
v.CopyTo(tCtx)
return nil
}
return errors.New("expected pcommon.Map")
},
}

tests := []struct {
name string
target ottl.PMapGetter[pcommon.Map]
target ottl.PMapGetSetter[pcommon.Map]
key string
want func(pcommon.Map)
}{
Expand Down Expand Up @@ -80,9 +88,12 @@ func Test_deleteKey(t *testing.T) {

func Test_deleteKey_bad_input(t *testing.T) {
input := pcommon.NewValueStr("not a map")
target := &ottl.StandardPMapGetter[any]{
Getter: func(_ context.Context, tCtx any) (any, error) {
return tCtx, nil
target := &ottl.StandardPMapGetSetter[any]{
Getter: func(_ context.Context, tCtx any) (pcommon.Map, error) {
if v, ok := tCtx.(pcommon.Map); ok {
return v, nil
}
return pcommon.Map{}, errors.New("expected pcommon.Map")
},
}

Expand All @@ -94,9 +105,12 @@ func Test_deleteKey_bad_input(t *testing.T) {
}

func Test_deleteKey_get_nil(t *testing.T) {
target := &ottl.StandardPMapGetter[any]{
Getter: func(_ context.Context, tCtx any) (any, error) {
return tCtx, nil
target := &ottl.StandardPMapGetSetter[any]{
Getter: func(_ context.Context, tCtx any) (pcommon.Map, error) {
if v, ok := tCtx.(pcommon.Map); ok {
return v, nil
}
return pcommon.Map{}, errors.New("expected pcommon.Map")
},
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/ottl/ottlfuncs/func_delete_matching_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

type DeleteMatchingKeysArguments[K any] struct {
Target ottl.PMapGetter[K]
Target ottl.PMapGetSetter[K]
Pattern string
}

Expand All @@ -33,7 +33,7 @@ func createDeleteMatchingKeysFunction[K any](_ ottl.FunctionContext, oArgs ottl.
return deleteMatchingKeys(args.Target, args.Pattern)
}

func deleteMatchingKeys[K any](target ottl.PMapGetter[K], pattern string) (ottl.ExprFunc[K], error) {
func deleteMatchingKeys[K any](target ottl.PMapGetSetter[K], pattern string) (ottl.ExprFunc[K], error) {
compiledPattern, err := regexp.Compile(pattern)
if err != nil {
return nil, fmt.Errorf("the regex pattern supplied to delete_matching_keys is not a valid pattern: %w", err)
Expand All @@ -46,6 +46,6 @@ func deleteMatchingKeys[K any](target ottl.PMapGetter[K], pattern string) (ottl.
val.RemoveIf(func(key string, _ pcommon.Value) bool {
return compiledPattern.MatchString(key)
})
return nil, nil
return nil, target.Set(ctx, tCtx, val)
}, nil
}
Loading
Loading