I wanted to use micrometer ( as it is recommended over sleuth ) and implemented using
https://docs.spring.io/spring-boot/3.3/reference/actuator/tracing.html
see Issue - [contactdetailsservice,,]
2024-04-14T19:03:40.054+01:00 DEBUG 11856 --- [nio-9001-exec-1] [contactdetailsservice,,]o.s.web.servlet.DispatcherServlet : Detected StandardServletMultipartResolver
The spring boot app is almost pure out of the box i.e. no RestTemplateBuilder or WebClient bean ( as this is a standalone small service - not talking with any other)
Code :
Additional dependencies other than web ( no actuator )
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-bridge-otel</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-exporter-zipkin</artifactId>
</dependency>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
application.properties
spring.application.name=contactdetailsservice server.port=9001
logging.level.org.springframework.web.servlet.DispatcherServlet=DEBUG
management.tracing.sampling.probability=1
logging.pattern.correlation=[${spring.application.name:},%X{traceId:-},%X{spanId:-}]
logging.include-application-name=false
Sample controller code
@GetMapping("/customer/{cid}/contactdetails")
public ContactDetails getCustomerContactDetails(@PathVariable String cid) {
return details.stream().filter(detail -> cid.equals(detail.contactId())).findAny().orElse(null);
}
I googled but could not get any solution from this ( but may be there is something that I skipped) https://github.com/spring-cloud/spring-cloud-sleuth/issues/1147
This is how I resolved the issue
Step1: Added Actuator dependency. this started adding trace and span id
I don't have zipkin yet, so one of the service was logging errors.
Step 2: Commented the dependency **THIS IS TEMPORARY
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-exporter-zipkin</artifactId>
</dependency>
Step 3 : After spinning zipkin, I added / uncommented the Step 2 dependency
This is ok. But there is no correlation as I am creating webclient as
@Bean
WebClient webClient() {
return WebClient.create(); //INCORRECT
}
After looking at the documentation -
https://docs.spring.io/spring-boot/3.3/reference/io/rest-client.html#io.rest-client.webclient MyService
I created bean as (using Builder)
@Bean
WebClient webClient(WebClient.Builder webClientBuilder) {
return webClientBuilder.build(); //CORRECT
}
Now there is correlation - traceid
2024-04-14T21:16:04.764+01:00 DEBUG 17880 --- [dvlaservice]
[nio-9080-exec-1]
[dvlaservice,90c8edf37db433ff02ca5b4c8f0e0fdc,98b476bebb74a420]o.s.web.servlet.DispatcherServlet
2024-04-14T21:16:04.745+01:00 DEBUG 15592 --- [vehicledetailsservice]
[nio-9002-exec-1]
[vehicledetailsservice,90c8edf37db433ff02ca5b4c8f0e0fdc,21616d998b9ea1d0]o.
2024-04-14T21:16:04.558+01:00 DEBUG 13748 --- [nio-9001-exec-1]
[contactdetailsservice,90c8edf37db433ff02ca5b4c8f0e0fdc,207b5a67568fe97e]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With