Skip to content
This repository has been archived by the owner on Apr 22, 2024. It is now read-only.

Latest commit

 

History

History
68 lines (55 loc) · 3.81 KB

Exercise_19_Transfer_CorrelationID.md

File metadata and controls

68 lines (55 loc) · 3.81 KB

[Optional] Exercise 19: Transfer Correlation-ID

Learning Goal

We've discussed in the Logging & Tracing chapter that it is very important to add the Correlation-ID to the HTTP header of the subsequent/outgoing requests. That gives us the opportunity to analyze all logs originated by one request in Kibana.

The task is to set the Correlation-ID into the header of the outgoing User service call and to ensure that the MDC (Mapped Diagnostic Context) is set properly for logs emitted by Hystrix' threads.

Prerequisite

Continue with your solution of the last exercise. If this does not work, you can checkout the branch origin/solution-18-Make-Communication-Resilient.

Step 1: Transfer the Correlation-ID Into the New Thread

The original Correlation-ID is generated by the logging library and is set into the MDC of the request thread. As each Hystrix command runs in a separate thread, we need to ensure that the Correlation-ID is transferred properly into the MDC of the Hystrix thread. We need to:

  • remember the Correlation-ID in the command instance, e.g. in the constructor of your command class:
this.correlationId = LogContext.getCorrelationId();
  • set the remembered correlation ID into the MDC (using a method provided by the logging library) in the run method:
LogContext.initializeContext(this.correlationId);

Step 2: Add Correlation-ID to Outgoing Request

Then we want to add the Correlation-ID to the header of the User service call by making use of the RestTemplate.exchange() method:

HttpHeaders headers = new HttpHeaders();
headers.add(HTTP_HEADER_CORRELATION_ID, correlationId);
HttpEntity<User> request = new HttpEntity<>(headers);
return restTemplate.exchange(url, HttpMethod.GET, request, User.class);

Step 3: Push to Cloud Foundry

Push your microservice using the command cf push -n bulletinboard-ads-d012345 and make sure the execution is successful.

Step 4: Analyze Logs

  • Create some logs, use Postman to create some advertisements.
  • Go to Kibana: For Europe (Frankfurt) use https://logs.cf.eu10.hana.ondemand.com/ and for US East (VA) use https://logs.cf.us10.hana.ondemand.com/ and login with your SAP Business Technology Platform user, i.e. your email address and password.
  • If necessary, change the time frame so that your recent logs are visible (top right).
  • Switch to the "Request and Logs" perspective of the dashboard.
  • If necessary, limit the logs by specifying the space and organization you used for this exercise.
  • Find a request you would like to analyse in the "Requests" view, and select its Correlation-ID in the "Correlation Ids" view on the left.

Using this filter you can see all related log messages, including those of other microservices. Sadly, as the User microservice is running as part of a different organization for which you are not allowed to see its log messages, the log messages emitted by the User microservice will not be shown to you in Kibana.

Used Frameworks and Tools

Further Reading


© 2018 SAP SE

Previous Exercise Next Exercise