-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[pkg/ottl] Add new ottl.ValueComparator
API and support for slices comparison
#40370
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
[pkg/ottl] Add new ottl.ValueComparator
API and support for slices comparison
#40370
Conversation
ottl.ValueComparator
API and support for slices comparisonottl.ValueComparator
API and support for slices comparison
} | ||
|
||
// ottlValueComparator is the default implementation of the ValueComparator | ||
type ottlValueComparator struct{} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add the var _ = (ValueComparator)(nil)
thing that enforces this type is a ValueComparator
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think in this case we probably don't need that, the NewValueComparator() ValueComparator { return &ottlValueComparator{} }
is already ensuring it, if ottlValueComparator
does not satisfy the interface, compilation would break.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm okay either way here, I don't think we gain or lose much by adding it since NewValueComparator
implicitly does this check.
…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
…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
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 .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
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.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 (#38611).Testing
Manual and unit tests
Documentation
Updated LANGUAGE.md