protected void handleError(String sensorType, byte[] originalMessage, Tuple tuple, Throwable ex, OutputCollector collector) { MetronError error = new MetronError() .withErrorType(Constants.ErrorType.PARSER_ERROR) .withThrowable(ex) .withSensorType(Collections.singleton(sensorType)) .addRawMessage(originalMessage); ErrorUtils.handleError(collector, error); }
/** * Error a set of tuples that may not contain a valid message. * * <p>Without a valid message, the source type is unknown. * <p>Without a valid message, the JSON message cannot be added to the error. * * @param e The exception that occurred. * @param tuple The tuple to error that may not contain a valid message. */ public void error(Throwable e, Tuple tuple) { LOG.error("Failing tuple", e); MetronError error = new MetronError() .withErrorType(Constants.ErrorType.INDEXING_ERROR) .withThrowable(e); handleError(tuple, error); }
public static void handleError(OutputCollector collector, MetronError error) { collector.emit(Constants.ERROR_STREAM, new Values(error.getJSONObject())); Optional<Throwable> throwable = error.getThrowable(); if (throwable.isPresent()) { collector.reportError(throwable.get()); } }
@SuppressWarnings("unchecked") @Test public void testExecuteShouldReportError() throws ExecutionException { joinBolt.withMaxCacheSize(100); joinBolt.withMaxTimeRetain(10000); joinBolt.prepare(new HashMap(), topologyContext, outputCollector); when(tuple.getValueByField("key")).thenReturn(key); when(tuple.getValueByField("message")).thenReturn(new JSONObject()); joinBolt.cache = mock(LoadingCache.class); when(joinBolt.cache.get(any())).thenThrow(new RuntimeException(new Exception("join exception"))); joinBolt.execute(tuple); RuntimeException expectedExecutionException = new RuntimeException(new Exception("join exception")); MetronError error = new MetronError() .withErrorType(Constants.ErrorType.ENRICHMENT_ERROR) .withMessage("Joining problem: {}") .withThrowable(expectedExecutionException) .addRawMessage(new JSONObject()); verify(outputCollector, times(1)).emit(eq(Constants.ERROR_STREAM), argThat(new MetronErrorJSONMatcher(error.getJSONObject()))); verify(outputCollector, times(1)).reportError(any(ExecutionException.class)); verify(outputCollector, times(1)).ack(eq(tuple)); verifyNoMoreInteractions(outputCollector); } }
@Test public void getJSONObjectShouldReturnBasicInformation() { MetronError error = new MetronError() .withMessage("test message") .withErrorType(Constants.ErrorType.PARSER_ERROR) .withSensorType(Collections.singleton("sensorType")); JSONObject errorJSON = error.getJSONObject(); assertEquals("test message", errorJSON.get(Constants.ErrorFields.MESSAGE.getName())); assertEquals(Constants.ErrorType.PARSER_ERROR.getType(), errorJSON.get(Constants.ErrorFields.ERROR_TYPE.getName())); assertEquals("error", errorJSON.get(Constants.SENSOR_TYPE)); assertEquals("sensorType", errorJSON.get(Constants.ErrorFields.FAILED_SENSOR_TYPE.getName())); String hostName = null; try { hostName = InetAddress.getLocalHost().getHostName(); } catch (UnknownHostException uhe) { // unable to get the hostname on this machine, don't test it } if (!StringUtils.isEmpty(hostName)) { assertTrue(((String) errorJSON.get(Constants.ErrorFields.HOSTNAME.getName())).length() > 0); assertEquals(hostName, (String) errorJSON.get(Constants.ErrorFields.HOSTNAME.getName())); } assertTrue(((long) errorJSON.get(Constants.ErrorFields.TIMESTAMP.getName())) > 0); }
@Test public void getJSONObjectShouldIncludeErrorFields() { JSONObject message = new JSONObject(); message.put("field1", "value1"); message.put("field2", "value2"); MetronError error = new MetronError().addRawMessage(message).withErrorFields(Sets.newHashSet("field1", "field2")); JSONObject errorJSON = error.getJSONObject(); assertEquals(Sets.newHashSet("field1", "field2"), Sets.newHashSet(((String) errorJSON.get(Constants.ErrorFields.ERROR_FIELDS.getName())).split(","))); assertEquals("04a2629c39e098c3944be85f35c75876598f2b44b8e5e3f52c59fa1ac182817c", errorJSON.get(Constants.ErrorFields.ERROR_HASH.getName())); } }
@Test public void handleErrorShouldEmitAndReportError() throws Exception { Throwable e = new Exception("error"); MetronError error = new MetronError().withMessage("error message").withThrowable(e); OutputCollector collector = mock(OutputCollector.class); ErrorUtils.handleError(collector, error); verify(collector, times(1)).emit(eq(Constants.ERROR_STREAM), argThat(new MetronErrorJSONMatcher(error.getJSONObject()))); verify(collector, times(1)).reportError(any()); } }
@Test public void getJSONObjectShouldIncludeRawMessages() { JSONObject message1 = new JSONObject(); JSONObject message2 = new JSONObject(); message1.put("value", "message1"); message2.put("value", "message2"); MetronError error = new MetronError().withRawMessages(Arrays.asList(message1, message2)); JSONObject errorJSON = error.getJSONObject(); assertEquals("{\"value\":\"message1\"}", errorJSON.get(Constants.ErrorFields.RAW_MESSAGE.getName() + "_0")); assertEquals("{\"value\":\"message2\"}", errorJSON.get(Constants.ErrorFields.RAW_MESSAGE.getName() + "_1")); error = new MetronError().addRawMessage("raw message".getBytes()); errorJSON = error.getJSONObject(); assertEquals("raw message", errorJSON.get(Constants.ErrorFields.RAW_MESSAGE.getName())); // It's unclear if we need a rawMessageBytes field so commenting out for now //assertEquals(Bytes.asList("raw message".getBytes()), errorJSON.get(Constants.ErrorFields.RAW_MESSAGE_BYTES.getName())); assertEquals("3b02cb29676bc448c69da1ec5eef7c89f4d6dc6a5a7ce0296ea25b207eea36be", errorJSON.get(Constants.ErrorFields.ERROR_HASH.getName())); error = new MetronError().addRawMessage(message1); errorJSON = error.getJSONObject(); assertEquals("{\"value\":\"message1\"}", errorJSON.get(Constants.ErrorFields.RAW_MESSAGE.getName())); assertEquals("e8aaf87c8494d345aac2d612ffd94fcf0b98c975fe6c4b991e2f8280a3a0bd10", errorJSON.get(Constants.ErrorFields.ERROR_HASH.getName())); }
@Test public void getJSONObjectShouldHandleThrowable() { Throwable e = new Exception("test exception"); MetronError error = new MetronError().withThrowable(e); JSONObject errorJSON = error.getJSONObject(); assertEquals("java.lang.Exception: test exception", errorJSON.get(Constants.ErrorFields.EXCEPTION.getName())); assertTrue(((String) errorJSON.get(Constants.ErrorFields.STACK.getName())).startsWith("java.lang.Exception: test exception")); assertEquals(e.getMessage(), errorJSON.get(Constants.ErrorFields.MESSAGE.getName())); }
@SuppressWarnings({"unchecked"}) public JSONObject getJSONObject() { JSONObject errorMessage = new JSONObject(); errorMessage.put(Constants.GUID, UUID.randomUUID().toString()); errorMessage.put(Constants.SENSOR_TYPE, "error"); if (sensorTypes.size() == 1) { errorMessage.put(ErrorFields.FAILED_SENSOR_TYPE.getName(), sensorTypes.iterator().next()); } else { errorMessage .put(ErrorFields.FAILED_SENSOR_TYPE.getName(), new JSONArray().addAll(sensorTypes)); } errorMessage.put(ErrorFields.ERROR_TYPE.getName(), errorType.getType()); addMessageString(errorMessage); addStacktrace(errorMessage); addTimestamp(errorMessage); addHostname(errorMessage); addRawMessages(errorMessage); addErrorHash(errorMessage); return errorMessage; }
private List<JSONObject> doParse(List<String> messages) { // initialize HashSet<String> sensorTypes = new HashSet<>(); sensorTypes.add(sensorType); ParserRunnerImpl runner = new ParserRunnerImpl(sensorTypes); runner.init(() -> parserConfigurations, context); // parse each message List<ParserRunnerResults<JSONObject>> results = messages .stream() .map(str -> str.getBytes()) .map(bytes -> DEFAULT.get(emptyMap(), bytes, false, emptyMap())) .map(msg -> runner.execute(sensorType, msg, parserConfigurations)) .collect(Collectors.toList()); // aggregate both successes and errors into a list that can be returned List<JSONObject> successes = results .stream() .flatMap(result -> result.getMessages().stream()) .collect(Collectors.toList()); successCount += successes.size(); List<JSONObject> errors = results .stream() .flatMap(result -> result.getErrors().stream()) .map(err -> err.getJSONObject()) .collect(Collectors.toList()); errorCount += errors.size(); // return a list of both successes and errors successes.addAll(errors); return successes; }
@Override public int hashCode() { int result = message != null ? message.hashCode() : 0; result = 31 * result + (getThrowable() != null ? getThrowable().hashCode() : 0); result = 31 * result + (sensorTypes != null ? sensorTypes.hashCode() : 0); result = 31 * result + (errorType != null ? errorType.hashCode() : 0); result = 31 * result + (errorFields != null ? errorFields.hashCode() : 0); result = 31 * result + (rawMessages != null ? rawMessages.hashCode() : 0); return result; } }
protected void handleError(String key, JSONObject rawMessage, String subGroup, JSONObject enrichedMessage, Exception e) { LOG.error("[Metron] Unable to enrich message: {}", rawMessage, e); if (key != null) { collector.emit(enrichmentType, new Values(key, enrichedMessage, subGroup)); } MetronError error = new MetronError() .withErrorType(Constants.ErrorType.ENRICHMENT_ERROR) .withThrowable(e) .addRawMessage(rawMessage); ErrorUtils.handleError(collector, error); }
@Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } MetronError that = (MetronError) o; if (message != null ? !message.equals(that.message) : that.message != null) { return false; } if (getThrowable() != null ? !getThrowable().equals(that.getThrowable()) : that.getThrowable() != null) { return false; } if (sensorTypes != null ? !sensorTypes.equals(that.sensorTypes) : that.sensorTypes != null) { return false; } if (errorType != that.errorType) { return false; } if (errorFields != null ? !errorFields.equals(that.errorFields) : that.errorFields != null) { return false; } return rawMessages != null ? rawMessages.equals(that.rawMessages) : that.rawMessages == null; }
@Override public void execute(Tuple tuple) { JSONObject message = null; try { message = (JSONObject) messageGetStrategy.get(tuple); handler.write(sensorType, tuple, message, configuration, messageGetStrategy); if(!handler.handleAck()) { collector.ack(tuple); } } catch (Throwable e) { MetronError error = new MetronError() .withErrorType(errorType) .withThrowable(e) .withSensorType(Collections.singleton(sensorType)) .addRawMessage(message); ErrorUtils.handleError(collector, error); collector.ack(tuple); } }
@Override public ParserRunnerResults<JSONObject> execute(String sensorType, RawMessage rawMessage, ParserConfigurations parserConfigurations) { DefaultParserRunnerResults parserRunnerResults = new DefaultParserRunnerResults(); this.rawMessage = rawMessage; if (!isInvalid) { parserRunnerResults.addMessage(message); } else { MetronError error = new MetronError() .withErrorType(Constants.ErrorType.PARSER_INVALID) .withSensorType(Collections.singleton(sensorType)) .addRawMessage(message); parserRunnerResults.addError(error); } return parserRunnerResults; }
public void error(String sensorType, Throwable e, Iterable<Tuple> tuples, MessageGetStrategy messageGetStrategy) { LOG.error(format("Failing %d tuple(s); sensorType=%s", Iterables.size(tuples), sensorType), e); tuples.forEach(t -> { MetronError error = new MetronError() .withSensorType(Collections.singleton(sensorType)) .withErrorType(Constants.ErrorType.INDEXING_ERROR) .withThrowable(e) .addRawMessage(messageGetStrategy.get(t)); collector.emit(Constants.ERROR_STREAM, new Values(error.getJSONObject())); }); if (handleCommit) { commit(tuples); } // there is only one error to report for all of the failed tuples collector.reportError(e); }
@Test public void testNonBatchErrorPathErrorInWrite() throws Exception { ParserConfigurations configurations = getConfigurations(1); String sensorType = "test"; Tuple t = mock(Tuple.class); when(t.toString()).thenReturn("tuple"); when(t.getValueByField(eq("message"))).thenReturn(new JSONObject()); WriterBolt bolt = new WriterBolt(new WriterHandler(writer), configurations, sensorType); bolt.prepare(new HashMap(), topologyContext, outputCollector); doThrow(new Exception("write error")).when(writer).write(any(), any(), any(), any()); verify(writer, times(1)).init(); bolt.execute(t); verify(outputCollector, times(1)).ack(t); verify(writer, times(1)).write(eq(sensorType), any(), any(), any()); verify(outputCollector, times(1)).reportError(any()); verify(outputCollector, times(0)).fail(any()); MetronError error = new MetronError() .withErrorType(Constants.ErrorType.DEFAULT_ERROR) .withThrowable(new IllegalStateException("Unhandled bulk errors in response: {java.lang.Exception: write error=[tuple]}")) .withSensorType(Collections.singleton(sensorType)) .addRawMessage(new JSONObject()); verify(outputCollector, times(1)).emit(eq(Constants.ERROR_STREAM), argThat(new MetronErrorJSONMatcher(error.getJSONObject()))); }
@Test public void shouldExecuteWithMasterThrowable() { parserRunner = spy(parserRunner); RawMessage rawMessage = new RawMessage("raw_message".getBytes(), new HashMap<>()); Throwable masterThrowable = mock(Throwable.class); MessageParserResult<JSONObject> messageParserResult = new DefaultMessageParserResult<>(masterThrowable); when(broParser.parseOptionalResult(rawMessage.getMessage())).thenReturn(Optional.of(messageParserResult)); parserRunner.setSensorToParserComponentMap(new HashMap<String, ParserComponent>() {{ put("bro", new ParserComponent(broParser, stellarFilter)); }}); ParserRunnerResults<JSONObject> parserRunnerResults = parserRunner.execute("bro", rawMessage, parserConfigurations); verify(parserRunner, times(0)) .processMessage(any(), any(), any(), any(), any()); MetronError expectedError = new MetronError() .withErrorType(Constants.ErrorType.PARSER_ERROR) .withThrowable(masterThrowable) .withSensorType(Collections.singleton("bro")) .addRawMessage(rawMessage.getMessage()); Assert.assertEquals(1, parserRunnerResults.getErrors().size()); Assert.assertTrue(parserRunnerResults.getErrors().contains(expectedError)); }
@Test public void shouldReturnMetronErrorOnInvalidMessage() { JSONObject inputMessage = new JSONObject(); inputMessage.put("guid", "guid"); RawMessage rawMessage = new RawMessage("raw_message".getBytes(), new HashMap<>()); JSONObject expectedOutput = new JSONObject(); expectedOutput.put("guid", "guid"); expectedOutput.put("source.type", "bro"); MetronError expectedMetronError = new MetronError() .withErrorType(Constants.ErrorType.PARSER_INVALID) .withSensorType(Collections.singleton("bro")) .addRawMessage(inputMessage); when(stellarFilter.emit(expectedOutput, parserRunner.getStellarContext())).thenReturn(true); when(broParser.validate(expectedOutput)).thenReturn(false); parserRunner.setSensorToParserComponentMap(new HashMap<String, ParserComponent>() {{ put("bro", new ParserComponent(broParser, stellarFilter)); }}); Optional<ParserRunnerImpl.ProcessResult> processResult = parserRunner.processMessage("bro", inputMessage, rawMessage, broParser, parserConfigurations); Assert.assertTrue(processResult.isPresent()); Assert.assertTrue(processResult.get().isError()); Assert.assertEquals(expectedMetronError, processResult.get().getError()); }