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] Change grammar to support expressing statements context via path names #34875

Conversation

edmocosta
Copy link
Contributor

Description:

This PR is part of the #29017, and introduces the necessary OTTL Grammar/Parser changes to make it possible for statements express their context via path names.

This first iteration changes the grammar to parse the first path segment as the Context name, for example, the path foo.bar.field would be parsed as the following AST example: {Context: "foo", Fields: [{Name: "bar"}, {Name: "field"}]}, instead of {Fields: [{Name: "foo"},{Name: "bar"}, {Name: "field"}]}.

To make it non-breaking to all components during the transition time and development, this PR also changes the ottl.Parser[k].newPath function to, by default, fall this new behavior back to the previous one, using the grammar's parsed Context value as the first path segment (Fields[0]).

Two new Parser[K] options were added, WithPathContextNames and WithPathContextNameValidation, The first option will be used to enable the statements context support, parsing the first path segment as Context when the value maches any configured context names, or falling it back otherwise. The second option will be used to enable the validation and turning off the backward compatible mode, raising an error when paths have no context prefix, or when it's invalid.

For more details, please check the #29017 discussion out.

Link to tracking Issue: #29017

Testing:

  • Unit tests

Documentation:

  • No documentation changes were made at this point.

pkg/ottl/parser.go Outdated Show resolved Hide resolved
@edmocosta edmocosta force-pushed the ottl_statements_context_change_grammar branch from e2d7f05 to ee02ebd Compare August 30, 2024 15:43
Copy link
Member

@TylerHelmuth TylerHelmuth left a comment

Choose a reason for hiding this comment

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

@edmocosta I like the way this is looking.

For big refactors like this is gonna end up being we typically create a draft PR with all the changes implemented as if we were going to merge in a breaking change. Can you create a draft PR like that for this work? I'm interested to see the final picture for what we're trying to achieve before we start merging things. Don't worry about unit tests, I just want to see who the API and implementation shakes out.

// path's segment.
if !hasPathContextNames {
return "", append([]field{{Name: path.Context}}, path.Fields...), nil
} else if _, ok := p.pathContextNames[path.Context]; !ok {
Copy link
Member

Choose a reason for hiding this comment

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

We can remove this else.

return "", path.Fields, fmt.Errorf(`context "%s" from path "%s" is not valid, it must be replaced by one of: %s`, path.Context, buildOriginalText(path), p.buildPathContextNamesText(""))
}
return path.Context, path.Fields, nil
} else if hasPathContextNames {
Copy link
Member

Choose a reason for hiding this comment

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

We can remove this else.

@edmocosta
Copy link
Contributor Author

@edmocosta I like the way this is looking.

For big refactors like this is gonna end up being we typically create a draft PR with all the changes implemented as if we were going to merge in a breaking change. Can you create a draft PR like that for this work? I'm interested to see the final picture for what we're trying to achieve before we start merging things. Don't worry about unit tests, I just want to see who the API and implementation shakes out.

Sure thing! I'll create a draft PR with all the pieces together soon! Thanks @TylerHelmuth!

@edmocosta
Copy link
Contributor Author

Hi @TylerHelmuth! here's the draft PR: #35050
It includes all the changes we've been discussing. It certainly needs more work/discussions, including extra changes, adding tests, docs, code style, etc. It's a working draft implementation, so If you want you can try it out locally.

Copy link
Member

@TylerHelmuth TylerHelmuth left a comment

Choose a reason for hiding this comment

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

@edmocosta thanks for quickly writing up that draft. There is definitely a lot of complexity we're adding with this change, but I believe the portability and readability of:

trace_statements:
  - set(span.status.code, 1) where span.attributes["http.path"] == "/health"

is really nice. Take a look at the remaining comments and then we can get this merged and move on to the next step.

.chloggen/ottl_statements_context_change_grammar.yaml Outdated Show resolved Hide resolved
.chloggen/ottl_statements_context_change_grammar.yaml Outdated Show resolved Hide resolved
@edmocosta
Copy link
Contributor Author

@TylerHelmuth, I've addressed the suggestions, plus two nitpicks that I changed on the draft PR:

  • Changed the pathContextNames type from map[string]bool to map[string]struct{}
  • Added the possibly word on the error message here. IMO, It's clear specially when the statement is invalid and only contains ambiguous context name, for example set(resource.attributes["foo"], invalid.context)

@TylerHelmuth TylerHelmuth merged commit 642cc35 into open-telemetry:main Sep 6, 2024
156 checks passed
@github-actions github-actions bot added this to the next release milestone Sep 6, 2024
f7o pushed a commit to f7o/opentelemetry-collector-contrib that referenced this pull request Sep 12, 2024
…a path names (open-telemetry#34875)

**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 the
open-telemetry#29017,
and introduces the necessary OTTL Grammar/Parser changes to make it
possible for statements express their context via path names.

This first iteration changes the grammar to parse the first `path`
segment as the `Context` name, for example, the path `foo.bar.field`
would be parsed as the following AST example: `{Context: "foo", Fields:
[{Name: "bar"}, {Name: "field"}]}`, instead of `{Fields: [{Name:
"foo"},{Name: "bar"}, {Name: "field"}]}`.

To make it non-breaking to all components during the transition time and
development, this PR also changes the `ottl.Parser[k].newPath` function
to, by default, fall this new behavior back to the previous one, using
the grammar's parsed `Context` value as the first path segment
(`Fields[0]`).

Two new `Parser[K]` options were added, `WithPathContextNames` and
`WithPathContextNameValidation`, The first option will be used to enable
the statements context support, parsing the first path segment as
Context when the value maches any configured context names, or falling
it back otherwise. The second option will be used to enable the
validation and turning off the backward compatible mode, raising an
error when paths have no context prefix, or when it's invalid.

For more details, please check the
open-telemetry#29017
discussion out.

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

**Testing:** 
- Unit tests

**Documentation:**
- No documentation changes were made at this point.
jriguera pushed a commit to springernature/opentelemetry-collector-contrib that referenced this pull request Oct 4, 2024
…a path names (open-telemetry#34875)

**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 the
open-telemetry#29017,
and introduces the necessary OTTL Grammar/Parser changes to make it
possible for statements express their context via path names.

This first iteration changes the grammar to parse the first `path`
segment as the `Context` name, for example, the path `foo.bar.field`
would be parsed as the following AST example: `{Context: "foo", Fields:
[{Name: "bar"}, {Name: "field"}]}`, instead of `{Fields: [{Name:
"foo"},{Name: "bar"}, {Name: "field"}]}`.

To make it non-breaking to all components during the transition time and
development, this PR also changes the `ottl.Parser[k].newPath` function
to, by default, fall this new behavior back to the previous one, using
the grammar's parsed `Context` value as the first path segment
(`Fields[0]`).

Two new `Parser[K]` options were added, `WithPathContextNames` and
`WithPathContextNameValidation`, The first option will be used to enable
the statements context support, parsing the first path segment as
Context when the value maches any configured context names, or falling
it back otherwise. The second option will be used to enable the
validation and turning off the backward compatible mode, raising an
error when paths have no context prefix, or when it's invalid.

For more details, please check the
open-telemetry#29017
discussion out.

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

**Testing:** 
- Unit tests

**Documentation:**
- No documentation changes were made at this point.
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.

3 participants