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

Allow ConcatFields for nodes of type object #10959

Merged
merged 7 commits into from
Mar 5, 2019

Conversation

simitt
Copy link
Contributor

@simitt simitt commented Feb 27, 2019

Allow ConcatFields to concatenate two fields when the first field contains a node's parent key but not the node itself and it is of type object.

Current behaviour (simplified):

a := a.b.c
b := a.b.c.d
f.ConcatFields(a, b) => error

Behaviour after merging this PR:

a := a.b.c
b := a.b.c.d
f.ConcatFields(a, b) => error

a := a.b.c, type(c)=object
b := a.b.c.d
f.ConcatFields(a, b) => success

This needs to be backported to >=6.6 as it leads to a concrete bug in APM, see apm-server#1959

Return false if field contains a nodes parent key but not the node
itself.
@simitt simitt added bug needs_backport PR is waiting to be backported to other branches. labels Feb 27, 2019
@simitt simitt requested a review from a team as a code owner February 27, 2019 09:13
@simitt simitt closed this Feb 27, 2019
@simitt simitt changed the title Fix field.HasNode method. Allo ConcatFields for nodes of type object Feb 27, 2019
@simitt simitt reopened this Feb 27, 2019
@simitt
Copy link
Contributor Author

simitt commented Feb 27, 2019

jenkins, retest this please

@graphaelli graphaelli changed the title Allo ConcatFields for nodes of type object Allow ConcatFields for nodes of type object Feb 27, 2019
libbeat/common/field.go Outdated Show resolved Hide resolved
@@ -304,7 +344,7 @@ func ConcatFields(a, b Fields) (Fields, error) {

// check for duplicates
for _, k := range b.GetKeys() {
if a.HasNode(k) {
if !a.CanConcat(k) {
Copy link
Member

Choose a reason for hiding this comment

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

This was the only caller of HasNode, can that be removed now? Can this implementation replace the previous one instead of introducing a new function?

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 didn't remove it as it is an exported method, and we should backport the bugfix to 6.x (best 6.6).

@@ -27,6 +27,58 @@ import (
"github.com/elastic/go-ucfg/yaml"
)

func TestFieldsHasNode(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

is this actually supposed to test CanConcat?

Copy link
Contributor Author

@simitt simitt Feb 28, 2019

Choose a reason for hiding this comment

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

Yes, will change.
There were no explicit tests for HasNode so I added them. Will add dedicated CanConcat tests though to make it more clear.

@simitt
Copy link
Contributor Author

simitt commented Feb 28, 2019

@elastic/beats could someone please review and lmk if you are fine with backporting to 7.0, 6.7 and 6.6 branches?

@@ -292,6 +292,46 @@ func (f Fields) getKeys(namespace string) []string {
return keys
}

// canConcat checks if inside fields the given node can be concatenated.
// If the node already exists, this is only allowed for nodes of type object.
func (f Fields) CanConcat(key string) bool {
Copy link

@urso urso Mar 4, 2019

Choose a reason for hiding this comment

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

Can we clarify the comment for when fields are concatenable. How about naming it conflicts ? This is what the for loop on ConcatFields does, look for conflicts between 2 field arrays.

canConcat checks that no node is reachable by key AND that the common prefix between all fields and the path given by key resolves into an object-node. Did I get this right?

We should return an error describing why key conflicts with f. Moving the loop from ConcatFields into a separate loop allows us to cleanly collect N errors into a multierror.

Do we need to export CanConcat? We seem to export too many symbols already. Let's try to not export symbols unless we know we will need them.

//// It's the last key to compare
if len(keys) == 0 {
return false
}
Copy link

Choose a reason for hiding this comment

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

nit:

  • ////
  • lets not add extra whitespace between for and if statements or beginning of function body. In most cases indentation should be enough to distinguish blocks.

@urso
Copy link

urso commented Mar 4, 2019

Logic LGTM.

@urso
Copy link

urso commented Mar 4, 2019

Jenkins, test this.

@simitt
Copy link
Contributor Author

simitt commented Mar 5, 2019

@urso I followed your suggestions, please have a look again.

@urso
Copy link

urso commented Mar 5, 2019

Note: Metricbeat windows tests didn't run due to build failure. All other tests have been green.

@simitt simitt merged commit 527fbd4 into elastic:master Mar 5, 2019
simitt added a commit to simitt/beats that referenced this pull request Mar 5, 2019
Checks if given field conflicts with current fields. Fixes an error where `append_fields` could not add valid field.
simitt added a commit to simitt/beats that referenced this pull request Mar 5, 2019
Checks if given field conflicts with current fields. Fixes an error where `append_fields` could not add valid field.
simitt added a commit to simitt/beats that referenced this pull request Mar 5, 2019
Checks if given field conflicts with current fields. Fixes an error where `append_fields` could not add valid field.
simitt added a commit that referenced this pull request Mar 5, 2019
Checks if given field conflicts with current fields. Fixes an error where `append_fields` could not add valid field.
simitt added a commit that referenced this pull request Mar 6, 2019
Checks if given field conflicts with current fields. Fixes an error where `append_fields` could not add valid field.
simitt added a commit that referenced this pull request Mar 6, 2019
Checks if given field conflicts with current fields. Fixes an error where `append_fields` could not add valid field.
@simitt simitt deleted the fix-field-has-node branch May 6, 2019 15:29
leweafan pushed a commit to leweafan/beats that referenced this pull request Apr 28, 2023
…11094)

Checks if given field conflicts with current fields. Fixes an error where `append_fields` could not add valid field.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug needs_backport PR is waiting to be backported to other branches.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants