top of page

Tracing Module in Mule 4



 

In this post:

 


The tracing module enables you to enhance your logs by adding, removing, and clearing all variables from the logging context for a given Mule event. It also enables you to modify the correlation ID during flow execution.



Uses Of Tracing Module

  • Clear all the logging variables from the event logging context.

  • Remove a logging variable from the logging context.

  • Set logging variables to logging context.

  • Modify the correlation ID during flow execution




Tracing Module Application


1 - Create Mule application: Go to Anypoint studio and create a mule project with the name mulesoft-tracing-module-integration.


2 - Add Tracing module: In the mule palette view, click on + Add Modules and search for tracing modules. Drag the Tracing module to your modules.


3 - HTTP Listener: Create an HTTP listener with the default configuration and specify the /trace base path.


4 - MDC Logging: Mapped Diagnostic Context (MDC) enriches logging and improves tracking by providing more context or information in the logs for the current Mule event. By default, Mule logs two MDC entries: processor, which shows the location of the current event; and event, which shows the correlation ID of the event. Open the log4j.xml file and replace [processor: %X{processorPath}; event: %X{correlationId}] with [%MDC].


5 - Create set variable logging: Create a set logging variable for customerId and request path as below. In the logger, we will print the payload.




6 - Run application: Run the application and test it out with the below command.



7 - Verify logs: Verify the logs customerId and request path will be printed in the context path logs. The below log will be printed in the file location mentioned in log4j.xml.



 

Note: You will see different logs in the console with processor and event.

 

8 - Console log4j changes: Open log4j.xml, add Console logging in Appenders tag. Also, add this reference to the AsyncRoot tag.



9 - Run application: Re-run the Mule application and verify the logger. The same logging will be printed in the console.


10 - Remove logging variable: After the logger, let’s remove one of the logging variables i.e request path. Also after this, remove the logging variable, and add the logger for payload.




11 - Run application and verify: Re-run the application, hit the endpoint, and verify the logs, you will see the request path will be removed from the context path. When you will hit the application multiple times, you will see different correlation IDs.



12 - Clear logging variable: After the payload logger, let's add a clear logging variable, it will remove all the logging variables which were created during the flow.



13 - Re-run application and verify: Run the application again and hit the endpoint and verify the logs. There will be no customerId logging variable.



14 - With Correlation ID: Let’s add with correlation id after payload logger, and change correlation Id. I have changed the correlation id to append my name. Add the payload logger into this too.



15 - Run the application: Re-run the application, hit the URL again, and verify the logs with the correlation logger. This updated correlation id will be visible only for the logger available under the correlation ID.



16 - Add payload logger: Add payload logger at the last, you can re-run the application and the updated correlation id will not be available to this logger.




GitHub repository



References



1,272 views0 comments
bottom of page