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

Write json processor #4514

Merged
merged 5 commits into from
May 9, 2024
Merged

Write json processor #4514

merged 5 commits into from
May 9, 2024

Conversation

kkondaka
Copy link
Collaborator

@kkondaka kkondaka commented May 7, 2024

Description

A processor to write JSON string representation of an object

Issues Resolved

Resolves #832

Check List

  • [X ] New functionality includes testing.
  • New functionality has a documentation issue. Please link to it in this PR.
    • New functionality has javadoc added
  • [X ] 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.

Krishna Kondaka added 2 commits May 7, 2024 17:42
Signed-off-by: Krishna Kondaka <[email protected]>
Signed-off-by: Krishna Kondaka <[email protected]>
graytaylor0
graytaylor0 previously approved these changes May 7, 2024
try {
event.put(target, objectMapper.writeValueAsString(value));
} catch (Exception e) {
LOG.error("Failed to convert source to json string");
Copy link
Member

Choose a reason for hiding this comment

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

Can we log the exception message here? Could also have a metric for errors and tags as well, although that is not required for this PR

Copy link
Member

Choose a reason for hiding this comment

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

how should customer handle when the record is not converted into json string ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@dinujoh In pipeline, error cases are logged or metrics are incremented. There is not much we can do about it.

Signed-off-by: Krishna Kondaka <[email protected]>
try {
event.put(target, objectMapper.writeValueAsString(value));
} catch (Exception e) {
LOG.error("Failed to convert source to json string", e);
Copy link
Member

Choose a reason for hiding this comment

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

Does the exception contain user data ?

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 fix this as followup to mask customer data.

Copy link
Member

Choose a reason for hiding this comment

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

Here is an example of marker

LOG.error(DataPrepperMarkers.EVENT, "Failed to get IP address from [{}] in event: [{}]. Caused by:[{}]",

dinujoh
dinujoh previously approved these changes May 8, 2024
Signed-off-by: Krishna Kondaka <[email protected]>
Copy link
Collaborator

@KarstenSchnitter KarstenSchnitter left a comment

Choose a reason for hiding this comment

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

Implementation looks good. I am not sure, whether a test case for arrays should be added. Similar for integration tests.

Comment on lines +40 to +43
target = writeJsonProcessorConfig.getTarget();
if (target == null) {
target = source;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Using Java 9 Objects class would allow to make target final:

Suggested change
target = writeJsonProcessorConfig.getTarget();
if (target == null) {
target = source;
}
target = Objects.requireNonNullElse(writeJsonProcessorConfig.getTarget(), 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.

@KarstenSchnitter, I will keep that in mind for future. For now, I am keeping it as is.

Signed-off-by: Krishna Kondaka <[email protected]>
@@ -25,7 +26,9 @@
@DataPrepperPlugin(name = "write_json", pluginType = Processor.class, pluginConfigurationType = WriteJsonProcessorConfig.class)
public class WriteJsonProcessor extends AbstractProcessor<Record<Event>, Record<Event>> {
private static final Logger LOG = LoggerFactory.getLogger(WriteJsonProcessor.class);
private static final String WRITE_JSON_FAILED_COUNTER = "writeJsonFailedCounter";
Copy link
Member

Choose a reason for hiding this comment

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

Nit: could just name this writeJsonEventsFailed. Counter is a bit redundant.

@@ -48,7 +52,8 @@ public Collection<Record<Event>> doExecute(Collection<Record<Event>> records) {
try {
event.put(target, objectMapper.writeValueAsString(value));
} catch (Exception e) {
LOG.error("Failed to convert source to json string");
LOG.error("Failed to convert source to json string", e);
writeJsonFailedCounter.increment();
Copy link
Member

Choose a reason for hiding this comment

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

Could add a unit test to cover this case and verify metric is incremented

@kkondaka kkondaka merged commit f0bd8d8 into opensearch-project:main May 9, 2024
71 of 74 checks passed
@kkondaka kkondaka deleted the write-json branch May 13, 2024 05:50
@kkondaka kkondaka added this to the v2.8 milestone May 14, 2024
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.

Support encoding JSON
5 participants