Skip to content

[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

Merged
merged 3 commits into from
Jun 4, 2025

Conversation

edmocosta
Copy link
Contributor

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.

// 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).

Testing

Manual and unit tests

Documentation

Updated LANGUAGE.md

@edmocosta edmocosta changed the title [pkg/ottl] Add newottl.ValueComparator API and support for slices comparison [pkg/ottl] Add new ottl.ValueComparator API and support for slices comparison May 29, 2025
@edmocosta edmocosta marked this pull request as ready for review May 29, 2025 16:29
@edmocosta edmocosta requested a review from a team as a code owner May 29, 2025 16:29
}

// ottlValueComparator is the default implementation of the ValueComparator
type ottlValueComparator struct{}
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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.

@atoulme atoulme merged commit 20d3551 into open-telemetry:main Jun 4, 2025
187 checks passed
@github-actions github-actions bot added this to the next release milestone Jun 4, 2025
@edmocosta edmocosta deleted the ottl-value-comparator-api branch June 9, 2025 12:29
rockdaboot pushed a commit to rockdaboot/opentelemetry-collector-contrib that referenced this pull request Jun 10, 2025
…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
dd-jasminesun pushed a commit to DataDog/opentelemetry-collector-contrib that referenced this pull request Jun 23, 2025
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants