Skip to content

Commit

Permalink
Add trace context fields to GcpLayout.json (apache#2498)
Browse files Browse the repository at this point in the history
Co-authored-by: Volkan Yazıcı <volkan@yazi.ci>
  • Loading branch information
aabmass and vy authored Apr 23, 2024
1 parent 03aa740 commit e8a5053
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 -> {

Expand Down
9 changes: 9 additions & 0 deletions log4j-layout-template-json/src/main/resources/GcpLayout.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 8 additions & 0 deletions src/changelog/.2.x.x/add_GcpLayout_tracing_support.xml
Original file line number Diff line number Diff line change
@@ -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>

0 comments on commit e8a5053

Please sign in to comment.