Correlation IDs

And how they can keep you sane.

Agenda:

  • What is a correlation id?
  • How could it help me?
  • How do I implement it in Java?

 

Cheatsheet:

  • https://github.homedepot.com/mc331w/correlation-id

What is a correlation id?


A unique identifier attached to a thread that allows reference to a particular transaction or event chain.

 

 

What does it look like?

How could it help me?

 

In THD we seem to have two types of calls:

Type A:

Type B:

How do I implement it in Java?

 

With the help of the Mapped Diagnostic Context, Javas way to enrich log messages with pieces of information that may not be available in the scope of where the logging actually occurs.

 

Slf4jMDCFilterConfiguration.java:  A configuration file is used to create a FilterRegistrationBean, which handles adding information to our header.

 

Slf4jMDCFilter.java: A filter to manage adding / creating a correlation id in our MDC and then clean up our MDC when the thread completes.


log4j2.xml: Update your log4j logging pattern to use our new correlation id, using the value - %X{Slf4jMDCFilter.UUID}

Appendix A

 

Aspect Logging with an Interceptor:

If your application is using Aspect Logging, you can add the following to our preHandler, in your interceptor file. This will add your correlation id to the MDC - though it does not allow you to pass in a correlation id in the incoming request.

 

UUID requestId = UUID.randomUUID();
MDC.clear();
MDC.put("requestId", String.valueOf(requestId));

Appendix B

 

BigQuery:

Here is a query to help look for your correlation id, note that in this example, their id is prefixed with "requestId=".

 

select  
timestamp, REGEXP_EXTRACT(message, r'^[\S\s]*requestId=([0-9a-zA-Z-]*) [\s\S]*$') as uniqueid, message 
from `io1-datalake-views.events_pr.cf_app_logs`
where
service LIKE "cloudfoundry/%Design%"
and timestamp >= '2021-01-30 20:31:53.889 UTC'
and message like '%discountAmount%'order by timestamp desc

Correlation ID

By Michael Celeste

Correlation ID

  • 404