This is an automated email from the ASF dual-hosted git repository.
vy pushed a commit to branch 2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/2.x by this push:
new e8a5053c91 Add trace context fields to `GcpLayout.json` (#2498)
e8a5053c91 is described below
commit e8a5053c91011781630d6f6584bd7602116b4d37
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.x.x/add_GcpLayout_tracing_support.xml | 8 +++++++
3 files changed, 43 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 a61a764cd1..c257b1c8af 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.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 {
@@ -54,6 +57,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",
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 0000000000..f37e80fdbc
--- /dev/null
+++ b/src/changelog/.2.x.x/add_GcpLayout_tracing_support.xml
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns="https://logging.apache.org/xml/ns"
+ xsi:schemaLocation="https://logging.apache.org/xml/ns
https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
+ type="added">
+ <issue id="2498" link="https://github.com/apache/logging-log4j2/pull/2498"/>
+ <description format="asciidoc">Add trace context fields to
`GcpLayout.json`</description>
+</entry>