2020
2121import co .elastic .apm .agent .AbstractInstrumentationTest ;
2222import co .elastic .apm .agent .configuration .CoreConfiguration ;
23+ import co .elastic .apm .agent .impl .transaction .Transaction ;
2324import co .elastic .apm .agent .logging .LogEcsReformatting ;
2425import co .elastic .apm .agent .logging .LoggingConfiguration ;
2526import co .elastic .apm .agent .logging .TestUtils ;
2930import org .junit .jupiter .api .BeforeEach ;
3031import org .junit .jupiter .api .Test ;
3132
32- import javax .annotation .Nonnull ;
33- import javax .annotation .Nullable ;
3433import java .io .IOException ;
3534import java .nio .file .Files ;
3635import java .nio .file .Path ;
4241import java .util .Map ;
4342import java .util .Objects ;
4443import java .util .TimeZone ;
45- import java .util .UUID ;
4644import java .util .concurrent .TimeUnit ;
4745import java .util .stream .Collectors ;
4846import java .util .stream .Stream ;
4947
48+ import static co .elastic .apm .agent .log .shader .AbstractLogCorrelationHelper .TRACE_ID_MDC_KEY ;
49+ import static co .elastic .apm .agent .log .shader .AbstractLogCorrelationHelper .TRANSACTION_ID_MDC_KEY ;
5050import static org .assertj .core .api .Assertions .assertThat ;
5151import static org .mockito .Mockito .doReturn ;
5252import static org .mockito .Mockito .when ;
@@ -65,6 +65,7 @@ public abstract class LogShadingInstrumentationTest extends AbstractInstrumentat
6565 private final ObjectMapper objectMapper ;
6666 private LoggingConfiguration loggingConfig ;
6767 private String serviceName ;
68+ private Transaction transaction ;
6869
6970 public LogShadingInstrumentationTest () {
7071 logger = createLoggerFacade ();
@@ -82,6 +83,8 @@ public void setup() throws Exception {
8283
8384 // IMPORTANT: keep this last, so that it doesn't interfere with Mockito settings above
8485 serviceName = Objects .requireNonNull (tracer .getMetaDataFuture ().get (2000 , TimeUnit .MILLISECONDS ).getService ().getName ());
86+
87+ transaction = Objects .requireNonNull (tracer .startRootTransaction (null )).activate ();
8588 }
8689
8790 private void setEcsReformattingConfig (LogEcsReformatting ecsReformattingConfig ) {
@@ -96,6 +99,7 @@ private void initializeShadeDir(String dirName) throws IOException {
9699
97100 @ AfterEach
98101 public void closeLogger () {
102+ transaction .deactivate ().end ();
99103 logger .close ();
100104 }
101105
@@ -109,16 +113,10 @@ public void testSimpleLogShading() throws Exception {
109113 }
110114
111115 private void runSimpleScenario () throws Exception {
112- String traceId = UUID .randomUUID ().toString ();
113- logger .putTraceIdToMdc (traceId );
114- try {
115- logger .trace (TRACE_MESSAGE );
116- logger .debug (DEBUG_MESSAGE );
117- logger .warn (WARN_MESSAGE );
118- logger .error (ERROR_MESSAGE );
119- } finally {
120- logger .removeTraceIdFromMdc ();
121- }
116+ logger .trace (TRACE_MESSAGE );
117+ logger .debug (DEBUG_MESSAGE );
118+ logger .warn (WARN_MESSAGE );
119+ logger .error (ERROR_MESSAGE );
122120
123121 ArrayList <String []> rawLogLines = readRawLogLines ();
124122 assertThat (rawLogLines ).hasSize (4 );
@@ -127,7 +125,7 @@ private void runSimpleScenario() throws Exception {
127125 assertThat (ecsLogLines ).hasSize (4 );
128126
129127 for (int i = 0 ; i < 4 ; i ++) {
130- verifyEcsFormat (rawLogLines .get (i ), ecsLogLines .get (i ), traceId );
128+ verifyEcsFormat (rawLogLines .get (i ), ecsLogLines .get (i ));
131129 }
132130 }
133131
@@ -146,7 +144,7 @@ public void testMarkers() throws Exception {
146144 assertThat (ecsLogLines ).hasSize (1 );
147145 JsonNode ecsLogLine = ecsLogLines .get (0 );
148146
149- verifyEcsFormat (rawLogLine , ecsLogLine , null );
147+ verifyEcsFormat (rawLogLine , ecsLogLine );
150148
151149 JsonNode tagsJson = ecsLogLine .get ("tags" );
152150 assertThat (tagsJson .isArray ()).isTrue ();
@@ -181,8 +179,8 @@ public void testLazyShadeFileCreation() throws Exception {
181179
182180 ArrayList <JsonNode > ecsLogLines = readShadeLogFile ();
183181 assertThat (ecsLogLines ).hasSize (2 );
184- verifyEcsFormat (rawLogLines .get (2 ), ecsLogLines .get (0 ), null );
185- verifyEcsFormat (rawLogLines .get (3 ), ecsLogLines .get (1 ), null );
182+ verifyEcsFormat (rawLogLines .get (2 ), ecsLogLines .get (0 ));
183+ verifyEcsFormat (rawLogLines .get (3 ), ecsLogLines .get (1 ));
186184 }
187185
188186 @ Test
@@ -265,9 +263,9 @@ public void testDynamicConfiguration() throws Exception {
265263 assertThat (debugLogLine .get ("log.level" ).textValue ()).isEqualTo ("DEBUG" );
266264
267265 // WARN messages should match content but not format
268- verifyEcsFormat (originalLogLines .get (2 ).split ("\\ s+" ), ecsLogLines .get (0 ), null );
266+ verifyEcsFormat (originalLogLines .get (2 ).split ("\\ s+" ), ecsLogLines .get (0 ));
269267 assertThat (ecsLogLines .get (0 ).get ("log.level" ).textValue ()).isEqualTo ("WARN" );
270- verifyEcsFormat (originalLogLines .get (5 ).split ("\\ s+" ), ecsLogLines .get (2 ), null );
268+ verifyEcsFormat (originalLogLines .get (5 ).split ("\\ s+" ), ecsLogLines .get (2 ));
271269 assertThat (ecsLogLines .get (2 ).get ("log.level" ).textValue ()).isEqualTo ("WARN" );
272270
273271 // ERROR messages should be only in shade file in ECS format
@@ -288,14 +286,14 @@ private void verifyEcsLogLine(JsonNode ecsLogLineTree) {
288286 assertThat (ecsLogLineTree .get ("event.dataset" ).textValue ()).isEqualTo (serviceName + ".FILE" );
289287 assertThat (ecsLogLineTree .get ("service.version" ).textValue ()).isEqualTo ("v42" );
290288 assertThat (ecsLogLineTree .get ("some.field" ).textValue ()).isEqualTo ("some-value" );
289+ assertThat (ecsLogLineTree .get (TRACE_ID_MDC_KEY ).textValue ()).isEqualTo (transaction .getTraceContext ().getTraceId ().toString ());
290+ assertThat (ecsLogLineTree .get (TRANSACTION_ID_MDC_KEY ).textValue ()).isEqualTo (transaction .getTraceContext ().getTransactionId ().toString ());
291291 }
292292
293- @ Nonnull
294293 private ArrayList <JsonNode > readShadeLogFile () throws IOException {
295294 return TestUtils .readJsonFile (getShadeLogFilePath ());
296295 }
297296
298- @ Nonnull
299297 private ArrayList <String []> readRawLogLines () throws IOException {
300298 ArrayList <String []> rawLogLines ;
301299 try (Stream <String > stream = Files .lines (getOriginalLogFilePath ())) {
@@ -304,17 +302,15 @@ private ArrayList<String[]> readRawLogLines() throws IOException {
304302 return rawLogLines ;
305303 }
306304
307- @ Nonnull
308305 private Path getOriginalLogFilePath () {
309306 return Paths .get (logger .getLogFilePath ());
310307 }
311308
312- @ Nonnull
313309 protected String getShadeLogFilePath () {
314310 return Utils .computeShadeLogFilePath (logger .getLogFilePath (), loggingConfig .getLogEcsFormattingDestinationDir ());
315311 }
316312
317- private void verifyEcsFormat (String [] splitRawLogLine , JsonNode ecsLogLineTree , @ Nullable String traceId ) throws Exception {
313+ private void verifyEcsFormat (String [] splitRawLogLine , JsonNode ecsLogLineTree ) throws Exception {
318314 SimpleDateFormat timestampFormat = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" );
319315 Date rawTimestamp = timestampFormat .parse (splitRawLogLine [0 ]);
320316 timestampFormat .setTimeZone (TimeZone .getTimeZone ("UTC" ));
@@ -329,13 +325,8 @@ private void verifyEcsFormat(String[] splitRawLogLine, JsonNode ecsLogLineTree,
329325 assertThat (ecsLogLineTree .get ("event.dataset" ).textValue ()).isEqualTo (serviceName + ".FILE" );
330326 assertThat (ecsLogLineTree .get ("service.version" ).textValue ()).isEqualTo ("v42" );
331327 assertThat (ecsLogLineTree .get ("some.field" ).textValue ()).isEqualTo ("some-value" );
332- JsonNode jsonTraceId = ecsLogLineTree .get ("trace.id" );
333- if (traceId != null ) {
334- assertThat (jsonTraceId ).isNotNull ();
335- assertThat (jsonTraceId .asText ()).isEqualTo (traceId );
336- } else {
337- assertThat (jsonTraceId ).isNull ();
338- }
328+ assertThat (ecsLogLineTree .get (TRACE_ID_MDC_KEY ).textValue ()).isEqualTo (transaction .getTraceContext ().getTraceId ().toString ());
329+ assertThat (ecsLogLineTree .get (TRANSACTION_ID_MDC_KEY ).textValue ()).isEqualTo (transaction .getTraceContext ().getTransactionId ().toString ());
339330 }
340331
341332 /**
0 commit comments