diff --git a/sleuth-downstream-service/README.md b/sleuth-downstream-service/README.md index 384a40c..429dc8a 100644 --- a/sleuth-downstream-service/README.md +++ b/sleuth-downstream-service/README.md @@ -1,3 +1,13 @@ # Log Tracing with Spring Cloud Sleuth -TBD \ No newline at end of file +This project shows how to implement tracing in a network of Spring Boot applications. + +This application is a service facing the user (a "downstream" service), meaning that +it accesses other upstream services to provide its functionality. + +1. Start the [upstream service](../sleuth-upstream-service/) with `./gradlew bootrun` +1. Start this service with `./gradlew bootrun` +1. Open `http://localhost:8080/customers-with-address/{id}` where IDs from 1-50 are + will return a valid HTTP 200 response and other IDs will return an error response. +1. Look into the log files of both services and verify that both contain the same + trace id. diff --git a/sleuth-downstream-service/build.gradle b/sleuth-downstream-service/build.gradle index e7b020e..f2da578 100644 --- a/sleuth-downstream-service/build.gradle +++ b/sleuth-downstream-service/build.gradle @@ -29,6 +29,7 @@ ext { dependencies { compile('org.springframework.cloud:spring-cloud-starter-feign') compile('org.springframework.cloud:spring-cloud-starter-sleuth') + compile('org.springframework.cloud:spring-cloud-starter-zipkin') compile('org.springframework.boot:spring-boot-starter-web') testCompile('org.springframework.boot:spring-boot-starter-test') } diff --git a/sleuth-downstream-service/src/main/java/com/example/demo/Controller.java b/sleuth-downstream-service/src/main/java/com/example/demo/Controller.java index 982f933..948561c 100644 --- a/sleuth-downstream-service/src/main/java/com/example/demo/Controller.java +++ b/sleuth-downstream-service/src/main/java/com/example/demo/Controller.java @@ -24,7 +24,7 @@ public class Controller { @GetMapping(path = "customers-with-address/{id}") public CustomerAndAddress getCustomerWithAddress(@PathVariable("id") long customerId){ - logger.info("COLLECTING CUSTOMER AND ADDRESS WITH ID {} FROM SECONDARY SERVICE", customerId); + logger.info("COLLECTING CUSTOMER AND ADDRESS WITH ID {} FROM UPSTREAM SERVICE", customerId); Customer customer = customerClient.getCustomer(customerId); Address address = addressClient.getAddressForCustomerId(customerId); return new CustomerAndAddress(customer, address); diff --git a/sleuth-downstream-service/src/main/java/com/example/demo/ErrorHandler.java b/sleuth-downstream-service/src/main/java/com/example/demo/ErrorHandler.java new file mode 100644 index 0000000..3a9cdc3 --- /dev/null +++ b/sleuth-downstream-service/src/main/java/com/example/demo/ErrorHandler.java @@ -0,0 +1,25 @@ +package com.example.demo; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.slf4j.MDC; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ControllerAdvice +public class ErrorHandler { + + private Logger logger = LoggerFactory.getLogger(ErrorHandler.class); + + @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR) + @ExceptionHandler(Exception.class) + @ResponseBody + public String handleInternalError(Exception e) { + logger.error("internal server error", e); + return String.format("Internal Server Error (traceId: %s)", MDC.get("X-B3-TraceId")); + } + +} diff --git a/sleuth-downstream-service/src/main/java/com/example/demo/RequestLoggingFilterConfig.java b/sleuth-downstream-service/src/main/java/com/example/demo/RequestLoggingFilterConfig.java new file mode 100644 index 0000000..b9e7fd2 --- /dev/null +++ b/sleuth-downstream-service/src/main/java/com/example/demo/RequestLoggingFilterConfig.java @@ -0,0 +1,20 @@ +package com.example.demo; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.filter.CommonsRequestLoggingFilter; + +@Configuration +public class RequestLoggingFilterConfig { + + @Bean + public CommonsRequestLoggingFilter logFilter() { + CommonsRequestLoggingFilter filter + = new CommonsRequestLoggingFilter(); + filter.setIncludeHeaders(true); + filter.setIncludeQueryString(true); + filter.setIncludePayload(true); + filter.setMaxPayloadLength(10000); + return filter; + } +} \ No newline at end of file diff --git a/sleuth-downstream-service/src/main/resources/application.yml b/sleuth-downstream-service/src/main/resources/application.yml index 6ef1938..133804f 100644 --- a/sleuth-downstream-service/src/main/resources/application.yml +++ b/sleuth-downstream-service/src/main/resources/application.yml @@ -1,8 +1,6 @@ server.port: 8080 -logging.level.com.example.demo.CustomerClient: DEBUG -logging.level.com.example.demo.AddressClient: DEBUG -logging.level.org.hibernate.SQL: DEBUG +spring.zipkin.baseUrl: http://localhost:9411/ customers: ribbon: diff --git a/sleuth-downstream-service/src/main/resources/bootstrap.properties b/sleuth-downstream-service/src/main/resources/bootstrap.properties index 20aaeee..bdd488d 100644 --- a/sleuth-downstream-service/src/main/resources/bootstrap.properties +++ b/sleuth-downstream-service/src/main/resources/bootstrap.properties @@ -1,2 +1,2 @@ # this file is loaded by Spring Cloud before application.properties -spring.application.name=sleuth-primary-service \ No newline at end of file +spring.application.name=sleuth-downstream-service \ No newline at end of file diff --git a/sleuth-downstream-service/src/main/resources/logback-spring.xml b/sleuth-downstream-service/src/main/resources/logback-spring.xml index 9335758..556d189 100644 --- a/sleuth-downstream-service/src/main/resources/logback-spring.xml +++ b/sleuth-downstream-service/src/main/resources/logback-spring.xml @@ -8,7 +8,7 @@ + value="%d{yyyy-MM-dd HH:mm:ss.SSS} %5p [${springAppName},%X{X-B3-TraceId:-}] %m%n"/> diff --git a/sleuth-primary-service/src/main/java/com/example/demo/Address.java b/sleuth-primary-service/src/main/java/com/example/demo/Address.java deleted file mode 100644 index 523f539..0000000 --- a/sleuth-primary-service/src/main/java/com/example/demo/Address.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.example.demo; - -public class Address { - - private long id; - - private String street; - - public long getId() { - return id; - } - - public void setId(long id) { - this.id = id; - } - - public String getStreet() { - return street; - } - - public void setStreet(String street) { - this.street = street; - } -} diff --git a/sleuth-primary-service/src/main/java/com/example/demo/Customer.java b/sleuth-primary-service/src/main/java/com/example/demo/Customer.java deleted file mode 100644 index 7142577..0000000 --- a/sleuth-primary-service/src/main/java/com/example/demo/Customer.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.example.demo; - -public class Customer { - - private long id; - - private String name; - - public long getId() { - return id; - } - - public void setId(long id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } -} diff --git a/sleuth-primary-service/src/main/java/com/example/demo/FeignConfiguration.java b/sleuth-primary-service/src/main/java/com/example/demo/FeignConfiguration.java deleted file mode 100644 index 07e62bd..0000000 --- a/sleuth-primary-service/src/main/java/com/example/demo/FeignConfiguration.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.example.demo; - -import feign.Logger; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -@Configuration -public class FeignConfiguration { - - @Bean - public Logger.Level logLevel(){ - return Logger.Level.FULL; - } - -} diff --git a/sleuth-upstream-service/README.md b/sleuth-upstream-service/README.md index 384a40c..dddba9e 100644 --- a/sleuth-upstream-service/README.md +++ b/sleuth-upstream-service/README.md @@ -1,3 +1,13 @@ # Log Tracing with Spring Cloud Sleuth -TBD \ No newline at end of file +This project shows how to implement tracing in a network of Spring Boot applications. + +This application is a service that's not facing the user (a "downstream" service), meaning that +it is accessed by user-facing services so they can provide their functionality. + +1. Start this service with `./gradlew bootrun` +1. Start the [downstream service](../sleuth-downstream-service/) with `./gradlew bootrun` +1. Open `http://localhost:8080/customers-with-address/{id}` where IDs from 1-50 are + will return a valid HTTP 200 response and other IDs will return an error response. +1. Look into the log files of both services and verify that both contain the same + trace id. diff --git a/sleuth-upstream-service/build.gradle b/sleuth-upstream-service/build.gradle index 5f4d097..3ffd010 100644 --- a/sleuth-upstream-service/build.gradle +++ b/sleuth-upstream-service/build.gradle @@ -28,6 +28,7 @@ ext { dependencies { compile('org.springframework.cloud:spring-cloud-starter-sleuth') + compile('org.springframework.cloud:spring-cloud-starter-zipkin') compile('org.springframework.boot:spring-boot-starter-web') testCompile('org.springframework.boot:spring-boot-starter-test') } diff --git a/sleuth-upstream-service/src/main/java/com/example/demo/ErrorHandler.java b/sleuth-upstream-service/src/main/java/com/example/demo/ErrorHandler.java new file mode 100644 index 0000000..3a9cdc3 --- /dev/null +++ b/sleuth-upstream-service/src/main/java/com/example/demo/ErrorHandler.java @@ -0,0 +1,25 @@ +package com.example.demo; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.slf4j.MDC; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ControllerAdvice +public class ErrorHandler { + + private Logger logger = LoggerFactory.getLogger(ErrorHandler.class); + + @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR) + @ExceptionHandler(Exception.class) + @ResponseBody + public String handleInternalError(Exception e) { + logger.error("internal server error", e); + return String.format("Internal Server Error (traceId: %s)", MDC.get("X-B3-TraceId")); + } + +} diff --git a/sleuth-upstream-service/src/main/java/com/example/demo/RequestLoggingFilterConfig.java b/sleuth-upstream-service/src/main/java/com/example/demo/RequestLoggingFilterConfig.java new file mode 100644 index 0000000..b9e7fd2 --- /dev/null +++ b/sleuth-upstream-service/src/main/java/com/example/demo/RequestLoggingFilterConfig.java @@ -0,0 +1,20 @@ +package com.example.demo; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.filter.CommonsRequestLoggingFilter; + +@Configuration +public class RequestLoggingFilterConfig { + + @Bean + public CommonsRequestLoggingFilter logFilter() { + CommonsRequestLoggingFilter filter + = new CommonsRequestLoggingFilter(); + filter.setIncludeHeaders(true); + filter.setIncludeQueryString(true); + filter.setIncludePayload(true); + filter.setMaxPayloadLength(10000); + return filter; + } +} \ No newline at end of file diff --git a/sleuth-upstream-service/src/main/resources/application.yml b/sleuth-upstream-service/src/main/resources/application.yml index ea01bdf..3940b33 100644 --- a/sleuth-upstream-service/src/main/resources/application.yml +++ b/sleuth-upstream-service/src/main/resources/application.yml @@ -1,3 +1,7 @@ +logging.level.org.springframework.web.filter.CommonsRequestLoggingFilter: DEBUG + +spring.zipkin.baseUrl: http://localhost:9411/ + server.port: 8081 spring: output: diff --git a/sleuth-upstream-service/src/main/resources/bootstrap.properties b/sleuth-upstream-service/src/main/resources/bootstrap.properties index d305ead..10c4229 100644 --- a/sleuth-upstream-service/src/main/resources/bootstrap.properties +++ b/sleuth-upstream-service/src/main/resources/bootstrap.properties @@ -1,2 +1,2 @@ # this file is loaded by Spring Cloud before application.properties -spring.application.name=sleuth-secondary-service \ No newline at end of file +spring.application.name=sleuth-upstream-service \ No newline at end of file diff --git a/sleuth-upstream-service/src/main/resources/logback-spring.xml b/sleuth-upstream-service/src/main/resources/logback-spring.xml index 9335758..556d189 100644 --- a/sleuth-upstream-service/src/main/resources/logback-spring.xml +++ b/sleuth-upstream-service/src/main/resources/logback-spring.xml @@ -8,7 +8,7 @@ + value="%d{yyyy-MM-dd HH:mm:ss.SSS} %5p [${springAppName},%X{X-B3-TraceId:-}] %m%n"/>