-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[processor/transform] Add business logic for handling traces queries.
- Loading branch information
Showing
13 changed files
with
2,061 additions
and
30 deletions.
There are no files selected for viewing
This file contains 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 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 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,54 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package traces // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/transformprocessor/internal/traces" | ||
|
||
import ( | ||
"fmt" | ||
|
||
"go.opentelemetry.io/collector/model/pdata" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/transformprocessor/internal/common" | ||
) | ||
|
||
func newConditionEvaluator(cond *common.Condition) (func(span pdata.Span, il pdata.InstrumentationLibrary, resource pdata.Resource) bool, error) { | ||
if cond == nil { | ||
return func(span pdata.Span, il pdata.InstrumentationLibrary, resource pdata.Resource) bool { | ||
return true | ||
}, nil | ||
} | ||
left, err := newGetter(cond.Left) | ||
if err != nil { | ||
return nil, err | ||
} | ||
right, err := newGetter(cond.Right) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
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 a == b | ||
}, nil | ||
case "!=": | ||
return func(span pdata.Span, il pdata.InstrumentationLibrary, resource pdata.Resource) bool { | ||
return left.get(span, il, resource) != right.get(span, il, resource) | ||
}, nil | ||
} | ||
|
||
return nil, fmt.Errorf("unrecognized boolean operation %v", cond.Op) | ||
} |
136 changes: 136 additions & 0 deletions
136
processor/transformprocessor/internal/traces/condition_test.go
This file contains 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,136 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package traces | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.opentelemetry.io/collector/model/pdata" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/transformprocessor/internal/common" | ||
) | ||
|
||
func Test_newConditionEvaluator(t *testing.T) { | ||
span := pdata.NewSpan() | ||
span.SetName("bear") | ||
tests := []struct { | ||
name string | ||
cond *common.Condition | ||
matching pdata.Span | ||
}{ | ||
{ | ||
name: "literals match", | ||
cond: &common.Condition{ | ||
Left: common.Value{ | ||
String: strp("hello"), | ||
}, | ||
Right: common.Value{ | ||
String: strp("hello"), | ||
}, | ||
Op: "==", | ||
}, | ||
matching: span, | ||
}, | ||
{ | ||
name: "literals don't match", | ||
cond: &common.Condition{ | ||
Left: common.Value{ | ||
String: strp("hello"), | ||
}, | ||
Right: common.Value{ | ||
String: strp("goodbye"), | ||
}, | ||
Op: "!=", | ||
}, | ||
matching: span, | ||
}, | ||
{ | ||
name: "path expression matches", | ||
cond: &common.Condition{ | ||
Left: common.Value{ | ||
Path: &common.Path{ | ||
Fields: []common.Field{ | ||
{ | ||
Name: "name", | ||
}, | ||
}, | ||
}, | ||
}, | ||
Right: common.Value{ | ||
String: strp("bear"), | ||
}, | ||
Op: "==", | ||
}, | ||
matching: span, | ||
}, | ||
{ | ||
name: "path expression not matches", | ||
cond: &common.Condition{ | ||
Left: common.Value{ | ||
Path: &common.Path{ | ||
Fields: []common.Field{ | ||
{ | ||
Name: "name", | ||
}, | ||
}, | ||
}, | ||
}, | ||
Right: common.Value{ | ||
String: strp("bear"), | ||
}, | ||
Op: "==", | ||
}, | ||
matching: span, | ||
}, | ||
{ | ||
name: "no condition", | ||
cond: nil, | ||
matching: span, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
evaluate, err := newConditionEvaluator(tt.cond) | ||
assert.NoError(t, err) | ||
assert.True(t, evaluate(tt.matching, pdata.NewInstrumentationLibrary(), pdata.NewResource())) | ||
}) | ||
} | ||
|
||
t.Run("invalid", func(t *testing.T) { | ||
_, err := newConditionEvaluator(&common.Condition{ | ||
Left: common.Value{ | ||
String: strp("bear"), | ||
}, | ||
Op: "<>", | ||
Right: common.Value{ | ||
String: strp("cat"), | ||
}, | ||
}) | ||
assert.Error(t, err) | ||
}) | ||
} | ||
|
||
func strp(s string) *string { | ||
return &s | ||
} | ||
|
||
func intp(i int64) *int64 { | ||
return &i | ||
} | ||
|
||
func floatp(f float64) *float64 { | ||
return &f | ||
} |
Oops, something went wrong.