You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[pkg/ottl] Add new ottl.ValueComparator API and support for slices comparison (open-telemetry#40370)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
#### Description
This PR introduces two related changes and contains 2 change logs
because of that.
**1 - Comparator API**
Exposes the internal OTTL comparators logic as a new API
(`ottl.ValueComparator`), which can be used by API consumers to compare
raw values following the same OTTL [comparison rules
](https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/pkg/ottl/LANGUAGE.md#comparison-rules).
**Why?**
Existing and new functions that needs to compare values can be
leveraging this new API to compare them, and keep it consistent with the
OTTL comparison logic. For example, the new
[`Contains`](open-telemetry#40193)
function for slices, can be using this API to determine whether an slice
contains a particular value, following the same comparison logic the
grammar does.
```go
// Exported interface:
type ValueComparator interface {
// Equal compares two values for equality, returning true if they are equals
// according to the OTTL comparison rules.
Equal(a any, b any) bool
// NotEqual compares two values for equality, returning true if they are different
// according to the OTTL comparison rules.
NotEqual(a any, b any) bool
// Less compares two values, returning true if the first value is less than the second
// value, using the OTTL comparison rules.
Less(a any, b any) bool
// LessEqual compares two values, returning true if the first value is less or equal
// to the second value, using the OTTL comparison rules.
LessEqual(a any, b any) bool
// Greater compares two values, returning true if the first value is greater than the
// second value, using the OTTL comparison rules.
Greater(a any, b any) bool
// GreaterEqual compares two values, returning true if the first value is greater or
// equal to the second value, using the OTTL comparison rules.
GreaterEqual(a any, b any) bool
}
// Usage:
comp := ottl.NewValueComparator()
```
**2 - Add ability to compare slices**
We currently don't have the ability to compare slices, which means
conditions like `attributes["slice"] == attributes["slice"]` returns
false. This PR also adds the ability to compare slices/pcommon.Slices,
similar to the maps support
(open-telemetry#38611).
<!--Describe what testing was performed and which tests were added.-->
#### Testing
Manual and unit tests
<!--Describe the documentation added.-->
#### Documentation
Updated LANGUAGE.md
Copy file name to clipboardExpand all lines: pkg/ottl/LANGUAGE.md
+14-12Lines changed: 14 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -274,18 +274,20 @@ A `not equal` notation in the table below means that the "!=" operator returns t
274
274
275
275
The `time.Time` and `time.Duration` types are compared using comparison functions from their respective packages. For more details on how those comparisons work, see the [Golang Time package](https://pkg.go.dev/time).
| bool | normal, T>F | not equal | not equal | not equal | not equal | not equal | not equal | not equal | not equal | not equal |
280
-
| int64 | not equal | compared as largest | compared as float64 | not equal | not equal | not equal | not equal | not equal | not equal | not equal |
281
-
| float64 | not equal | compared as float64 | compared as largest | not equal | not equal | not equal | not equal | not equal | not equal | not equal |
282
-
| string | not equal | not equal | not equal | normal (compared as Go strings) | not equal | not equal | not equal | not equal | not equal | not equal |
283
-
| Bytes | not equal | not equal | not equal | not equal | byte-for-byte comparison |[]byte(nil) == nil | not equal | not equal | not equal | not equal |
284
-
| nil | not equal | not equal | not equal | not equal |[]byte(nil) == nil | true for equality only | not equal | not equal | not equal | not equal |
285
-
| time.Time | not equal | not equal | not equal | not equal | not equal | not equal | uses `time.Equal()`to check equality | not equal | not equal | not equal |
286
-
| time.Duration | not equal | not equal | not equal | not equal | not equal | not equal | not equal | uses `time.Before()` and `time.After` for comparison | not equal | not equal |
287
-
| map[string]any | not equal | not equal | not equal | not equal | not equal | not equal | not equal | not equal | uses reflect.DeepEqual for comparison | convert to raw map and uses reflect.DeepEqual for comparison |
288
-
| pcommon.Map | not equal | not equal | not equal | not equal | not equal | not equal | not equal | not equal | convert to raw map and uses reflect.DeepEqual for comparison | uses pcommon.Map Equal for comparison |
| bool | normal, T>F | not equal | not equal | not equal | not equal | not equal | not equal | not equal | not equal | not equal | not equal | not equal |
280
+
| int64 | not equal | compared as largest | compared as float64 | not equal | not equal | not equal | not equal | not equal | not equal | not equal | not equal | not equal |
281
+
| float64 | not equal | compared as float64 | compared as largest | not equal | not equal | not equal | not equal | not equal | not equal | not equal | not equal | not equal |
282
+
| string | not equal | not equal | not equal | normal (compared as Go strings) | not equal | not equal | not equal | not equal | not equal | not equal | not equal | not equal |
283
+
| Bytes | not equal | not equal | not equal | not equal | byte-for-byte comparison |[]byte(nil) == nil | not equal | not equal | not equal | not equal | not equal | not equal |
284
+
| nil | not equal | not equal | not equal | not equal |[]byte(nil) == nil | true for equality only | not equal | not equal | not equal | not equal | not equal | not equal |
285
+
| time.Time | not equal | not equal | not equal | not equal | not equal | not equal | uses `time.Equal()`to check equality | not equal | not equal | not equal | not equal | not equal |
286
+
| time.Duration | not equal | not equal | not equal | not equal | not equal | not equal | not equal | uses `time.Before()` and `time.After` for comparison | not equal | not equal | not equal | not equal |
287
+
| map[string]any | not equal | not equal | not equal | not equal | not equal | not equal | not equal | not equal | uses reflect.DeepEqual for comparison | convert to raw map and uses reflect.DeepEqual for comparison | not equal | not equal |
288
+
| pcommon.Map | not equal | not equal | not equal | not equal | not equal | not equal | not equal | not equal | convert to raw map and uses reflect.DeepEqual for comparison | uses pcommon.Map Equal for comparison | not equal | not equal |
289
+
|[]any | not equal | not equal | not equal | not equal | not equal | not equal | not equal | not equal | not equal | not equal | uses reflect.DeepEqual for comparison | convert to raw slice and uses reflect.DeepEqual for comparison |
290
+
| pcommon.Slice | not equal | not equal | not equal | not equal | not equal | not equal | not equal | not equal | not equal | not equal | convert to raw slice and uses reflect.DeepEqual for comparison | uses pcommon.Slice Equal for comparison |
0 commit comments