ElasticSearchMonitorJob implements Job { private void publishError(Throwable e) { Instant now = Instant.now(); var message = new StatMessage(); message.id = LogManager.ID_GENERATOR.next(now); message.date = now; message.result = "ERROR"; message.app = app; message.host = host; message.errorCode = "FAILED_TO_COLLECT"; message.errorMessage = e.getMessage(); message.info = Map.of("stack_trace", Exceptions.stackTrace(e)); publisher.publish(message); } ElasticSearchMonitorJob(ElasticSearchClient elasticSearchClient, String app, String host, MessagePublisher<StatMessage> publisher); @Override void execute(JobContext context); public double highHeapUsageThreshold; public double highDiskUsageThreshold; }
@Test void publishError() throws IOException { when(elasticSearchClient.stats("localhost")).thenThrow(new HTTPClientException("mock", "MOCK_ERROR_CODE", null)); job.execute(null); verify(publisher).publish(argThat(message -> "es".equals(message.app) && "ERROR".equals(message.result) && "FAILED_TO_COLLECT".equals(message.errorCode))); }