Skip to content

Commit e8a5053

Browse files
aabmassvy
andauthored
Add trace context fields to GcpLayout.json (#2498)
Co-authored-by: Volkan Yazıcı <[email protected]>
1 parent 03aa740 commit e8a5053

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-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
@@ -28,6 +28,9 @@
2828
import java.util.Locale;
2929
import org.apache.logging.log4j.Level;
3030
import org.apache.logging.log4j.core.LogEvent;
31+
import org.apache.logging.log4j.core.impl.ContextDataFactory;
32+
import org.apache.logging.log4j.core.impl.Log4jLogEvent;
33+
import org.apache.logging.log4j.util.StringMap;
3134
import org.junit.jupiter.api.Test;
3235

3336
class GcpLayoutTest {
@@ -54,6 +57,29 @@ void test_full_log_events() {
5457
LogEventFixture.createFullLogEvents(LOG_EVENT_COUNT).forEach(GcpLayoutTest::verifySerialization);
5558
}
5659

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

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",
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="https://logging.apache.org/xml/ns"
4+
xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
5+
type="added">
6+
<issue id="2498" link="https://github.com/apache/logging-log4j2/pull/2498"/>
7+
<description format="asciidoc">Add trace context fields to `GcpLayout.json`</description>
8+
</entry>

0 commit comments

Comments
 (0)