Skip to content

Commit

Permalink
[internal/filter] Replace usage of deprecated Value.Equal (open-telem…
Browse files Browse the repository at this point in the history
…etry#17521)

[chore] [internal/filter] Remove usage of deprecated Value.Equal

Implement the needed equality function in place
  • Loading branch information
dmitryax committed Jan 18, 2023
1 parent a2acb3c commit 5266ecb
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 3 deletions.
33 changes: 30 additions & 3 deletions internal/filter/filtermatcher/attributematcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,23 @@ import (

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

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/pdatautil"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filterconfig"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filterset"
)

type AttributesMatcher []AttributeMatcher

type valueIdentifier struct {
value pcommon.Value
valueHash [16]byte
}

// AttributeMatcher is a attribute key/value pair to match to.
type AttributeMatcher struct {
Key string
// If both AttributeValue and StringFilter are nil only check for key existence.
AttributeValue *pcommon.Value
AttributeValue *valueIdentifier
// StringFilter is needed to match against a regular expression
StringFilter filterset.FilterSet
}
Expand Down Expand Up @@ -72,7 +78,10 @@ func NewAttributesMatcher(config filterset.Config, attributes []filterconfig.Att
}
entry.StringFilter = filter
case filterset.Strict:
entry.AttributeValue = &val
entry.AttributeValue = &valueIdentifier{
value: val,
valueHash: pdatautil.ValueHash(val),
}
default:
return nil, filterset.NewUnrecognizedMatchTypeError(config.MatchType)

Expand Down Expand Up @@ -110,7 +119,7 @@ func (ma AttributesMatcher) Match(attrs pcommon.Map) bool {
return false
}
} else if property.AttributeValue != nil {
if !attr.Equal(*property.AttributeValue) {
if !attributeValueMatch(property.AttributeValue, attr) {
return false
}
}
Expand All @@ -132,3 +141,21 @@ func attributeStringValue(attr pcommon.Value) (string, error) {
return "", errUnexpectedAttributeType
}
}

func attributeValueMatch(vi *valueIdentifier, val pcommon.Value) bool {
if vi.value.Type() != val.Type() {
return false
}
switch val.Type() {
case pcommon.ValueTypeStr:
return vi.value.Str() == val.Str()
case pcommon.ValueTypeBool:
return vi.value.Bool() == val.Bool()
case pcommon.ValueTypeDouble:
return vi.value.Double() == val.Double()
case pcommon.ValueTypeInt:
return vi.value.Int() == val.Int()
}
// Use hash for other complex data types.
return vi.valueHash == pdatautil.ValueHash(val)
}
80 changes: 80 additions & 0 deletions internal/filter/filtermatcher/attributematcher_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// 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
//
// https://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 filtermatcher // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filtermatcher"

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/pdata/pcommon"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filterconfig"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filterset"
)

func TestMatchAttributes(t *testing.T) {
matchCfg := filterset.Config{MatchType: filterset.Strict}
attrsCfg := []filterconfig.Attribute{
{Key: "strKey", Value: "strVal"},
{Key: "intKey", Value: 1},
{Key: "sliceKey", Value: []any{"a", "b"}},
}
matcher, err := NewAttributesMatcher(matchCfg, attrsCfg)
require.NoError(t, err)

matchingMap := pcommon.NewMap()
matchingMap.PutStr("strKey", "strVal")
matchingMap.PutInt("intKey", 1)
sl := matchingMap.PutEmptySlice("sliceKey")
sl.AppendEmpty().SetStr("a")
sl.AppendEmpty().SetStr("b")
matchingMap.PutStr("anotherKey", "anotherVal")

notMatchingMap := pcommon.NewMap()
notMatchingMap.PutStr("strKey", "strVal")
notMatchingMap.PutInt("intKey", 1)
notMatchingMap.PutStr("anotherKey", "anotherVal")

assert.True(t, matcher.Match(matchingMap))
assert.False(t, matcher.Match(notMatchingMap))
}

func BenchmarkMatchAttributes(b *testing.B) {
matchCfg := filterset.Config{MatchType: filterset.Strict}
attrsCfg := []filterconfig.Attribute{
{Key: "strKey", Value: "strVal"},
{Key: "intKey", Value: 1},
}
matcher, err := NewAttributesMatcher(matchCfg, attrsCfg)
require.NoError(b, err)

matchingMap := pcommon.NewMap()
matchingMap.PutStr("strKey", "strVal")
matchingMap.PutInt("intKey", 1)
matchingMap.PutStr("anotherKey", "anotherVal")

notMatchingMap := pcommon.NewMap()
notMatchingMap.PutStr("strKey", "strVal")
notMatchingMap.PutBool("boolKey", true)
notMatchingMap.PutStr("anotherKey", "anotherVal")

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
matcher.Match(matchingMap)
matcher.Match(notMatchingMap)
}
}
1 change: 1 addition & 0 deletions internal/filter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (

require (
github.com/alecthomas/participle/v2 v2.0.0-beta.5 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
Expand Down
2 changes: 2 additions & 0 deletions internal/filter/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions processor/attributesprocessor/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
require (
github.com/alecthomas/participle/v2 v2.0.0-beta.5 // indirect
github.com/antonmedv/expr v1.9.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
Expand Down
2 changes: 2 additions & 0 deletions processor/attributesprocessor/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions processor/filterprocessor/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
require (
github.com/alecthomas/participle/v2 v2.0.0-beta.5 // indirect
github.com/antonmedv/expr v1.9.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
Expand Down
2 changes: 2 additions & 0 deletions processor/filterprocessor/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5266ecb

Please sign in to comment.