Skip to content

Commit

Permalink
[processor/transform] Add IsMatch function (open-telemetry#11123)
Browse files Browse the repository at this point in the history
* added IsMatch function
  • Loading branch information
TylerHelmuth authored and atoulme committed Jul 16, 2022
1 parent 5fcf4ba commit 0676d43
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -67,6 +67,7 @@
- `transformprocessor`: Add byte slice literal to the grammar. Add new SpanID and TraceID functions that take a byte slice and return a Span/Trace ID. (#10487)
- `transformprocessor`: Add Summary transform functions. (#11041)
- `transformprocessor`: Add nil literal to the grammar. (#11150)
- `transformprocessor`: Add IsMatch factory function. This function allows regex matching in conditions (#10903)
- `elasticsearchreceiver`: Add integration test for elasticsearch receiver (#10165)
- `tailsamplingprocessor`: New sampler added that allows to sample based on minimum number of spans
- `datadogexporter`: Some config validation and unmarshaling steps are now done on `Validate` and `Unmarshal` instead of `Sanitize` (#8829)
Expand Down
2 changes: 2 additions & 0 deletions processor/transformprocessor/README.md
Expand Up @@ -27,6 +27,8 @@ Supported functions:

- `TraceID(bytes)` - `bytes` is a byte slice of exactly 16 bytes. The function returns a TraceID from `bytes`. e.g., `TraceID(0x00000000000000000000000000000000)`

- `IsMatch(target, pattern)` - `target` is either a path expression to a telemetry field to retrieve or a literal string. `pattern` is a regexp pattern. The function matches the target against the pattern, returning true if the match is successful and false otherwise. If target is nil or not a string false is always returned.

- `set(target, value)` - `target` is a path expression to a telemetry field to set `value` into. `value` is any value type.
e.g., `set(attributes["http.path"], "/foo")`, `set(name, attributes["http.route"])`, `set(trace_state["svc"], "example")`, `set(attributes["source"], trace_state["source"])`. If `value` resolves to `nil`, e.g.
it references an unset map value, there will be no action.
Expand Down
35 changes: 35 additions & 0 deletions processor/transformprocessor/internal/common/func_is_match.go
@@ -0,0 +1,35 @@
// 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 common // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/transformprocessor/internal/common"

import (
"fmt"
"regexp"
)

func isMatch(target Getter, pattern string) (ExprFunc, error) {
regexp, err := regexp.Compile(pattern)
if err != nil {
return nil, fmt.Errorf("the pattern supplied to IsMatch is not a valid regexp pattern: %w", err)
}
return func(ctx TransformContext) interface{} {
if val := target.Get(ctx); val != nil {
if valStr, ok := val.(string); ok {
return regexp.MatchString(valStr)
}
}
return false
}, nil
}
103 changes: 103 additions & 0 deletions processor/transformprocessor/internal/common/func_is_match_test.go
@@ -0,0 +1,103 @@
// 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 common

import (
"testing"

"github.com/stretchr/testify/assert"

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

func Test_isMatch(t *testing.T) {
tests := []struct {
name string
target Getter
pattern string
expected bool
}{
{
name: "replace match true",
target: &testGetSetter{
getter: func(ctx TransformContext) interface{} {
return "hello world"
},
},
pattern: "hello.*",
expected: true,
},
{
name: "replace match false",
target: &testGetSetter{
getter: func(ctx TransformContext) interface{} {
return "goodbye world"
},
},
pattern: "hello.*",
expected: false,
},
{
name: "replace match complex",
target: &testGetSetter{
getter: func(ctx TransformContext) interface{} {
return "-12.001"
},
},
pattern: "[-+]?\\d*\\.\\d+([eE][-+]?\\d+)?",
expected: true,
},
{
name: "target not a string",
target: &testGetSetter{
getter: func(ctx TransformContext) interface{} {
return 1
},
},
pattern: "doesnt matter will be false",
expected: false,
},
{
name: "target nil",
target: &testGetSetter{
getter: func(ctx TransformContext) interface{} {
return nil
},
},
pattern: "doesnt matter will be false",
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := testhelper.TestTransformContext{}

exprFunc, _ := isMatch(tt.target, tt.pattern)
actual := exprFunc(ctx)

assert.Equal(t, tt.expected, actual)
})
}
}

func Test_isMatch_validation(t *testing.T) {
target := &testGetSetter{
getter: func(ctx TransformContext) interface{} {
return "anything"
},
}
_, err := isMatch(target, "\\K")
assert.Error(t, err)
}
1 change: 1 addition & 0 deletions processor/transformprocessor/internal/common/functions.go
Expand Up @@ -22,6 +22,7 @@ import (
var registry = map[string]interface{}{
"TraceID": traceID,
"SpanID": spanID,
"IsMatch": isMatch,
"keep_keys": keepKeys,
"set": set,
"truncate_all": truncateAll,
Expand Down
6 changes: 6 additions & 0 deletions processor/transformprocessor/internal/logs/processor_test.go
Expand Up @@ -103,6 +103,12 @@ func TestProcess(t *testing.T) {
td.ResourceLogs().At(0).ScopeLogs().At(0).LogRecords().At(0).Attributes().InsertString("test", "pass")
},
},
{
query: `set(attributes["test"], "pass") where IsMatch(body, "operation[AC]") == true`,
want: func(td plog.Logs) {
td.ResourceLogs().At(0).ScopeLogs().At(0).LogRecords().At(0).Attributes().InsertString("test", "pass")
},
},
}

for _, tt := range tests {
Expand Down
Expand Up @@ -237,6 +237,15 @@ func TestProcess(t *testing.T) {
td.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(3).SetUnit("new unit")
},
},
{
query: []string{`set(attributes["test"], "pass") where IsMatch(metric.name, "operation[AC]") == true`},
want: func(td pmetric.Metrics) {
td.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(0).Sum().DataPoints().At(0).Attributes().InsertString("test", "pass")
td.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(0).Sum().DataPoints().At(1).Attributes().InsertString("test", "pass")
td.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(2).ExponentialHistogram().DataPoints().At(0).Attributes().InsertString("test", "pass")
td.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(2).ExponentialHistogram().DataPoints().At(1).Attributes().InsertString("test", "pass")
},
},
}

for _, tt := range tests {
Expand Down
Expand Up @@ -121,6 +121,12 @@ func TestProcess(t *testing.T) {
td.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(1).Attributes().UpdateString("http.method", "post")
},
},
{
query: `set(attributes["test"], "pass") where IsMatch(name, "operation[AC]") == true`,
want: func(td ptrace.Traces) {
td.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(0).Attributes().InsertString("test", "pass")
},
},
{
query: `set(attributes["test"], "pass") where attributes["doesnt exist"] == nil`,
want: func(td ptrace.Traces) {
Expand Down

0 comments on commit 0676d43

Please sign in to comment.