Skip to content

Commit

Permalink
Logging improvements when failing to parse JSON/XML/ION. Do not inclu…
Browse files Browse the repository at this point in the history
…de the stack trace since it doesn't provide any value with these exceptions which are expected when the JSON is invalid. Log the input string rather than the Event which was not readable. (opensearch-project#4839)

Signed-off-by: David Venable <[email protected]>
  • Loading branch information
dlvenable authored Aug 16, 2024
1 parent 72e2db7 commit 79db359
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
1 change: 1 addition & 0 deletions data-prepper-plugins/parse-json-processor/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dependencies {
implementation libs.parquet.common
testImplementation project(':data-prepper-test-common')
testImplementation project(':data-prepper-test-event')
testImplementation testLibs.slf4j.simple
}

test {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import java.util.HashMap;
import java.util.Optional;

import static org.opensearch.dataprepper.logging.DataPrepperMarkers.EVENT;
import static org.opensearch.dataprepper.logging.DataPrepperMarkers.SENSITIVE;

@DataPrepperPlugin(name = "parse_ion", pluginType = Processor.class, pluginConfigurationType = ParseIonProcessorConfig.class)
public class ParseIonProcessor extends AbstractParseProcessor {
Expand All @@ -45,10 +45,10 @@ protected Optional<HashMap<String, Object>> readValue(String message, Event cont
// We need to do a two-step process here, read the value in, then convert away any Ion types like Timestamp
return Optional.of(objectMapper.convertValue(objectMapper.readValue(message, new TypeReference<>() {}), new TypeReference<>() {}));
} catch (JsonProcessingException e) {
LOG.error(EVENT, "An exception occurred due to invalid Ion while reading event [{}]", context, e);
LOG.error(SENSITIVE, "An exception occurred due to invalid Ion while parsing [{}] due to {}", message, e.getMessage());
return Optional.empty();
} catch (Exception e) {
LOG.error(EVENT, "An exception occurred while using the parse_ion processor on Event [{}]", context, e);
LOG.error(SENSITIVE, "An exception occurred while using the parse_ion processor while parsing [{}]", message, e);
return Optional.empty();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@

package org.opensearch.dataprepper.plugins.processor.parse.json;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.opensearch.dataprepper.expression.ExpressionEvaluator;
import org.opensearch.dataprepper.metrics.PluginMetrics;
import org.opensearch.dataprepper.model.annotations.DataPrepperPlugin;
import org.opensearch.dataprepper.model.annotations.DataPrepperPluginConstructor;
import org.opensearch.dataprepper.model.event.Event;
import org.opensearch.dataprepper.model.processor.Processor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.opensearch.dataprepper.plugins.processor.parse.AbstractParseProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Optional;

import static org.opensearch.dataprepper.logging.DataPrepperMarkers.EVENT;
import static org.opensearch.dataprepper.logging.DataPrepperMarkers.SENSITIVE;

@DataPrepperPlugin(name = "parse_json", pluginType = Processor.class, pluginConfigurationType = ParseJsonProcessorConfig.class)
public class ParseJsonProcessor extends AbstractParseProcessor {
Expand All @@ -41,10 +41,10 @@ protected Optional<HashMap<String, Object>> readValue(String message, Event cont
try {
return Optional.of(objectMapper.readValue(message, new TypeReference<>() {}));
} catch (JsonProcessingException e) {
LOG.error(EVENT, "An exception occurred due to invalid JSON while reading event [{}]", context, e);
LOG.error(SENSITIVE, "An exception occurred due to invalid JSON while parsing [{}] due to {}", message, e.getMessage());
return Optional.empty();
} catch (Exception e) {
LOG.error(EVENT, "An exception occurred while using the parse_json processor on Event [{}]", context, e);
LOG.error(SENSITIVE, "An exception occurred while using the parse_json processor while parsing [{}]", message, e);
return Optional.empty();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import java.util.HashMap;
import java.util.Optional;

import static org.opensearch.dataprepper.logging.DataPrepperMarkers.EVENT;
import static org.opensearch.dataprepper.logging.DataPrepperMarkers.SENSITIVE;

@DataPrepperPlugin(name = "parse_xml", pluginType =Processor.class, pluginConfigurationType =ParseXmlProcessorConfig.class)
public class ParseXmlProcessor extends AbstractParseProcessor {
Expand All @@ -36,10 +36,10 @@ protected Optional<HashMap<String, Object>> readValue(final String message, fina
try {
return Optional.of(xmlMapper.readValue(message, new TypeReference<>() {}));
} catch (JsonProcessingException e) {
LOG.error(EVENT, "An exception occurred due to invalid XML while reading event [{}]", context, e);
LOG.error(SENSITIVE, "An exception occurred due to invalid XML while parsing [{}] due to {}", message, e.getMessage());
return Optional.empty();
} catch (Exception e) {
LOG.error(EVENT, "An exception occurred while using the parse_xml processor on Event [{}]", context, e);
LOG.error(SENSITIVE, "An exception occurred while using the parse_xml processor while parsing [{}]", message, e);
return Optional.empty();
}
}
Expand Down

0 comments on commit 79db359

Please sign in to comment.