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

Enhancements to map_to_list processor #4033

Merged
merged 11 commits into from
Feb 14, 2024

Conversation

oeyh
Copy link
Collaborator

@oeyh oeyh commented Jan 30, 2024

Description

Adds some enhancements to to map_to_list processor:

  • adds a convert_field_to_list option. If true, will convert fields to lists ([key, value]) instead of objects ({key: value})
  • allows source to be root when source is not specified
  • adds tags_on_failure option to add tags when events failed to process

Issues Resolved

Contributes to #3965

Check List

  • New functionality includes testing.
  • New functionality has a documentation issue. Please link to it in this PR.
    • New functionality has javadoc added
  • Commits are signed with a real name per the DCO

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

final Map<String, Object> sourceMap;
if (config.getSource() == null) {
// Source is root
sourceMap = OBJECT_MAPPER.treeToValue(recordEvent.getJsonNode(), Map.class);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I have a high level question here, if you get the entire JSON node if source is null, I think it's not going to work. Correct me if I am wrong.
For example, if the following is the event.

{
  "my-map": {
    "key1": "value1",
    "key2": "value2",
    "key3": "value3"
  }
}

The result will be

[
  ["my-map", {
    "key1": "value1",
    "key2": "value2",
    "key3": "value3"
  }]
]

Copy link
Collaborator

Choose a reason for hiding this comment

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

Good point. I think adding test cases and documentation for euch cases would help.

Copy link
Collaborator Author

@oeyh oeyh Feb 2, 2024

Choose a reason for hiding this comment

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

@asifsmohammed Good question, though it isn't necessarily related to root being the source. I think it's an issue with nested map in the source map. Currently we assume the source map structure is flat, so what you show is the expected result. We can have a recursive option to go deeper into each field in the future if needed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added readme and more test cases.

Copy link
Collaborator

@kkondaka kkondaka left a comment

Choose a reason for hiding this comment

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

Please document new config options in README.md. Also add examples for each option.

final Map<String, Object> sourceMap;
if (config.getSource() == null) {
// Source is root
sourceMap = OBJECT_MAPPER.treeToValue(recordEvent.getJsonNode(), Map.class);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Good point. I think adding test cases and documentation for euch cases would help.

}
}
return records;
}

private Map<String, Object> getSourceMap(Event recordEvent) throws JsonProcessingException {
final Map<String, Object> sourceMap;
if (config.getSource() == null) {
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 a desirable feature?

If so, can we make it take in / instead of null? I think this could lead to confusion when users don't set the source field they are probably not getting what they want.

Copy link
Collaborator Author

@oeyh oeyh Feb 2, 2024

Choose a reason for hiding this comment

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

Yes, we will need it for #3965.

If so, can we make it take in / instead of null? I think this could lead to confusion when users don't set the source field they are probably not getting what they want.

In JsonPointer specification, it is actually the empty string "" that refers to the whole document and "/" refers to the field with "" as key. If we remove the restrictions in JacksonEvent that key cannot be empty string, we should already support referring to the root document with Json Pointer "". What do you think of doing it this way?

Copy link
Collaborator Author

@oeyh oeyh Feb 5, 2024

Choose a reason for hiding this comment

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

Did some testing if we use "/" as source, it will try to get the value with empty string as key.

Copy link
Member

Choose a reason for hiding this comment

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

@oeyh , That is a very useful finding.

I think we will need to make the syntax be explicit then:

source: ''

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Implemented this in recent commits.

dlvenable
dlvenable previously approved these changes Feb 9, 2024
} else {
jsonPointerKey = SEPARATOR + key;
}
return jsonNode.at(jsonPointerKey);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this optimization we are doing? If yes, why not modify toJsonPointer() function? Or delete that function if that's not needed anymoer?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good point. Moved the changes to toJsonPointer() method. In the process, found some corner case issues with previous changes, addressed those and added more unit tests.

@oeyh oeyh force-pushed the conv-object-to-list-in-mtl branch from d3a05dd to 7e7b3a3 Compare February 13, 2024 22:49
Signed-off-by: Hai Yan <[email protected]>
@oeyh oeyh merged commit ab58c96 into opensearch-project:main Feb 14, 2024
44 of 47 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants