From 7cf09c614f90199756cb7afbcbb7c0ce1cd15860 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20Yaz=C4=B1c=C4=B1?= Date: Sun, 15 Sep 2024 22:14:22 +0200 Subject: [PATCH 01/10] Enable `docker` profile in CI for `log4j-layout-template-json-test` --- log4j-layout-template-json-test/pom.xml | 94 +++++++++---------- .../layout/template/json/LogstashIT.java | 23 ++++- log4j-parent/pom.xml | 10 +- 3 files changed, 75 insertions(+), 52 deletions(-) diff --git a/log4j-layout-template-json-test/pom.xml b/log4j-layout-template-json-test/pom.xml index 7031fd4d80b..d8521e6bb36 100644 --- a/log4j-layout-template-json-test/pom.xml +++ b/log4j-layout-template-json-test/pom.xml @@ -129,23 +129,6 @@ - - - org.apache.maven.plugins - maven-failsafe-plugin - - true - - - - - integration-test - verify - - - - - org.apache.maven.plugins maven-surefire-plugin @@ -170,12 +153,20 @@ docker + - false + + linux + + + env.CI + true + - 8.10.2 -Xms750m -Xmx750m @@ -202,7 +193,8 @@ ${elastic.java-opts} - 9200:9200 + + elasticsearch.port:9200 custom @@ -235,8 +227,10 @@ ${elastic.java-opts} - 12222:12222 - 12345:12345 + + logstash.gelf.port:12222 + + logstash.tcp.port:12345 [LS] @@ -248,37 +242,38 @@ --pipeline.batch.size 1 -e - input { + "logstash" + use_tcp => true + use_udp => false + port => 12222 + type => "gelf" } tcp { - port => 12345 - codec => json - type => "tcp" + port => 12345 + codec => json + type => "tcp" } } filter { if [type] == "gelf" { # These are GELF/Syslog logging levels as defined in RFC 3164. - # Map the integer level to its human readable format. + # Map the integer level to its human-readable format. translate { - field => "[level]" - destination => "[levelName]" - dictionary => { - "0" => "EMERG" - "1" => "ALERT" - "2" => "CRITICAL" - "3" => "ERROR" - "4" => "WARN" - "5" => "NOTICE" - "6" => "INFO" - "7" => "DEBUG" + field => "[level]" + destination => "[levelName]" + dictionary => { + "0" => "EMERG" + "1" => "ALERT" + "2" => "CRITICAL" + "3" => "ERROR" + "4" => "WARN" + "5" => "NOTICE" + "6" => "INFO" + "7" => "DEBUG" } } } @@ -286,12 +281,12 @@ output { # (Un)comment for debugging purposes - # stdout { codec => rubydebug } + # stdout { codec => rubydebug } elasticsearch { - hosts => ["http://elasticsearch:9200"] - index => "log4j" + hosts => ["http://elasticsearch:9200"] + index => "log4j" } - } + }]]> @@ -327,6 +322,11 @@ **/*IT.java + + ${elasticsearch.port} + ${logstash.gelf.port} + ${logstash.tcp.port} + diff --git a/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/LogstashIT.java b/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/LogstashIT.java index abb86f8ebc0..d7a553cf1e9 100644 --- a/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/LogstashIT.java +++ b/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/LogstashIT.java @@ -58,6 +58,7 @@ import org.apache.logging.log4j.layout.template.json.util.ThreadLocalRecyclerFactory; import org.apache.logging.log4j.message.SimpleMessage; import org.apache.logging.log4j.status.StatusLogger; +import org.apache.logging.log4j.util.Strings; import org.assertj.core.api.Assertions; import org.awaitility.Awaitility; import org.elasticsearch.client.RestClient; @@ -146,13 +147,29 @@ private static final class MavenHardcodedConstants { private MavenHardcodedConstants() {} - private static final int LS_GELF_INPUT_PORT = 12222; + private static final int LS_GELF_INPUT_PORT = readPort("log4j.logstash.gelf.port"); - private static final int LS_TCP_INPUT_PORT = 12345; + private static final int LS_TCP_INPUT_PORT = readPort("log4j.logstash.tcp.port"); - private static final int ES_PORT = 9200; + private static final int ES_PORT = readPort("log4j.elasticsearch.port"); private static final String ES_INDEX_NAME = "log4j"; + + private static int readPort(final String propertyName) { + final String propertyValue = System.getProperty(propertyName); + final int port; + final String errorMessage = String.format( + "was expecting a valid port number in the system property `%s`, found: `%s`", + propertyName, propertyValue); + try { + if (Strings.isBlank(propertyValue) || (port = Integer.parseInt(propertyValue)) < 0 || port >= 0xFFFF) { + throw new IllegalArgumentException(errorMessage); + } + } catch (final NumberFormatException error) { + throw new IllegalArgumentException(errorMessage, error); + } + return port; + } } @BeforeAll diff --git a/log4j-parent/pom.xml b/log4j-parent/pom.xml index 969c18523c5..91aec15400f 100644 --- a/log4j-parent/pom.xml +++ b/log4j-parent/pom.xml @@ -79,7 +79,13 @@ 1.2.15 3.4.4 - 8.15.1 + + 8.15.1 0.9.0 7.0.5 3.0.22 @@ -385,7 +391,7 @@ co.elastic.clients elasticsearch-java - ${elasticsearch-java.version} + ${elastic.version} From 9dceb8a5d4b842067f74492202f5bb2b040991c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20Yaz=C4=B1c=C4=B1?= Date: Mon, 16 Sep 2024 10:01:19 +0200 Subject: [PATCH 02/10] Fix the hostname to `localhost` to hopefully fix CI issues --- log4j-layout-template-json-test/pom.xml | 6 +++--- .../logging/log4j/layout/template/json/LogstashIT.java | 3 +-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/log4j-layout-template-json-test/pom.xml b/log4j-layout-template-json-test/pom.xml index d8521e6bb36..e1b256eb467 100644 --- a/log4j-layout-template-json-test/pom.xml +++ b/log4j-layout-template-json-test/pom.xml @@ -194,7 +194,7 @@ - elasticsearch.port:9200 + localhost:elasticsearch.port:9200 custom @@ -228,9 +228,9 @@ - logstash.gelf.port:12222 + localhost:logstash.gelf.port:12222 - logstash.tcp.port:12345 + localhost:logstash.tcp.port:12345 [LS] diff --git a/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/LogstashIT.java b/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/LogstashIT.java index d7a553cf1e9..08ad9174a18 100644 --- a/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/LogstashIT.java +++ b/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/LogstashIT.java @@ -53,7 +53,6 @@ import org.apache.logging.log4j.core.config.DefaultConfiguration; import org.apache.logging.log4j.core.impl.Log4jLogEvent; import org.apache.logging.log4j.core.layout.GelfLayout; -import org.apache.logging.log4j.core.util.NetUtils; import org.apache.logging.log4j.layout.template.json.JsonTemplateLayout.EventTemplateAdditionalField; import org.apache.logging.log4j.layout.template.json.util.ThreadLocalRecyclerFactory; import org.apache.logging.log4j.message.SimpleMessage; @@ -78,7 +77,7 @@ class LogstashIT { private static final Charset CHARSET = StandardCharsets.UTF_8; - private static final String HOST_NAME = NetUtils.getLocalHostname(); + private static final String HOST_NAME = "localhost"; private static final String SERVICE_NAME = "LogstashIT"; From 6bc992126ef76133bc2ce8756872d8fc7314ae34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20Yaz=C4=B1c=C4=B1?= Date: Mon, 16 Sep 2024 11:30:53 +0200 Subject: [PATCH 03/10] Add more diagnostics --- .../layout/template/json/LogstashIT.java | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/LogstashIT.java b/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/LogstashIT.java index 08ad9174a18..6cdafcfa5ad 100644 --- a/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/LogstashIT.java +++ b/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/LogstashIT.java @@ -439,8 +439,24 @@ private static SocketAppender createStartedAppender(final Layout layout, fina } private static boolean checkDocumentCount(int expectedCount) throws IOException { - final CountResponse countResponse = - ES_CLIENT.count(builder -> builder.index(MavenHardcodedConstants.ES_INDEX_NAME)); + final CountResponse countResponse; + try { + countResponse = ES_CLIENT.count(builder -> builder.index(MavenHardcodedConstants.ES_INDEX_NAME)); + } + // Try to enrich the failure with the available list of indices + catch (final ElasticsearchException error) { + try { + if (error.getMessage().contains("index_not_found_exception")) { + final Set indexNames = + ES_CLIENT.cluster().health().indices().keySet(); + final String message = String.format("Could not find index! Available index names: %s", indexNames); + throw new AssertionError(message, error); + } + } catch (final Exception suppressed) { + error.addSuppressed(suppressed); + } + throw error; + } final long actualCount = countResponse.count(); Assertions.assertThat(actualCount).isLessThanOrEqualTo(expectedCount); return actualCount == expectedCount; From 34b2baa0a98798bcb53af8ec4f89c169cb3b23b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20Yaz=C4=B1c=C4=B1?= Date: Mon, 16 Sep 2024 13:01:58 +0200 Subject: [PATCH 04/10] Disable JTL IT parallelism --- log4j-layout-template-json-test/pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/log4j-layout-template-json-test/pom.xml b/log4j-layout-template-json-test/pom.xml index e1b256eb467..d95343f6693 100644 --- a/log4j-layout-template-json-test/pom.xml +++ b/log4j-layout-template-json-test/pom.xml @@ -319,6 +319,7 @@ org.apache.maven.plugins maven-failsafe-plugin + none **/*IT.java From 3aa8bd5a0536c58111e52737b610e276cc7dce30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20Yaz=C4=B1c=C4=B1?= Date: Mon, 16 Sep 2024 21:20:21 +0200 Subject: [PATCH 05/10] More debugging aid --- log4j-layout-template-json-test/pom.xml | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/log4j-layout-template-json-test/pom.xml b/log4j-layout-template-json-test/pom.xml index d95343f6693..d50b242d2ce 100644 --- a/log4j-layout-template-json-test/pom.xml +++ b/log4j-layout-template-json-test/pom.xml @@ -138,9 +138,9 @@ relying on the system defaults. --> -Dfile.encoding=US-ASCII - - true - concurrent + + false + @@ -319,11 +319,13 @@ org.apache.maven.plugins maven-failsafe-plugin + false none **/*IT.java + true ${elasticsearch.port} ${logstash.gelf.port} ${logstash.tcp.port} From 6d2af2f3ae7bee6c0d389d9b61c8f8bf0afca6f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20Yaz=C4=B1c=C4=B1?= Date: Mon, 16 Sep 2024 22:09:56 +0200 Subject: [PATCH 06/10] Try hardcoding port numbers --- log4j-layout-template-json-test/pom.xml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/log4j-layout-template-json-test/pom.xml b/log4j-layout-template-json-test/pom.xml index d50b242d2ce..574afe9e1d4 100644 --- a/log4j-layout-template-json-test/pom.xml +++ b/log4j-layout-template-json-test/pom.xml @@ -167,6 +167,9 @@ + 9200 + 12222 + 12345 -Xms750m -Xmx750m From d989ab2c77729f17ec21afcb32f62b8eb068b913 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20Yaz=C4=B1c=C4=B1?= Date: Tue, 17 Sep 2024 10:10:42 +0200 Subject: [PATCH 07/10] Is `until()`, short-circuiting due to exceptions, the culprit? --- log4j-layout-template-json-test/pom.xml | 30 +++--- .../layout/template/json/LogstashIT.java | 101 +++++++++++------- 2 files changed, 76 insertions(+), 55 deletions(-) diff --git a/log4j-layout-template-json-test/pom.xml b/log4j-layout-template-json-test/pom.xml index 574afe9e1d4..14997d08948 100644 --- a/log4j-layout-template-json-test/pom.xml +++ b/log4j-layout-template-json-test/pom.xml @@ -137,11 +137,6 @@ indeed handles everything in UTF-8 without implicitly relying on the system defaults. --> -Dfile.encoding=US-ASCII - - - false - - @@ -167,9 +162,6 @@ - 9200 - 12222 - 12345 -Xms750m -Xmx750m @@ -246,7 +238,10 @@ 1 -e "logstash" use_tcp => true @@ -254,20 +249,24 @@ port => 12222 type => "gelf" } + + # Documentation: https://www.elastic.co/guide/en/logstash/current/plugins-inputs-tcp.html tcp { port => 12345 codec => json type => "tcp" } + } filter { if [type] == "gelf" { # These are GELF/Syslog logging levels as defined in RFC 3164. # Map the integer level to its human-readable format. + # Documentation: https://www.elastic.co/guide/en/logstash/current/plugins-filters-translate.html translate { - field => "[level]" - destination => "[levelName]" + source => "[level]" + target => "[levelName]" dictionary => { "0" => "EMERG" "1" => "ALERT" @@ -282,18 +281,21 @@ } } + # Documentation: https://www.elastic.co/guide/en/logstash/current/plugins-filters-elasticsearch.html output { # (Un)comment for debugging purposes - # stdout { codec => rubydebug } + stdout { codec => rubydebug } elasticsearch { hosts => ["http://elasticsearch:9200"] index => "log4j" } - }]]> + } + + ]]> - Successfully started Logstash API endpoint + Pipelines running @@ -322,8 +324,6 @@ org.apache.maven.plugins maven-failsafe-plugin - false - none **/*IT.java diff --git a/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/LogstashIT.java b/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/LogstashIT.java index 6cdafcfa5ad..15436beb721 100644 --- a/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/LogstashIT.java +++ b/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/LogstashIT.java @@ -16,6 +16,9 @@ */ package org.apache.logging.log4j.layout.template.json; +import static org.assertj.core.api.Assertions.assertThat; +import static org.awaitility.Awaitility.await; + import co.elastic.clients.elasticsearch.ElasticsearchClient; import co.elastic.clients.elasticsearch._types.ElasticsearchException; import co.elastic.clients.elasticsearch._types.HealthStatus; @@ -32,6 +35,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; +import java.net.Socket; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.time.Duration; @@ -41,6 +45,7 @@ import java.util.Map; import java.util.Objects; import java.util.Set; +import java.util.concurrent.TimeUnit; import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -58,8 +63,6 @@ import org.apache.logging.log4j.message.SimpleMessage; import org.apache.logging.log4j.status.StatusLogger; import org.apache.logging.log4j.util.Strings; -import org.assertj.core.api.Assertions; -import org.awaitility.Awaitility; import org.elasticsearch.client.RestClient; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; @@ -71,14 +74,14 @@ @Execution(ExecutionMode.SAME_THREAD) class LogstashIT { + private static final String LOG_PREFIX = LogstashIT.class.getSimpleName() + ' '; + private static final StatusLogger LOGGER = StatusLogger.getLogger(); private static final DefaultConfiguration CONFIGURATION = new DefaultConfiguration(); private static final Charset CHARSET = StandardCharsets.UTF_8; - private static final String HOST_NAME = "localhost"; - private static final String SERVICE_NAME = "LogstashIT"; private static final String EVENT_DATASET = SERVICE_NAME + ".log"; @@ -88,7 +91,7 @@ class LogstashIT { .setCharset(CHARSET) .setCompressionType(GelfLayout.CompressionType.OFF) .setIncludeNullDelimiter(true) - .setHost(HOST_NAME) + .setHost(MavenHardcodedConstants.HOST_NAME) .build(); private static final JsonTemplateLayout JSON_TEMPLATE_GELF_LAYOUT = JsonTemplateLayout.newBuilder() @@ -99,13 +102,12 @@ class LogstashIT { .setEventTemplateAdditionalFields(new EventTemplateAdditionalField[] { EventTemplateAdditionalField.newBuilder() .setKey("host") - .setValue(HOST_NAME) + .setValue(MavenHardcodedConstants.HOST_NAME) .build() }) .build(); - // Note that EcsLayout doesn't support charset configuration, though it uses - // UTF-8 internally. + // Note that `EcsLayout` doesn't support charset configuration, though it uses UTF-8 internally. private static final EcsLayout ECS_LAYOUT = EcsLayout.newBuilder() .setConfiguration(CONFIGURATION) .setServiceName(SERVICE_NAME) @@ -140,12 +142,14 @@ class LogstashIT { private static ElasticsearchClient ES_CLIENT; /** - * Constants hardcoded in docker-maven-plugin configuration, do not change! + * Constants hardcoded in `docker-maven-plugin` configuration, do not change! */ private static final class MavenHardcodedConstants { private MavenHardcodedConstants() {} + private static final String HOST_NAME = "localhost"; + private static final int LS_GELF_INPUT_PORT = readPort("log4j.logstash.gelf.port"); private static final int LS_TCP_INPUT_PORT = readPort("log4j.logstash.tcp.port"); @@ -172,28 +176,46 @@ private static int readPort(final String propertyName) { } @BeforeAll - public static void initClient() throws IOException { + public static void initEsClient() throws IOException { - LOGGER.info("instantiating the ES client"); - REST_CLIENT = RestClient.builder( - HttpHost.create(String.format("http://%s:%d", HOST_NAME, MavenHardcodedConstants.ES_PORT))) - .build(); + LOGGER.info(LOG_PREFIX + "instantiating the ES client"); + final String hostUri = + String.format("http://%s:%d", MavenHardcodedConstants.HOST_NAME, MavenHardcodedConstants.ES_PORT); + REST_CLIENT = RestClient.builder(HttpHost.create(hostUri)).build(); ES_TRANSPORT = new RestClientTransport(REST_CLIENT, new JacksonJsonpMapper()); ES_CLIENT = new ElasticsearchClient(ES_TRANSPORT); - LOGGER.info("verifying the ES connection"); + LOGGER.info(LOG_PREFIX + "verifying the ES connection to `{}`", hostUri); HealthResponse healthResponse = ES_CLIENT.cluster().health(); - Assertions.assertThat(healthResponse.status()).isNotEqualTo(HealthStatus.Red); + assertThat(healthResponse.status()).isNotEqualTo(HealthStatus.Red); + } + + @BeforeAll + public static void waitForLsInputSockets() { + waitForSocketBinding(MavenHardcodedConstants.LS_GELF_INPUT_PORT, "Logstash GELF input"); + waitForSocketBinding(MavenHardcodedConstants.LS_TCP_INPUT_PORT, "Logstash TCP input"); + } + + private static void waitForSocketBinding(final int port, final String name) { + LOGGER.info(LOG_PREFIX + "verifying socket binding at port {} for {}", port, name); + await("socket binding at port " + port) + .pollDelay(100, TimeUnit.MILLISECONDS) + .atMost(1, TimeUnit.MINUTES) + .untilAsserted(() -> { + try (final Socket socket = new Socket(MavenHardcodedConstants.HOST_NAME, port)) { + assertThat(socket.isConnected()).isTrue(); + } + }); } @BeforeEach void deleteIndex() throws IOException { - LOGGER.info("deleting the ES index"); + LOGGER.info(LOG_PREFIX + "deleting the ES index"); try { DeleteIndexResponse deleteIndexResponse = ES_CLIENT .indices() .delete(DeleteIndexRequest.of(builder -> builder.index(MavenHardcodedConstants.ES_INDEX_NAME))); - Assertions.assertThat(deleteIndexResponse.acknowledged()).isTrue(); + assertThat(deleteIndexResponse.acknowledged()).isTrue(); } catch (ElasticsearchException error) { if (!error.getMessage().contains("index_not_found_exception")) { throw new RuntimeException(error); @@ -225,15 +247,15 @@ private static void testEvents(final List logEvents) throws IOExceptio try { // Append events. - LOGGER.info("appending events"); + LOGGER.info(LOG_PREFIX + "appending events"); logEvents.forEach(appender::append); - LOGGER.info("completed appending events"); + LOGGER.info(LOG_PREFIX + "completed appending events"); // Wait all messages to arrive. - Awaitility.await() + await("message delivery") .atMost(Duration.ofSeconds(60)) .pollDelay(Duration.ofSeconds(2)) - .until(() -> checkDocumentCount(LOG_EVENT_COUNT)); + .untilAsserted(() -> assertDocumentCount(LOG_EVENT_COUNT)); // Verify indexed messages. final Set expectedMessages = logEvents.stream() @@ -243,7 +265,7 @@ private static void testEvents(final List logEvents) throws IOExceptio .map(source -> (String) source.get(ES_INDEX_MESSAGE_FIELD_NAME)) .filter(Objects::nonNull) .collect(Collectors.toSet()); - Assertions.assertThat(actualMessages).isEqualTo(expectedMessages); + assertThat(actualMessages).isEqualTo(expectedMessages); } finally { appender.stop(); @@ -296,16 +318,16 @@ void test_newlines() throws IOException { try { // Append the event. - LOGGER.info("appending events"); + LOGGER.info(LOG_PREFIX + "appending events"); appender.append(logEvent1); appender.append(logEvent2); - LOGGER.info("completed appending events"); + LOGGER.info(LOG_PREFIX + "completed appending events"); // Wait the message to arrive. - Awaitility.await() + await("message delivery") .atMost(Duration.ofSeconds(60)) .pollDelay(Duration.ofSeconds(2)) - .until(() -> checkDocumentCount(2)); + .untilAsserted(() -> assertDocumentCount(2)); // Verify indexed messages. final Set expectedMessages = Stream.of(logEvent1, logEvent2) @@ -315,7 +337,7 @@ void test_newlines() throws IOException { .map(source -> (String) source.get(ES_INDEX_MESSAGE_FIELD_NAME)) .filter(Objects::nonNull) .collect(Collectors.toSet()); - Assertions.assertThat(actualMessages).isEqualTo(expectedMessages); + assertThat(actualMessages).isEqualTo(expectedMessages); } finally { appender.stop(); @@ -352,7 +374,7 @@ void test_GelfLayout() throws IOException { Collections.emptySet()); // Compare persisted sources. - Assertions.assertThat(actualSourceByKey).isEqualTo(expectedSourceByKey); + assertThat(actualSourceByKey).isEqualTo(expectedSourceByKey); } @Test @@ -386,7 +408,7 @@ void test_EcsLayout() throws IOException { excludedKeys); // Compare persisted sources. - Assertions.assertThat(actualSourceByKey).isEqualTo(expectedSourceByKey); + assertThat(actualSourceByKey).isEqualTo(expectedSourceByKey); } private static Map appendAndCollect( @@ -400,15 +422,15 @@ private static Map appendAndCollect( try { // Append the event. - LOGGER.info("appending events"); + LOGGER.info(LOG_PREFIX + "appending events"); logEvents.forEach(appender::append); - LOGGER.info("completed appending events"); + LOGGER.info(LOG_PREFIX + "completed appending events"); // Wait the message to arrive. - Awaitility.await() + await("message delivery") .atMost(Duration.ofSeconds(60)) .pollDelay(Duration.ofSeconds(2)) - .until(() -> checkDocumentCount(LOG_EVENT_COUNT)); + .untilAsserted(() -> assertDocumentCount(LOG_EVENT_COUNT)); // Retrieve the persisted messages. return queryDocuments().stream().collect(Collectors.toMap(keyMapper, (final Map source) -> { @@ -422,10 +444,10 @@ private static Map appendAndCollect( } private static SocketAppender createStartedAppender(final Layout layout, final int port) { - LOGGER.info("creating the appender"); + LOGGER.info(LOG_PREFIX + "creating the appender"); final SocketAppender appender = SocketAppender.newBuilder() .setConfiguration(CONFIGURATION) - .setHost(HOST_NAME) + .setHost(MavenHardcodedConstants.HOST_NAME) .setPort(port) .setReconnectDelayMillis(100) .setName("LogstashItAppender") @@ -438,7 +460,7 @@ private static SocketAppender createStartedAppender(final Layout layout, fina return appender; } - private static boolean checkDocumentCount(int expectedCount) throws IOException { + private static void assertDocumentCount(final int expectedCount) throws IOException { final CountResponse countResponse; try { countResponse = ES_CLIENT.count(builder -> builder.index(MavenHardcodedConstants.ES_INDEX_NAME)); @@ -458,8 +480,7 @@ private static boolean checkDocumentCount(int expectedCount) throws IOException throw error; } final long actualCount = countResponse.count(); - Assertions.assertThat(actualCount).isLessThanOrEqualTo(expectedCount); - return actualCount == expectedCount; + assertThat(actualCount).isEqualTo(expectedCount); } private static List> queryDocuments() throws IOException { @@ -473,7 +494,7 @@ private static List> queryDocuments() throws IOException { return searchResponse.hits().hits().stream() .map(hit -> { @SuppressWarnings("unchecked") - Map source = hit.source(); + final Map source = hit.source(); return source; }) .collect(Collectors.toList()); From 0d3883c429eb89f81233deb45acd23917ba23246 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20Yaz=C4=B1c=C4=B1?= Date: Tue, 17 Sep 2024 11:30:15 +0200 Subject: [PATCH 08/10] Improve container initialization checks and suppress output --- log4j-layout-template-json-test/pom.xml | 35 ++++++++++++++----- .../layout/template/json/LogstashIT.java | 9 +++-- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/log4j-layout-template-json-test/pom.xml b/log4j-layout-template-json-test/pom.xml index 14997d08948..eadf7c9ce9a 100644 --- a/log4j-layout-template-json-test/pom.xml +++ b/log4j-layout-template-json-test/pom.xml @@ -137,6 +137,11 @@ indeed handles everything in UTF-8 without implicitly relying on the system defaults. --> -Dfile.encoding=US-ASCII + + + true + concurrent + @@ -162,9 +167,15 @@ + + + false + false + - -Xms750m -Xmx750m + -Xms750m -Xmx750m + @@ -174,7 +185,6 @@ io.fabric8 docker-maven-plugin - all true true @@ -185,7 +195,7 @@ single-node false - ${elastic.java-opts} + ${elastic.javaOpts} @@ -201,7 +211,11 @@ cyan - recovered \[0\] indices into cluster_state + + + 9200 + + @@ -219,7 +233,7 @@ logstash - ${elastic.java-opts} + ${elastic.javaOpts} @@ -284,7 +298,7 @@ # Documentation: https://www.elastic.co/guide/en/logstash/current/plugins-filters-elasticsearch.html output { # (Un)comment for debugging purposes - stdout { codec => rubydebug } + # stdout { codec => rubydebug } elasticsearch { hosts => ["http://elasticsearch:9200"] index => "log4j" @@ -295,7 +309,13 @@ - Pipelines running + + localhost + + 12222 + 12345 + + @@ -328,7 +348,6 @@ **/*IT.java - true ${elasticsearch.port} ${logstash.gelf.port} ${logstash.tcp.port} diff --git a/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/LogstashIT.java b/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/LogstashIT.java index 15436beb721..0de69443d0d 100644 --- a/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/LogstashIT.java +++ b/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/LogstashIT.java @@ -186,8 +186,13 @@ public static void initEsClient() throws IOException { ES_CLIENT = new ElasticsearchClient(ES_TRANSPORT); LOGGER.info(LOG_PREFIX + "verifying the ES connection to `{}`", hostUri); - HealthResponse healthResponse = ES_CLIENT.cluster().health(); - assertThat(healthResponse.status()).isNotEqualTo(HealthStatus.Red); + await("ES cluster health") + .pollDelay(100, TimeUnit.MILLISECONDS) + .atMost(1, TimeUnit.MINUTES) + .untilAsserted(() -> { + final HealthResponse healthResponse = ES_CLIENT.cluster().health(); + assertThat(healthResponse.status()).isNotEqualTo(HealthStatus.Red); + }); } @BeforeAll From 7573f9ec65db7f534b7704ac90ab983ab2343c63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20Yaz=C4=B1c=C4=B1?= Date: Tue, 17 Sep 2024 11:35:08 +0200 Subject: [PATCH 09/10] Remove redundant exception --- .../apache/logging/log4j/layout/template/json/LogstashIT.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/LogstashIT.java b/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/LogstashIT.java index 0de69443d0d..17b83629755 100644 --- a/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/LogstashIT.java +++ b/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/LogstashIT.java @@ -176,7 +176,7 @@ private static int readPort(final String propertyName) { } @BeforeAll - public static void initEsClient() throws IOException { + public static void initEsClient() { LOGGER.info(LOG_PREFIX + "instantiating the ES client"); final String hostUri = From b2e173800843e47ee25afa86c926fdd488526cbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20Yaz=C4=B1c=C4=B1?= Date: Tue, 17 Sep 2024 12:09:39 +0200 Subject: [PATCH 10/10] Rearrange dependencies --- log4j-layout-template-json-test/pom.xml | 9 +++++++++ log4j-parent/pom.xml | 13 ------------- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/log4j-layout-template-json-test/pom.xml b/log4j-layout-template-json-test/pom.xml index eadf7c9ce9a..51bc8a9660b 100644 --- a/log4j-layout-template-json-test/pom.xml +++ b/log4j-layout-template-json-test/pom.xml @@ -43,6 +43,14 @@ org.apache.logging.log4j.layout.template.json.test org.apache.logging.log4j.core + + 8.15.1 + @@ -85,6 +93,7 @@ co.elastic.clients elasticsearch-java + ${elastic.version} test diff --git a/log4j-parent/pom.xml b/log4j-parent/pom.xml index 9bc63977882..304231465a5 100644 --- a/log4j-parent/pom.xml +++ b/log4j-parent/pom.xml @@ -79,13 +79,6 @@ 1.2.15 3.4.4 - - 8.15.1 0.9.0 7.0.5 3.0.22 @@ -388,12 +381,6 @@ ${disruptor.version} - - co.elastic.clients - elasticsearch-java - ${elastic.version} - - org.zapodot embedded-ldap-junit