Skip to content

Commit 0ddf261

Browse files
aabmassvy
andcommitted
Add trace context fields to GcpLayout.json (#2498)
Co-authored-by: Volkan Yazıcı <volkan@yazi.ci>
1 parent abf4638 commit 0ddf261

File tree

2 files changed

+35
-0
lines changed
  • log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json
  • log4j-layout-template-json/src/main/resources

2 files changed

+35
-0
lines changed

log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/GcpLayoutTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
import java.util.Locale;
2828
import org.apache.logging.log4j.Level;
2929
import org.apache.logging.log4j.core.LogEvent;
30+
import org.apache.logging.log4j.core.impl.ContextDataFactory;
31+
import org.apache.logging.log4j.core.impl.Log4jLogEvent;
32+
import org.apache.logging.log4j.util.StringMap;
3033
import org.junit.jupiter.api.Test;
3134

3235
class GcpLayoutTest {
@@ -53,6 +56,29 @@ void test_full_log_events() {
5356
LogEventFixture.createFullLogEvents(LOG_EVENT_COUNT).forEach(GcpLayoutTest::verifySerialization);
5457
}
5558

59+
@Test
60+
void test_trace_context() {
61+
final StringMap contextData = ContextDataFactory.createContextData();
62+
contextData.putValue("trace_id", "4bf92f3577b34da6a3ce929d0e0e4736");
63+
contextData.putValue("span_id", "00f067aa0ba902b7");
64+
65+
LogEvent logEvent =
66+
Log4jLogEvent.newBuilder().setContextData(contextData).build();
67+
68+
usingSerializedLogEventAccessor(LAYOUT, logEvent, accessor -> {
69+
// Verify trace id
70+
assertThat(accessor.getString("logging.googleapis.com/trace"))
71+
.isEqualTo("4bf92f3577b34da6a3ce929d0e0e4736");
72+
73+
// Verify span ID
74+
assertThat(accessor.getString("logging.googleapis.com/spanId")).isEqualTo("00f067aa0ba902b7");
75+
76+
// Verify trace sampled
77+
assertThat(accessor.getBoolean("logging.googleapis.com/trace_sampled"))
78+
.isTrue();
79+
});
80+
}
81+
5682
private static void verifySerialization(final LogEvent logEvent) {
5783
usingSerializedLogEventAccessor(LAYOUT, logEvent, accessor -> {
5884

log4j-layout-template-json/src/main/resources/GcpLayout.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@
4040
"$resolver": "counter",
4141
"stringified": true
4242
},
43+
"logging.googleapis.com/trace": {
44+
"$resolver": "mdc",
45+
"key": "trace_id"
46+
},
47+
"logging.googleapis.com/spanId": {
48+
"$resolver": "mdc",
49+
"key": "span_id"
50+
},
51+
"logging.googleapis.com/trace_sampled": true,
4352
"_exception": {
4453
"class": {
4554
"$resolver": "exception",

0 commit comments

Comments
 (0)