From 100eaeaf340c1dd75467a913779b209709961328 Mon Sep 17 00:00:00 2001 From: Aaron Abbott Date: Fri, 19 Apr 2024 22:28:51 +0000 Subject: [PATCH 1/3] Add trace context fields to GcpLayout.json --- .../layout/template/json/GcpLayoutTest.java | 26 +++++++++++++++++++ .../src/main/resources/GcpLayout.json | 9 +++++++ 2 files changed, 35 insertions(+) diff --git a/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/GcpLayoutTest.java b/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/GcpLayoutTest.java index a61a764cd13..c257b1c8af8 100644 --- a/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/GcpLayoutTest.java +++ b/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/GcpLayoutTest.java @@ -28,6 +28,9 @@ import java.util.Locale; import org.apache.logging.log4j.Level; import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.impl.ContextDataFactory; +import org.apache.logging.log4j.core.impl.Log4jLogEvent; +import org.apache.logging.log4j.util.StringMap; import org.junit.jupiter.api.Test; class GcpLayoutTest { @@ -54,6 +57,29 @@ void test_full_log_events() { LogEventFixture.createFullLogEvents(LOG_EVENT_COUNT).forEach(GcpLayoutTest::verifySerialization); } + @Test + void test_trace_context() { + final StringMap contextData = ContextDataFactory.createContextData(); + contextData.putValue("trace_id", "4bf92f3577b34da6a3ce929d0e0e4736"); + contextData.putValue("span_id", "00f067aa0ba902b7"); + + LogEvent logEvent = + Log4jLogEvent.newBuilder().setContextData(contextData).build(); + + usingSerializedLogEventAccessor(LAYOUT, logEvent, accessor -> { + // Verify trace id + assertThat(accessor.getString("logging.googleapis.com/trace")) + .isEqualTo("4bf92f3577b34da6a3ce929d0e0e4736"); + + // Verify span ID + assertThat(accessor.getString("logging.googleapis.com/spanId")).isEqualTo("00f067aa0ba902b7"); + + // Verify trace sampled + assertThat(accessor.getBoolean("logging.googleapis.com/trace_sampled")) + .isTrue(); + }); + } + private static void verifySerialization(final LogEvent logEvent) { usingSerializedLogEventAccessor(LAYOUT, logEvent, accessor -> { diff --git a/log4j-layout-template-json/src/main/resources/GcpLayout.json b/log4j-layout-template-json/src/main/resources/GcpLayout.json index 563e5a2fc3c..f00c84d981e 100644 --- a/log4j-layout-template-json/src/main/resources/GcpLayout.json +++ b/log4j-layout-template-json/src/main/resources/GcpLayout.json @@ -40,6 +40,15 @@ "$resolver": "counter", "stringified": true }, + "logging.googleapis.com/trace": { + "$resolver": "mdc", + "key": "trace_id" + }, + "logging.googleapis.com/spanId": { + "$resolver": "mdc", + "key": "span_id" + }, + "logging.googleapis.com/trace_sampled": true, "_exception": { "class": { "$resolver": "exception", From e4a761f0ef5be954e553cf8b5517936d0f93c542 Mon Sep 17 00:00:00 2001 From: Aaron Abbott Date: Mon, 22 Apr 2024 20:25:03 +0000 Subject: [PATCH 2/3] Add changelog xml --- src/changelog/.2.x.x/add_GcpLayout_tracing_support.xml | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 src/changelog/.2.x.x/add_GcpLayout_tracing_support.xml diff --git a/src/changelog/.2.x.x/add_GcpLayout_tracing_support.xml b/src/changelog/.2.x.x/add_GcpLayout_tracing_support.xml new file mode 100644 index 00000000000..31e13453f05 --- /dev/null +++ b/src/changelog/.2.x.x/add_GcpLayout_tracing_support.xml @@ -0,0 +1,8 @@ + + + + Add trace context fields to GcpLayout.json + From ac714e229509b2fad4beb5fb67a1e6080f672216 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20Yaz=C4=B1c=C4=B1?= Date: Tue, 23 Apr 2024 09:48:39 +0200 Subject: [PATCH 3/3] Minor changelog fix --- src/changelog/.2.x.x/add_GcpLayout_tracing_support.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/changelog/.2.x.x/add_GcpLayout_tracing_support.xml b/src/changelog/.2.x.x/add_GcpLayout_tracing_support.xml index 31e13453f05..f37e80fdbcd 100644 --- a/src/changelog/.2.x.x/add_GcpLayout_tracing_support.xml +++ b/src/changelog/.2.x.x/add_GcpLayout_tracing_support.xml @@ -2,7 +2,7 @@ + type="added"> - Add trace context fields to GcpLayout.json + Add trace context fields to `GcpLayout.json`