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

Fix tags type to []interface{} in event (#5389) #5395

Merged
merged 1 commit into from
Oct 25, 2017

Conversation

liketic
Copy link

@liketic liketic commented Oct 14, 2017

Closes #5389 @urso Could you please review this? 😄

@elasticmachine
Copy link
Collaborator

Can one of the admins verify this patch?

@liketic
Copy link
Author

liketic commented Oct 14, 2017

Maybe we should support both []string and []interface{} like:

       if tagsIfc, ok := ms[TagsKey]; ok {
		switch tagsIfc.(type) {
		case []interface{}:
			existingTags := tagsIfc.([]interface{})
			for _, tag := range tags {
				existingTags = append(existingTags, tag)
			}
			ms[TagsKey] = existingTags
		case []string:
			existingTags := tagsIfc.([]string)
			existingTags = append(existingTags, tags...)
			ms[TagsKey] = existingTags
		default:
			return errors.Errorf("expected string array by type is %T", tagsIfc)
		}
	} else {
		ms[TagsKey] = tags
	}

@ruflin
Copy link
Contributor

ruflin commented Oct 16, 2017

@liketic Thanks for the fix. This could also use a CHANGELOG entry. The code review I let to @urso .

Copy link

@urso urso left a comment

Choose a reason for hiding this comment

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

just changing []string to []interface{} unfortunately won't be enough. We need to support both, as the internal tags from config files are correctly parsed as []string.

@urso
Copy link

urso commented Oct 16, 2017

The type-switch use-case from your comment is definitely better. With some cleanup it becomes:

  T, exists := ms[TagsKey]
  if !exists {
    ms[TagsKey] = tags
    return nil
  }

  switch arr := T.(type) {
    case []string:
      ms[TagsKey] = append(arr, tags...)
    case []interface:
      for _, tag := range tags {
        arr = append(arr, tag)
      }
      ms[TagsKey] = arr
    default:
      return errors....
  }

  return nil

In addition one can change this event normalization/parsing code to also handle the tags keyword. But once AddTags supports []string and []interface{} it should be fine.

Please add a Changlog entry when you are done.

As this fixes an issue with filebeat, having a filebeat system test would be nice to have. Check for tests/system/test_json.py.

@liketic
Copy link
Author

liketic commented Oct 16, 2017

@urso I updated the pr, please review again. Thanks!

@liketic
Copy link
Author

liketic commented Oct 16, 2017

Sorry I didn't notice your comments, will update again.

@liketic
Copy link
Author

liketic commented Oct 18, 2017

Hi @urso , I didn't find a way to run the system test system/test_json.py, could you please provide some help? 😃

@urso
Copy link

urso commented Oct 18, 2017

system tests are run via nosetest. You can setup the python environment from within the filebeat directory via

$ make python-env
$ source build/python-env/bin/active

head over to tests/system and run the tests via nosetest test_json.py.

@@ -367,3 +390,4 @@ def test_integer_condition(self):
output = self.read_output()
assert len(output) == 1
assert output[0]["status"] == 404

Copy link

Choose a reason for hiding this comment

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

Not sure, but the linter might complain about a newline at the end of the file.

proc.check_kill_and_wait()

output = self.read_output()[0]
assert sorted(output["tags"]) == ["tag1", "tag2", "tag3", "tag4"]
Copy link

Choose a reason for hiding this comment

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

👍

@urso
Copy link

urso commented Oct 23, 2017

PR looks good to me. Thanks for taking the time!!! Also the filebeat system test are much appreciated!

Unfortunately there is a conflict in the changelog file (github sometimes has problem with the changelog). Please rebase and check git not introducing any more errors (e.g. wrong changelog entries). This should also restart the tests, which have not been executed at all :(

Support append tags to both []string and []interface{}

Add system test and CHANGELOG
@liketic
Copy link
Author

liketic commented Oct 25, 2017

@urso I have to say I didn't get it passed on my mac... Because I always got a error file not found seems like the root cause is the file filebeat.test not generated. It's too hard to do the test without a guide. And also, seems like the permission of a lot of files is root user access only. Is that expected behaviour? Is the testing failure caused by my changes? Thanks very much!

@urso
Copy link

urso commented Oct 25, 2017

I see. you can build filebeat.test via make filebeat.test.

No idea why files are owned by root user. Did you run via docker? Try sudo make clean.

@urso
Copy link

urso commented Oct 25, 2017

I checked CI and your tests did pass. It's some flaky kafka tests in metricbeat that have failed.

Will merge PR. Thanks for taking to time fix the issue.

@urso urso merged commit 5541503 into elastic:master Oct 25, 2017
@liketic liketic deleted the issues/5389 branch October 25, 2017 13:51
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.

4 participants