Skip to content

[pkg/ottl] add ability to compare maps #38611

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

Merged
merged 3 commits into from
Mar 13, 2025

Conversation

TylerHelmuth
Copy link
Member

Description

I realized today we don't have the ability to compare maps. So a condition like attributes == attributes returns false. This PR adds the ability to compare maps/pcommon.Maps.

Testing

manual testing and unit tests

Documentation

updated docs

@evan-bradley evan-bradley merged commit 302eae3 into open-telemetry:main Mar 13, 2025
171 checks passed
@github-actions github-actions bot added this to the next release milestone Mar 13, 2025
@TylerHelmuth TylerHelmuth deleted the ottl.map-comparison branch March 13, 2025 23:33
atoulme pushed a commit that referenced this pull request Jun 4, 2025
…comparison (#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`](#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
(#38611).

<!--Describe what testing was performed and which tests were added.-->
#### Testing
Manual and unit tests

<!--Describe the documentation added.-->
#### Documentation
Updated LANGUAGE.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants