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.
Continue with your solution of the last exercise. If this does not work, you can checkout the branch origin/solution-18-Make-Communication-Resilient
.
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);
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);
Push your microservice using the command cf push -n bulletinboard-ads-d012345
and make sure the execution is successful.
- 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 Cloud 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.
-
© 2018 SAP SE