Search in sources :

Example 1 with MessageParserResult

use of org.apache.metron.parsers.interfaces.MessageParserResult in project metron by apache.

the class MultiLineGrokParserTest method testOptionalResultReturnsMultiline.

/**
 * Test that if a byte[] with multiple lines of log is passed in
 * it will be parsed into the correct number of messages using the
 * parseOptionalResult call.
 */
@Test
@SuppressWarnings("unchecked")
public void testOptionalResultReturnsMultiline() {
    Map<String, Object> parserConfig = new HashMap<>();
    parserConfig.put("grokPath", getGrokPath());
    parserConfig.put("patternLabel", getGrokPatternLabel());
    parserConfig.put("timestampField", getTimestampField());
    parserConfig.put("dateFormat", getDateFormat());
    parserConfig.put("timeFields", getTimeFields());
    parserConfig.put("multiLine", getMultiLine());
    GrokParser grokParser = new GrokParser();
    grokParser.configure(parserConfig);
    grokParser.init();
    JSONParser jsonParser = new JSONParser();
    Map<String, String> testData = getTestData();
    for (Map.Entry<String, String> e : testData.entrySet()) {
        byte[] rawMessage = e.getKey().getBytes(StandardCharsets.UTF_8);
        Optional<MessageParserResult<JSONObject>> resultOptional = grokParser.parseOptionalResult(rawMessage);
        assertTrue(resultOptional.isPresent());
        Optional<Throwable> throwableOptional = resultOptional.get().getMasterThrowable();
        List<JSONObject> resultList = resultOptional.get().getMessages();
        Map<Object, Throwable> errorMap = resultOptional.get().getMessageThrowables();
        assertFalse(throwableOptional.isPresent());
        assertEquals(0, errorMap.size());
        assertEquals(10, resultList.size());
    }
}
Also used : MessageParserResult(org.apache.metron.parsers.interfaces.MessageParserResult) JSONObject(org.json.simple.JSONObject) JSONObject(org.json.simple.JSONObject) JSONParser(org.json.simple.parser.JSONParser) Test(org.junit.jupiter.api.Test)

Example 2 with MessageParserResult

use of org.apache.metron.parsers.interfaces.MessageParserResult in project metron by apache.

the class MultiLineGrokParserTest method testLegacyInterfaceReturnsMultiline.

/**
 * Test that if a byte[] with multiple lines of log is passed in
 * it will be parsed into the correct number of messages.
 */
@Test
@SuppressWarnings("unchecked")
public void testLegacyInterfaceReturnsMultiline() {
    Map<String, Object> parserConfig = new HashMap<>();
    parserConfig.put("grokPath", getGrokPath());
    parserConfig.put("patternLabel", getGrokPatternLabel());
    parserConfig.put("timestampField", getTimestampField());
    parserConfig.put("dateFormat", getDateFormat());
    parserConfig.put("timeFields", getTimeFields());
    parserConfig.put("multiLine", getMultiLine());
    GrokParser grokParser = new GrokParser();
    grokParser.configure(parserConfig);
    grokParser.init();
    JSONParser jsonParser = new JSONParser();
    Map<String, String> testData = getTestData();
    for (Map.Entry<String, String> e : testData.entrySet()) {
        byte[] rawMessage = e.getKey().getBytes(StandardCharsets.UTF_8);
        Optional<MessageParserResult<JSONObject>> resultOptional = grokParser.parseOptionalResult(rawMessage);
        assertNotNull(resultOptional);
        assertTrue(resultOptional.isPresent());
        List<JSONObject> parsedList = resultOptional.get().getMessages();
        assertEquals(10, parsedList.size());
    }
}
Also used : MessageParserResult(org.apache.metron.parsers.interfaces.MessageParserResult) JSONObject(org.json.simple.JSONObject) JSONObject(org.json.simple.JSONObject) JSONParser(org.json.simple.parser.JSONParser) Test(org.junit.jupiter.api.Test)

Example 3 with MessageParserResult

use of org.apache.metron.parsers.interfaces.MessageParserResult in project metron by apache.

the class ParserRunnerImpl method execute.

/**
 * Parses messages with the appropriate MessageParser based on sensor type.  The resulting list of messages are then
 * post-processed and added to the ParserRunnerResults message list.  Any errors that happen during post-processing are
 * added to the ParserRunnerResults error list.  Any exceptions (including a master exception) thrown by the MessageParser
 * are also added to the ParserRunnerResults error list.
 *
 * @param sensorType Sensor type of the message
 * @param rawMessage Raw message including metadata
 * @param parserConfigurations Parser configurations
 * @return ParserRunnerResults containing a list of messages and a list of errors
 */
@Override
public ParserRunnerResults<JSONObject> execute(String sensorType, RawMessage rawMessage, ParserConfigurations parserConfigurations) {
    DefaultParserRunnerResults parserRunnerResults = new DefaultParserRunnerResults();
    SensorParserConfig sensorParserConfig = parserConfigurations.getSensorParserConfig(sensorType);
    if (sensorParserConfig != null) {
        MessageParser<JSONObject> parser = sensorToParserComponentMap.get(sensorType).getMessageParser();
        Optional<MessageParserResult<JSONObject>> optionalMessageParserResult = parser.parseOptionalResult(rawMessage.getMessage());
        if (optionalMessageParserResult.isPresent()) {
            MessageParserResult<JSONObject> messageParserResult = optionalMessageParserResult.get();
            // Process each message returned from the MessageParser
            messageParserResult.getMessages().forEach(message -> {
                Optional<ProcessResult> processResult = processMessage(sensorType, message, rawMessage, parser, parserConfigurations);
                if (processResult.isPresent()) {
                    if (processResult.get().isError()) {
                        parserRunnerResults.addError(processResult.get().getError());
                    } else {
                        parserRunnerResults.addMessage(processResult.get().getMessage());
                    }
                }
            });
            // If a master exception is thrown by the MessageParser, wrap it with a MetronError and add it to the list of errors
            messageParserResult.getMasterThrowable().ifPresent(throwable -> parserRunnerResults.addError(new MetronError().withErrorType(Constants.ErrorType.PARSER_ERROR).withThrowable(throwable).withSensorType(Collections.singleton(sensorType)).withMetadata(rawMessage.getMetadata()).addRawMessage(rawMessage.getMessage())));
            // If exceptions are thrown by the MessageParser, wrap them with MetronErrors and add them to the list of errors
            parserRunnerResults.addErrors(messageParserResult.getMessageThrowables().entrySet().stream().map(entry -> new MetronError().withErrorType(Constants.ErrorType.PARSER_ERROR).withThrowable(entry.getValue()).withSensorType(Collections.singleton(sensorType)).withMetadata(rawMessage.getMetadata()).addRawMessage(entry.getKey())).collect(Collectors.toList()));
        }
    } else {
        throw new IllegalStateException(String.format("Could not execute parser.  Cannot find configuration for sensor %s.", sensorType));
    }
    return parserRunnerResults;
}
Also used : MessageParserResult(org.apache.metron.parsers.interfaces.MessageParserResult) JSONObject(org.json.simple.JSONObject) MetronError(org.apache.metron.common.error.MetronError) SensorParserConfig(org.apache.metron.common.configuration.SensorParserConfig)

Example 4 with MessageParserResult

use of org.apache.metron.parsers.interfaces.MessageParserResult in project metron by apache.

the class MessageParserTest method testParse.

@Test
public void testParse() {
    JSONObject message = new JSONObject();
    MessageParser<JSONObject> parser = new TestMessageParser() {

        @Override
        public List<JSONObject> parse(byte[] rawMessage) {
            return Collections.singletonList(message);
        }
    };
    Optional<MessageParserResult<JSONObject>> ret = parser.parseOptionalResult("message".getBytes(StandardCharsets.UTF_8));
    assertTrue(ret.isPresent());
    assertEquals(1, ret.get().getMessages().size());
    assertEquals(message, ret.get().getMessages().get(0));
}
Also used : MessageParserResult(org.apache.metron.parsers.interfaces.MessageParserResult) JSONObject(org.json.simple.JSONObject) Test(org.junit.jupiter.api.Test)

Example 5 with MessageParserResult

use of org.apache.metron.parsers.interfaces.MessageParserResult in project metron by apache.

the class MultiLineWithErrorsGrokParserTest method testResultInterfaceReturnsErrorsAndMessagesWithMultiline.

/**
 * Test that if a byte[] with multiple lines of log is passed in
 * it will be parsed into the correct number of messages using the
 * parseOptionalResult call.
 */
@Test
@SuppressWarnings("unchecked")
public void testResultInterfaceReturnsErrorsAndMessagesWithMultiline() {
    Map<String, Object> parserConfig = new HashMap<>();
    parserConfig.put("grokPath", getGrokPath());
    parserConfig.put("patternLabel", getGrokPatternLabel());
    parserConfig.put("timestampField", getTimestampField());
    parserConfig.put("dateFormat", getDateFormat());
    parserConfig.put("timeFields", getTimeFields());
    parserConfig.put("multiLine", getMultiLine());
    GrokParser grokParser = new GrokParser();
    grokParser.configure(parserConfig);
    grokParser.init();
    JSONParser jsonParser = new JSONParser();
    Map<String, String> testData = getTestData();
    for (Map.Entry<String, String> e : testData.entrySet()) {
        byte[] rawMessage = e.getKey().getBytes(StandardCharsets.UTF_8);
        Optional<MessageParserResult<JSONObject>> resultOptional = grokParser.parseOptionalResult(rawMessage);
        assertTrue(resultOptional.isPresent());
        Optional<Throwable> throwableOptional = resultOptional.get().getMasterThrowable();
        List<JSONObject> resultList = resultOptional.get().getMessages();
        Map<Object, Throwable> errorMap = resultOptional.get().getMessageThrowables();
        assertFalse(throwableOptional.isPresent());
        assertEquals(3, errorMap.size());
        assertEquals(10, resultList.size());
    }
}
Also used : MessageParserResult(org.apache.metron.parsers.interfaces.MessageParserResult) JSONObject(org.json.simple.JSONObject) JSONObject(org.json.simple.JSONObject) JSONParser(org.json.simple.parser.JSONParser) Test(org.junit.jupiter.api.Test)

Aggregations

MessageParserResult (org.apache.metron.parsers.interfaces.MessageParserResult)15 JSONObject (org.json.simple.JSONObject)15 Test (org.junit.jupiter.api.Test)14 JSONParser (org.json.simple.parser.JSONParser)4 HashMap (java.util.HashMap)1 Map (java.util.Map)1 SensorParserConfig (org.apache.metron.common.configuration.SensorParserConfig)1 MetronError (org.apache.metron.common.error.MetronError)1