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

[processor/transform] Extract function arguments into separate object… #8339

Merged
merged 2 commits into from
Mar 24, 2022
Merged
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
18 changes: 8 additions & 10 deletions processor/transformprocessor/internal/traces/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ package traces // import "github.com/open-telemetry/opentelemetry-collector-cont
import (
"fmt"

"go.opentelemetry.io/collector/model/pdata"

"github.com/open-telemetry/opentelemetry-collector-contrib/processor/transformprocessor/internal/common"
)

type condFunc = func(span pdata.Span, il pdata.InstrumentationLibrary, resource pdata.Resource) bool
type condFunc = func(ctx spanTransformContext) bool

var alwaysTrue = func(span pdata.Span, il pdata.InstrumentationLibrary, resource pdata.Resource) bool {
var alwaysTrue = func(ctx spanTransformContext) bool {
return true
}

Expand All @@ -44,15 +42,15 @@ func newConditionEvaluator(cond *common.Condition, functions map[string]interfac

switch cond.Op {
case "==":
return func(span pdata.Span, il pdata.InstrumentationLibrary, resource pdata.Resource) bool {
a := left.get(span, il, resource)
b := right.get(span, il, resource)
return func(ctx spanTransformContext) bool {
a := left.get(ctx)
b := right.get(ctx)
return a == b
}, nil
case "!=":
return func(span pdata.Span, il pdata.InstrumentationLibrary, resource pdata.Resource) bool {
a := left.get(span, il, resource)
b := right.get(span, il, resource)
return func(ctx spanTransformContext) bool {
a := left.get(ctx)
b := right.get(ctx)
return a != b
}, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ func Test_newConditionEvaluator(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
evaluate, err := newConditionEvaluator(tt.cond, DefaultFunctions())
assert.NoError(t, err)
assert.True(t, evaluate(tt.matching, pdata.NewInstrumentationLibrary(), pdata.NewResource()))
assert.True(t, evaluate(spanTransformContext{
span: tt.matching,
il: pdata.NewInstrumentationLibrary(),
resource: pdata.NewResource(),
}))
})
}

Expand Down
18 changes: 12 additions & 6 deletions processor/transformprocessor/internal/traces/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,23 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/transformprocessor/internal/common"
)

type exprFunc func(span pdata.Span, il pdata.InstrumentationLibrary, resource pdata.Resource) interface{}
type spanTransformContext struct {
span pdata.Span
il pdata.InstrumentationLibrary
resource pdata.Resource
}

type exprFunc func(ctx spanTransformContext) interface{}

// getter allows reading a value while processing traces. Note that data is not necessarily read from input
// telemetry but may be a literal value or a function invocation.
type getter interface {
get(span pdata.Span, il pdata.InstrumentationLibrary, resource pdata.Resource) interface{}
get(ctx spanTransformContext) interface{}
}

// setter allows writing a value to trace data.
type setter interface {
set(span pdata.Span, il pdata.InstrumentationLibrary, resource pdata.Resource, val interface{})
set(ctx spanTransformContext, val interface{})
}

// getSetter allows reading or writing a value to trace data.
Expand All @@ -46,7 +52,7 @@ type literal struct {
value interface{}
}

func (l literal) get(pdata.Span, pdata.InstrumentationLibrary, pdata.Resource) interface{} {
func (l literal) get(ctx spanTransformContext) interface{} {
return l.value
}

Expand Down Expand Up @@ -75,8 +81,8 @@ func newGetter(val common.Value, functions map[string]interface{}) (getter, erro
return nil, err
}
return &pathGetSetter{
getter: func(span pdata.Span, il pdata.InstrumentationLibrary, resource pdata.Resource) interface{} {
return call(span, il, resource)
getter: func(ctx spanTransformContext) interface{} {
return call(ctx)
},
}, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
)

func hello() exprFunc {
return func(span pdata.Span, il pdata.InstrumentationLibrary, resource pdata.Resource) interface{} {
return func(ctx spanTransformContext) interface{} {
return "world"
}
}
Expand Down Expand Up @@ -88,7 +88,11 @@ func Test_newGetter(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
reader, err := newGetter(tt.val, functions)
assert.NoError(t, err)
val := reader.get(span, pdata.NewInstrumentationLibrary(), pdata.NewResource())
val := reader.get(spanTransformContext{
span: span,
il: pdata.NewInstrumentationLibrary(),
resource: pdata.NewResource(),
})
assert.Equal(t, tt.want, val)
})
}
Expand Down
12 changes: 6 additions & 6 deletions processor/transformprocessor/internal/traces/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ func DefaultFunctions() map[string]interface{} {
}

func set(target setter, value getter) exprFunc {
return func(span pdata.Span, il pdata.InstrumentationLibrary, resource pdata.Resource) interface{} {
val := value.get(span, il, resource)
return func(ctx spanTransformContext) interface{} {
val := value.get(ctx)
if val != nil {
target.set(span, il, resource, val)
target.set(ctx, val)
}
return nil
}
Expand All @@ -48,8 +48,8 @@ func keepKeys(target getSetter, keys []string) exprFunc {
keySet[key] = struct{}{}
}

return func(span pdata.Span, il pdata.InstrumentationLibrary, resource pdata.Resource) interface{} {
val := target.get(span, il, resource)
return func(ctx spanTransformContext) interface{} {
val := target.get(ctx)
if val == nil {
return nil
}
Expand All @@ -64,7 +64,7 @@ func keepKeys(target getSetter, keys []string) exprFunc {
}
return true
})
target.set(span, il, resource, filtered)
target.set(ctx, filtered)
}
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,11 @@ func Test_newFunctionCall(t *testing.T) {

evaluate, err := newFunctionCall(tt.inv, DefaultFunctions())
assert.NoError(t, err)
evaluate(span, pdata.NewInstrumentationLibrary(), pdata.NewResource())
evaluate(spanTransformContext{
span: span,
il: pdata.NewInstrumentationLibrary(),
resource: pdata.NewResource(),
})

expected := pdata.NewSpan()
tt.want(expected)
Expand Down
8 changes: 6 additions & 2 deletions processor/transformprocessor/internal/traces/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,21 @@ func NewProcessor(statements []string, functions map[string]interface{}, setting
}

func (p *Processor) ProcessTraces(_ context.Context, td pdata.Traces) (pdata.Traces, error) {
ctx := spanTransformContext{}
for i := 0; i < td.ResourceSpans().Len(); i++ {
rspans := td.ResourceSpans().At(i)
ctx.resource = rspans.Resource()
for j := 0; j < rspans.InstrumentationLibrarySpans().Len(); j++ {
il := rspans.InstrumentationLibrarySpans().At(j).InstrumentationLibrary()
ctx.il = il
spans := rspans.InstrumentationLibrarySpans().At(j).Spans()
for k := 0; k < spans.Len(); k++ {
span := spans.At(k)
ctx.span = span

for _, statement := range p.queries {
if statement.condition(span, il, rspans.Resource()) {
statement.function(span, il, rspans.Resource())
if statement.condition(ctx) {
statement.function(ctx)
}
}
}
Expand Down
Loading