This is an automated email from the ASF dual-hosted git repository.

vy pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git


The following commit(s) were added to refs/heads/main by this push:
     new 0ddf261201 Add trace context fields to `GcpLayout.json` (#2498)
0ddf261201 is described below

commit 0ddf261201dc5b79fd69634dd0e36bb022f67b94
Author: Aaron Abbott <[email protected]>
AuthorDate: Tue Apr 23 05:11:27 2024 -0400

    Add trace context fields to `GcpLayout.json` (#2498)
    
    Co-authored-by: Volkan Yazıcı <[email protected]>
---
 .../log4j/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 d72d015a28..a617d3a2e2 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
@@ -27,6 +27,9 @@ import java.time.format.DateTimeFormatter;
 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 {
@@ -53,6 +56,29 @@ class GcpLayoutTest {
         
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 563e5a2fc3..f00c84d981 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",

Reply via email to