From badc9014f5025e28465d0314f456e748d34289a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Konrad?= Date: Wed, 27 Dec 2023 13:48:22 +0100 Subject: [PATCH] logging-gelf: Adding missing GELF MDC Config --- .../io/quarkus/logging/gelf/GelfConfig.java | 21 +++++++++++++++++++ .../logging/gelf/GelfLogHandlerRecorder.java | 3 +++ .../gelf/it/GelfLogHandlerResource.java | 3 +++ .../src/main/resources/application.properties | 4 +++- .../logging/gelf/it/GelfLogHandlerTest.java | 2 ++ 5 files changed, 32 insertions(+), 1 deletion(-) diff --git a/extensions/logging-gelf/runtime/src/main/java/io/quarkus/logging/gelf/GelfConfig.java b/extensions/logging-gelf/runtime/src/main/java/io/quarkus/logging/gelf/GelfConfig.java index e67d288e8d14b..0d604ae5c3b9f 100644 --- a/extensions/logging-gelf/runtime/src/main/java/io/quarkus/logging/gelf/GelfConfig.java +++ b/extensions/logging-gelf/runtime/src/main/java/io/quarkus/logging/gelf/GelfConfig.java @@ -100,6 +100,27 @@ public class GelfConfig { @ConfigItem public boolean includeFullMdc; + /** + * Send additional fields whose values are obtained from MDC. Name of the Fields are comma-separated. Example: + * mdcFields=Application,Version,SomeOtherFieldName + */ + @ConfigItem() + public Optional mdcFields; + + /** + * Dynamic MDC Fields allows you to extract MDC values based on one or more regular expressions. Multiple regexes are + * comma-separated. The name of the MDC entry is used as GELF field name. + */ + @ConfigItem + public Optional dynamicMdcFields; + + /** + * Pattern-based type specification for additional and MDC fields. Key-value pairs are comma-separated. Example: + * my_field.*=String,business\..*\.field=double + */ + @ConfigItem + public Optional dynamicMdcFieldTypes; + /** * Maximum message size (in bytes). * If the message size is exceeded, the appender will submit the message in multiple chunks. diff --git a/extensions/logging-gelf/runtime/src/main/java/io/quarkus/logging/gelf/GelfLogHandlerRecorder.java b/extensions/logging-gelf/runtime/src/main/java/io/quarkus/logging/gelf/GelfLogHandlerRecorder.java index 6000d36215459..95d4a9b03e32d 100644 --- a/extensions/logging-gelf/runtime/src/main/java/io/quarkus/logging/gelf/GelfLogHandlerRecorder.java +++ b/extensions/logging-gelf/runtime/src/main/java/io/quarkus/logging/gelf/GelfLogHandlerRecorder.java @@ -39,6 +39,9 @@ public RuntimeValue> initializeHandler(final GelfConfig config handler.setFilterStackTrace(config.filterStackTrace); handler.setTimestampPattern(config.timestampPattern); handler.setIncludeFullMdc(config.includeFullMdc); + handler.setDynamicMdcFields(config.dynamicMdcFields.orElse(null)); + handler.setMdcFields(config.mdcFields.orElse(null)); + handler.setDynamicMdcFieldTypes(config.dynamicMdcFieldTypes.orElse(null)); handler.setHost(config.host); handler.setPort(config.port); handler.setLevel(config.level); diff --git a/integration-tests/logging-gelf/src/main/java/io/quarkus/logging/gelf/it/GelfLogHandlerResource.java b/integration-tests/logging-gelf/src/main/java/io/quarkus/logging/gelf/it/GelfLogHandlerResource.java index 6de56f46342f9..e634b3cd11664 100644 --- a/integration-tests/logging-gelf/src/main/java/io/quarkus/logging/gelf/it/GelfLogHandlerResource.java +++ b/integration-tests/logging-gelf/src/main/java/io/quarkus/logging/gelf/it/GelfLogHandlerResource.java @@ -21,6 +21,7 @@ import jakarta.ws.rs.Path; import org.jboss.logging.Logger; +import org.jboss.logging.MDC; /** * This endpoint allow to test central logging solution by generating a log event when @@ -33,6 +34,8 @@ public class GelfLogHandlerResource { @GET public void log() { + MDC.put("field3", 99); + MDC.put("field4", 98); LOG.info("Some useful log message"); } diff --git a/integration-tests/logging-gelf/src/main/resources/application.properties b/integration-tests/logging-gelf/src/main/resources/application.properties index 59759a7e82150..b186f323a47fb 100644 --- a/integration-tests/logging-gelf/src/main/resources/application.properties +++ b/integration-tests/logging-gelf/src/main/resources/application.properties @@ -6,4 +6,6 @@ quarkus.log.handler.gelf.facility=custom quarkus.log.handler.gelf.additional-field.field1.value=value quarkus.log.handler.gelf.additional-field.field1.type=String quarkus.log.handler.gelf.additional-field.field2.value=666 -quarkus.log.handler.gelf.additional-field.field2.type=long \ No newline at end of file +quarkus.log.handler.gelf.additional-field.field2.type=long +quarkus.log.handler.gelf.include-full-mdc=true +quarkus.log.handler.gelf.dynamic-mdc-field-types=field3=String \ No newline at end of file diff --git a/integration-tests/logging-gelf/src/test/java/io/quarkus/logging/gelf/it/GelfLogHandlerTest.java b/integration-tests/logging-gelf/src/test/java/io/quarkus/logging/gelf/it/GelfLogHandlerTest.java index efe13272e0fc8..350d63c6911b5 100644 --- a/integration-tests/logging-gelf/src/test/java/io/quarkus/logging/gelf/it/GelfLogHandlerTest.java +++ b/integration-tests/logging-gelf/src/test/java/io/quarkus/logging/gelf/it/GelfLogHandlerTest.java @@ -42,6 +42,8 @@ public void test() { assertEquals(200, response.statusCode()); assertNotNull(response.body().path("hits.hits[0]._source")); assertEquals("Some useful log message", response.body().path("hits.hits[0]._source.message")); + assertEquals(Integer.valueOf(98), response.body().path("hits.hits[0]._source.field4")); + assertEquals("99", response.body().path("hits.hits[0]._source.field3")); }); } }