-
Notifications
You must be signed in to change notification settings - Fork 3k
[pkg/ottl] Change grammar to support expressing statements context via path names #34875
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
TylerHelmuth
merged 6 commits into
open-telemetry:main
from
edmocosta:ottl_statements_context_change_grammar
Sep 6, 2024
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
277f285
Change the OTTL grammar to support expressing statements context via …
edmocosta 6ee9855
Merge branch 'main' of https://github.com/edmocosta/opentelemetry-col…
edmocosta a2c8ab8
Fix failing test after branch update, adding more tests cases and nit…
edmocosta 0552371
Removed `WithPathContextNameValidation` option
edmocosta ee02ebd
Fix test to use assert.Len (testifylint)
edmocosta e04e2a3
Applied PR suggestions, changed pathContextNames set type, and added …
edmocosta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: enhancement | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: pkg/ottl | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: "Change the OTTL grammar to support expressing statements context via path names" | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [29017] | ||
|
||
# (Optional) One or more lines of additional information to render under the primary note. | ||
# These lines will be padded with 2 spaces and then inserted directly into the document. | ||
# Use pipe (|) for multiline entries. | ||
subtext: | ||
edmocosta marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# If your change doesn't affect end users or the exported elements of any package, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
# Optional: The change log or logs in which this entry should be included. | ||
# e.g. '[user]' or '[user, api]' | ||
# Include 'user' if the change is relevant to end users. | ||
# Include 'api' if there is a change to a library API. | ||
# Default: '[user]' | ||
change_logs: [api] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,9 +22,15 @@ type Enum int64 | |
|
||
type EnumSymbol string | ||
|
||
func buildOriginalText(fields []field) string { | ||
func buildOriginalText(path *path) string { | ||
var builder strings.Builder | ||
for i, f := range fields { | ||
if path.Context != "" { | ||
builder.WriteString(path.Context) | ||
if len(path.Fields) > 0 { | ||
builder.WriteString(".") | ||
} | ||
} | ||
for i, f := range path.Fields { | ||
builder.WriteString(f.Name) | ||
if len(f.Keys) > 0 { | ||
for _, k := range f.Keys { | ||
|
@@ -38,21 +44,28 @@ func buildOriginalText(fields []field) string { | |
builder.WriteString("]") | ||
} | ||
} | ||
if i != len(fields)-1 { | ||
if i != len(path.Fields)-1 { | ||
builder.WriteString(".") | ||
} | ||
} | ||
return builder.String() | ||
} | ||
|
||
func newPath[K any](fields []field) (*basePath[K], error) { | ||
if len(fields) == 0 { | ||
func (p *Parser[K]) newPath(path *path) (*basePath[K], error) { | ||
if len(path.Fields) == 0 { | ||
return nil, fmt.Errorf("cannot make a path from zero fields") | ||
} | ||
originalText := buildOriginalText(fields) | ||
|
||
pathContext, fields, err := p.parsePathContext(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
originalText := buildOriginalText(path) | ||
var current *basePath[K] | ||
for i := len(fields) - 1; i >= 0; i-- { | ||
current = &basePath[K]{ | ||
context: pathContext, | ||
name: fields[i].Name, | ||
keys: newKeys[K](fields[i].Keys), | ||
nextPath: current, | ||
|
@@ -64,10 +77,51 @@ func newPath[K any](fields []field) (*basePath[K], error) { | |
return current, nil | ||
} | ||
|
||
func (p *Parser[K]) parsePathContext(path *path) (string, []field, error) { | ||
hasPathContextNames := len(p.pathContextNames) > 0 | ||
if path.Context != "" { | ||
// no pathContextNames means the Parser isn't handling the grammar path's context yet, | ||
// so it falls back to the previous behavior with the path.Context value as the first | ||
// path's segment. | ||
if !hasPathContextNames { | ||
return "", append([]field{{Name: path.Context}}, path.Fields...), nil | ||
} else if _, ok := p.pathContextNames[path.Context]; !ok { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can remove this else. |
||
originalText := buildOriginalText(path) | ||
return "", nil, fmt.Errorf(`missing context name for path "%s", valid options are: %s`, originalText, p.buildPathContextNamesText(originalText)) | ||
} | ||
|
||
return "", path.Fields, nil | ||
} | ||
|
||
func (p *Parser[K]) buildPathContextNamesText(path string) string { | ||
var builder strings.Builder | ||
var suffix string | ||
if path != "" { | ||
suffix = "." + path | ||
} | ||
|
||
i := 0 | ||
for ctx := range p.pathContextNames { | ||
builder.WriteString(fmt.Sprintf(`"%s%s"`, ctx, suffix)) | ||
if i != len(p.pathContextNames)-1 { | ||
builder.WriteString(", ") | ||
} | ||
i++ | ||
} | ||
return builder.String() | ||
} | ||
|
||
// Path represents a chain of path parts in an OTTL statement, such as `body.string`. | ||
// A Path has a name, and potentially a set of keys. | ||
// If the path in the OTTL statement contains multiple parts (separated by a dot (`.`)), then the Path will have a pointer to the next Path. | ||
type Path[K any] interface { | ||
// Context is the OTTL context name of this Path. | ||
Context() string | ||
|
||
// Name is the name of this segment of the path. | ||
Name() string | ||
|
||
|
@@ -86,6 +140,7 @@ type Path[K any] interface { | |
var _ Path[any] = &basePath[any]{} | ||
|
||
type basePath[K any] struct { | ||
context string | ||
name string | ||
keys []Key[K] | ||
nextPath *basePath[K] | ||
|
@@ -94,6 +149,10 @@ type basePath[K any] struct { | |
originalText string | ||
} | ||
|
||
func (p *basePath[K]) Context() string { | ||
return p.context | ||
} | ||
|
||
func (p *basePath[K]) Name() string { | ||
return p.name | ||
} | ||
|
@@ -412,7 +471,7 @@ func (p *Parser[K]) buildArg(argVal value, argType reflect.Type) (any, error) { | |
if argVal.Literal == nil || argVal.Literal.Path == nil { | ||
return nil, fmt.Errorf("must be a path") | ||
} | ||
np, err := newPath[K](argVal.Literal.Path.Fields) | ||
np, err := p.newPath(argVal.Literal.Path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.