Skip to content

Commit 5d4c860

Browse files
committed
added error handling and READMEs
1 parent 67a895e commit 5d4c860

18 files changed

Lines changed: 124 additions & 73 deletions

File tree

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
11
# Log Tracing with Spring Cloud Sleuth
22

3-
TBD
3+
This project shows how to implement tracing in a network of Spring Boot applications.
4+
5+
This application is a service facing the user (a "downstream" service), meaning that
6+
it accesses other upstream services to provide its functionality.
7+
8+
1. Start the [upstream service](../sleuth-upstream-service/) with `./gradlew bootrun`
9+
1. Start this service with `./gradlew bootrun`
10+
1. Open `http://localhost:8080/customers-with-address/{id}` where IDs from 1-50 are
11+
will return a valid HTTP 200 response and other IDs will return an error response.
12+
1. Look into the log files of both services and verify that both contain the same
13+
trace id.

sleuth-downstream-service/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ ext {
2929
dependencies {
3030
compile('org.springframework.cloud:spring-cloud-starter-feign')
3131
compile('org.springframework.cloud:spring-cloud-starter-sleuth')
32+
compile('org.springframework.cloud:spring-cloud-starter-zipkin')
3233
compile('org.springframework.boot:spring-boot-starter-web')
3334
testCompile('org.springframework.boot:spring-boot-starter-test')
3435
}

sleuth-downstream-service/src/main/java/com/example/demo/Controller.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public Controller(CustomerClient customerClient, AddressClient addressClient) {
2424

2525
@GetMapping(path = "customers-with-address/{id}")
2626
public CustomerAndAddress getCustomerWithAddress(@PathVariable("id") long customerId){
27-
logger.info("COLLECTING CUSTOMER AND ADDRESS WITH ID {} FROM SECONDARY SERVICE", customerId);
27+
logger.info("COLLECTING CUSTOMER AND ADDRESS WITH ID {} FROM UPSTREAM SERVICE", customerId);
2828
Customer customer = customerClient.getCustomer(customerId);
2929
Address address = addressClient.getAddressForCustomerId(customerId);
3030
return new CustomerAndAddress(customer, address);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.example.demo;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.slf4j.MDC;
6+
import org.springframework.http.HttpStatus;
7+
import org.springframework.web.bind.annotation.ControllerAdvice;
8+
import org.springframework.web.bind.annotation.ExceptionHandler;
9+
import org.springframework.web.bind.annotation.ResponseBody;
10+
import org.springframework.web.bind.annotation.ResponseStatus;
11+
12+
@ControllerAdvice
13+
public class ErrorHandler {
14+
15+
private Logger logger = LoggerFactory.getLogger(ErrorHandler.class);
16+
17+
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
18+
@ExceptionHandler(Exception.class)
19+
@ResponseBody
20+
public String handleInternalError(Exception e) {
21+
logger.error("internal server error", e);
22+
return String.format("Internal Server Error (traceId: %s)", MDC.get("X-B3-TraceId"));
23+
}
24+
25+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.example.demo;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.web.filter.CommonsRequestLoggingFilter;
6+
7+
@Configuration
8+
public class RequestLoggingFilterConfig {
9+
10+
@Bean
11+
public CommonsRequestLoggingFilter logFilter() {
12+
CommonsRequestLoggingFilter filter
13+
= new CommonsRequestLoggingFilter();
14+
filter.setIncludeHeaders(true);
15+
filter.setIncludeQueryString(true);
16+
filter.setIncludePayload(true);
17+
filter.setMaxPayloadLength(10000);
18+
return filter;
19+
}
20+
}

sleuth-downstream-service/src/main/resources/application.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
server.port: 8080
22

3-
logging.level.com.example.demo.CustomerClient: DEBUG
4-
logging.level.com.example.demo.AddressClient: DEBUG
5-
logging.level.org.hibernate.SQL: DEBUG
3+
spring.zipkin.baseUrl: http://localhost:9411/
64

75
customers:
86
ribbon:
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# this file is loaded by Spring Cloud before application.properties
2-
spring.application.name=sleuth-primary-service
2+
spring.application.name=sleuth-downstream-service

sleuth-downstream-service/src/main/resources/logback-spring.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
<!-- You can override this to have a custom pattern -->
1010
<property name="CONSOLE_LOG_PATTERN"
11-
value="%d{yyyy-MM-dd HH:mm:ss.SSS} %5p [${springAppName},%X{X-B3-TraceId:-},%X{X-B3-SpanId:-},%X{X-Span-Export:-}] ${PID:- } --- [%15.15t] %-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"/>
11+
value="%d{yyyy-MM-dd HH:mm:ss.SSS} %5p [${springAppName},%X{X-B3-TraceId:-}] %m%n"/>
1212

1313
<!-- Appender to log to console -->
1414
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">

sleuth-primary-service/src/main/java/com/example/demo/Address.java

Lines changed: 0 additions & 24 deletions
This file was deleted.

sleuth-primary-service/src/main/java/com/example/demo/Customer.java

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)