Tracing Module with Mule Runtime 4.4.0

Tracing Module with Mule Runtime 4.4.0

Introduction to Correlation Id and Tracing Module

With the earlier version of Mule Runtime, it was difficult to find the exact point of failure ( in which flow the error actually happened) when we had a complex API with multiple flows and sub-flows configured.?

Correlation Id is part of a Mule Event and is automatically generated when the application receives a message. Correlation Ids cannot be changed within an application flow and can be set only at the source.?

When a HTTP Request is received, it checks for the "X-Correlation-Id" header.

  • If present, then the same Correlation Id will be used across the Mule Application
  • Else, a Correlation Id is randomly generated by Java functionality

With the earlier version of Mule Runtime, it was not possible to set the Correlation Id for a particular flow.

So, to overcome this issue, MuleSoft recently introduced the Tracing Module as one of the new features which is only available with the Mule 4.4.0 or later Runtime engine. With this new Tracing Module, we can modify the Correlation Id during the flow execution of any API. It helps us to have better Correlation Id management throughout the flow which in turn, help us conduct better code troubleshooting for any issue.

Perquisites:

  • Anypoint Studio 7.11.1(Latest version can be downloaded from here).
  • Mule Runtime 4.4.0 (it's already embedded in Anypoint 7.11.1 version)
  • Postman or Any REST client testing tool.

So, let's explore the Tracing Module used in the below example:

1) Download the Tracing Module version 1.0.0 from Anypoint Exchange.

No alt text provided for this image

2) Once downloaded, it will be available in your Studio palette with all available operations as shown below:

No alt text provided for this image

3) Created a small API to examine the Correlation Id behavior throughout the API. (Code Snippet available at the end)

  • Before Logger - to check the correlation id before the Tracing Module.
  • During Logger - to check the correlation id during the Tracing Module.
  • ?After Logger - to check the correlation id after the tracing Module.
  • Tracing Module (With Correlation Id) - to set the correlation id coming from input request.

No alt text provided for this image

4) Now, to test the API, we are sending the request from Postman by calling the API Endpoint.

Please note that "X-Correlation-Id" is passed in the Header.

No alt text provided for this image
No alt text provided for this image

?5) After the successful call, let’s check our loggers which get printed in the Anypoint Studio Console. These are highlighted in yellow.

No alt text provided for this image

As we can see, our first logger which is before the Tracing Module, it prints the Correlation Id which was sent from the Postman in "X-Correlation-Id" Header.

The second logger which is inside the Tracing Module scope, shows the Correlation Id has been modified as per user input. In our case it is #[payload.id].

Lastly, the third logger which is after the Tracing Module, highlights that it's reverted back to the original Correlation Id which was set by the client in the request using "X-Correlation-Id"

Conclusion/Benefits

Hence, we can conclude that with the help of a Tracing Module, we now have flexibility to have a user defined own Correlation Id for a particular flow.

This future definitely helps us to troubleshoot any kind of issue when the logs are huge and it is a bit difficult to trace the logs.

We will explore some more new features of the Tracing Module in the next article. Please stay tuned.

Happy Learning!!

Code Snippet

<?xml version="1.0" encoding="UTF-8"?

<mule xmlns:ee="https://www.mulesoft.org/schema/mule/ee/core" xmlns:tracing="https://www.mulesoft.org/schema/mule/tracing"
xmlns:http="https://www.mulesoft.org/schema/mule/http"
xmlns="https://www.mulesoft.org/schema/mule/core" xmlns:doc="https://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://www.mulesoft.org/schema/mule/core?https://www.mulesoft.org/schema/mule/core/current/mule.xsd
https://www.mulesoft.org/schema/mule/http?https://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
https://www.mulesoft.org/schema/mule/tracing?https://www.mulesoft.org/schema/mule/tracing/current/mule-tracing.xsd
https://www.mulesoft.org/schema/mule/ee/core?https://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="18ea5944-3004-4708-8899-c76905447157" >
<http:listener-connection host="0.0.0.0" port="8081" />
</http:listener-config>
<flow name="muletracemoduleFlow" doc:id="fc4d1886-827a-4cf8-911c-7aafc55f045d" >
<http:listener doc:name="Listener" doc:id="b3aa117d-7c9d-4429-ae14-8ca80f264647" config-ref="HTTP_Listener_config" path="/TracingDemo"/>
<logger level="INFO" doc:name="Before" doc:id="c2ff4f42-20f1-4161-b764-e61da0a63627" message='#["Correlation id " ++ attributes.headers."X-Correlation-Id"++ " before the Trace Module"]'/>
<tracing:with-correlation-id doc:name="With CorrelationID" doc:id="9e3117e2-1c3a-4572-84bd-efb2e7c321df" correlationId="#[payload.id]">
<ee:transform doc:name="Transform Message" doc:id="1759c150-aae0-46c8-8cc9-9264cd84eb9e" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
payload]]></ee:set-payload>
</ee:message>
</ee:transform>
<logger level="INFO" doc:name="During" doc:id="6235333a-c576-4b80-a5b9-54629adca902" message='#["Correlation id "++?payload.id?++" ?within trace Module Scope"]'/>
</tracing:with-correlation-id>
<logger level="INFO" doc:name="After" doc:id="13c0f791-2410-4d04-b09d-b17eea48a6cc" message='#["Correlation id " ++ attributes.headers."X-Correlation-Id"++ " before the Trace Module"]'/>
</flow>
</mule>        

Author

Amit Kumar, MuleSoft Mentor, Accenture

No alt text provided for this image
Ashish Jaiswal

MuleSoft | GCP | Digibee | Workato | MCIA | MCPA | MCD | MCIASSOC | MCD - LEVEL 2 | 5x MuleSoft Certified | 3x Workato Certified | Ex - Oracle Integration | Ex - PeopleSoft Developer

1 年

After deployment on CloudHub's public network, does the application work properly without any MuleSoft support ? Because Cloudhub ingore the application log4j.xml

回复
Sourav Chandra

Architect | Technical Lead| MuleSoft Certified Architect| MCD level 1| Mule Delivery Champion | MuleSoft Mentor | Ex-Wipro | Ex-TCS | Specializing in Enterprise Integration | Member of Kolkata Mulesoft meetup group

2 年

Good one! Thanks for sharing

回复
MANSI SINGH

Mulesoft Developer | Mule 4 Certified | 5xMule Certified | Application Development Senior Analyst at Accenture | ex-Capgeminite

2 年

Awesome Amit Kumar way to go ??

回复
ATUL KUMAR BRAHMA

Application Development Senior Analyst Accenture ?MuleSoft Certified Developer I & II? KIIT University

2 年

Always learns from you a lot.. thank you for always sharing sir ????

回复
Pascal Gacina

Partner Vice President Consulting - Expert - Enterprise Event & Integration Practice (EDA/API/iPaaS) - CGI Digital Innovation Center

2 年
回复

要查看或添加评论,请登录

MuleSoft Community的更多文章

社区洞察

其他会员也浏览了