Skip to content
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 grammar utility to extract paths from statements #35174

Merged

Conversation

edmocosta
Copy link
Contributor

Description:

This PR is part of #29017, and adds a grammar utility to allow extracting ottl.path from parsed statements and expressions. The new functions will power the context inferrer utility (see draft), which implementation will basically gather all declared statement's Path.Context, and choose the highest priority one.

For the statements rewriter utility purpose, it changes the grammar.go file including a new field Pos lexer.Position into the ottl.path struct. This value is automatically set by the participle lexer and holds the path's offsets, being useful for identifying their positions in the raw statement, without using regexes an being coupled to the grammar's definition.

Additional changes:

This proposal uses the visitor pattern to gather all path's from the parsed statements. Considering the grammar's custom error mechanism (checkForCustomError) also works with a similar pattern, I've added the visitor implementation as part of the grammar objects, and refactored all checkForCustomError functions to be part of a validator visitor. Differently of the current implementation, it joins and displays all errors at once instead of one by one, IMO, it's specially useful for statements with more than one error, for example: set(name, int(2), float(1)).

* We can change it back do be error-by-error if necessary.
* If modifying the custom validator is not desired, I can roll those changes back keeping only the necessary code to extract the path's + Pos field.

Link to tracking Issue: #29017

Testing: Unit tests were added

Documentation: No changes

@edmocosta edmocosta changed the title [ottl/pkg] Add grammar utility to extract paths from statements [pkg/ottl] Add grammar utility to extract paths from statements Sep 13, 2024
@TylerHelmuth
Copy link
Member

@edmocosta I'm holding off on reviewing this until it is marked Ready, but I am interested.

@edmocosta
Copy link
Contributor Author

@edmocosta I'm holding off on reviewing this until it is marked Ready, but I am interested.

I was running a few more tests and forgot to mark it as ready. Thanks @TylerHelmuth!

@edmocosta edmocosta marked this pull request as ready for review September 25, 2024 09:39
@edmocosta edmocosta requested a review from a team as a code owner September 25, 2024 09:39
pkg/ottl/grammar.go Show resolved Hide resolved
pkg/ottl/grammar.go Show resolved Hide resolved
@edmocosta edmocosta force-pushed the ottl_add_grammar_paths_visitor branch from 9ff99fb to 055d362 Compare September 27, 2024 12:12
@TylerHelmuth TylerHelmuth added the Skip Changelog PRs that do not require a CHANGELOG.md entry label Sep 27, 2024
@edmocosta
Copy link
Contributor Author

Hi @TylerHelmuth! please let me know if there is anything left before it can be merged. Thank you!

@TylerHelmuth
Copy link
Member

@evan-bradley please review

Copy link
Contributor

@evan-bradley evan-bradley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, sorry for the long delay in reviewing this. Thank you @edmocosta!

@evan-bradley evan-bradley merged commit e95bae2 into open-telemetry:main Oct 7, 2024
156 checks passed
@github-actions github-actions bot added this to the next release milestone Oct 7, 2024
shalper2 pushed a commit to shalper2/opentelemetry-collector-contrib that referenced this pull request Oct 7, 2024
…-telemetry#35174)

**Description:** <Describe what has changed.>
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->

This PR is part of
open-telemetry#29017,
and adds a grammar utility to allow extracting `ottl.path` from parsed
statements and expressions. The new functions will power the context
inferrer utility (see
[draft](open-telemetry#35050)),
which implementation will basically gather all declared statement's
`Path.Context`, and choose the highest priority one.

For the statements rewriter utility purpose, it changes the `grammar.go`
file including a new field `Pos lexer.Position` into the `ottl.path`
struct. This value is automatically set by the `participle` lexer and
holds the path's offsets, being useful for identifying their positions
in the raw statement, without using regexes an being coupled to the
grammar's definition.

**Additional changes**:

This proposal uses the visitor pattern to gather all path's from the
parsed statements. Considering the grammar's custom error mechanism
(`checkForCustomError`) also works with a similar pattern, I've added
the visitor implementation as part of the grammar objects, and
refactored all `checkForCustomError` functions to be part of a validator
visitor. Differently of the current implementation, it joins and
displays all errors at once instead of one by one, IMO, it's specially
useful for statements with more than one error, for example: `set(name,
int(2), float(1))`.

*&ast;* We can change it back do be error-by-error if necessary.
*&ast;* If modifying the custom validator is not desired, I can roll
those changes back keeping only the necessary code to extract the path's
+ `Pos` field.

**Link to tracking Issue:**
open-telemetry#29017

**Testing:** Unit tests were added

**Documentation:** No changes
ghost pushed a commit to sematext/opentelemetry-collector-contrib that referenced this pull request Oct 9, 2024
…-telemetry#35174)

**Description:** <Describe what has changed.>
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->

This PR is part of
open-telemetry#29017,
and adds a grammar utility to allow extracting `ottl.path` from parsed
statements and expressions. The new functions will power the context
inferrer utility (see
[draft](open-telemetry#35050)),
which implementation will basically gather all declared statement's
`Path.Context`, and choose the highest priority one.

For the statements rewriter utility purpose, it changes the `grammar.go`
file including a new field `Pos lexer.Position` into the `ottl.path`
struct. This value is automatically set by the `participle` lexer and
holds the path's offsets, being useful for identifying their positions
in the raw statement, without using regexes an being coupled to the
grammar's definition.

**Additional changes**:

This proposal uses the visitor pattern to gather all path's from the
parsed statements. Considering the grammar's custom error mechanism
(`checkForCustomError`) also works with a similar pattern, I've added
the visitor implementation as part of the grammar objects, and
refactored all `checkForCustomError` functions to be part of a validator
visitor. Differently of the current implementation, it joins and
displays all errors at once instead of one by one, IMO, it's specially
useful for statements with more than one error, for example: `set(name,
int(2), float(1))`.

*&ast;* We can change it back do be error-by-error if necessary.
*&ast;* If modifying the custom validator is not desired, I can roll
those changes back keeping only the necessary code to extract the path's
+ `Pos` field.

**Link to tracking Issue:**
open-telemetry#29017

**Testing:** Unit tests were added

**Documentation:** No changes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg/ottl Skip Changelog PRs that do not require a CHANGELOG.md entry
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants