Skip to content

Commit

Permalink
Add the ability to log the payload of spans/traces
Browse files Browse the repository at this point in the history
- Add Apache httpclient dependency
- Add ZipkinRestTemplateCustomizer bean
- Customize RestTemplate with the `HttpComponentsClientHttpRequestFactory`
  • Loading branch information
ghillert committed Feb 4, 2022
1 parent 1a19887 commit 7aa9932
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
6 changes: 5 additions & 1 deletion users/pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2021 Oracle and/or its affiliates.
Copyright (c) 2021, 2022 Oracle and/or its affiliates.
Licensed under the Universal Permissive License v 1.0 as shown at
https://oss.oracle.com/licenses/upl.
Expand Down Expand Up @@ -104,6 +104,10 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-zipkin</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates.
* Copyright (c) 2021, 2022 Oracle and/or its affiliates.
*
* Licensed under the Universal Permissive License v 1.0 as shown at
* https://oss.oracle.com/licenses/upl.
*/
package com.oracle.coherence.examples.sockshop.spring.users;

import com.oracle.coherence.examples.sockshop.spring.users.model.User;
import com.oracle.coherence.spring.configuration.annotation.EnableCoherence;
import com.oracle.coherence.spring.data.config.EnableCoherenceRepositories;
import com.oracle.coherence.examples.sockshop.spring.users.service.UserService;
import io.swagger.v3.oas.annotations.ExternalDocumentation;
Expand All @@ -29,7 +28,9 @@
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.sleuth.zipkin2.ZipkinRestTemplateCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.web.client.RestTemplate;

Expand Down Expand Up @@ -88,8 +89,13 @@ public void run(ApplicationArguments args) throws Exception {
}

@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
// Do any additional configuration here
return builder.build();
public ZipkinRestTemplateCustomizer zipkinRestTemplateCustomizer() {
return new ZipkinRestTemplateCustomizer() {
@Override
public RestTemplate customizeTemplate(RestTemplate restTemplate) {
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
return restTemplate;
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.sleuth.SpanName;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.Link;

Expand Down Expand Up @@ -46,13 +47,11 @@ public class CustomerController {
@Autowired
private UserService userService;


@GetMapping
@Operation(summary = "Return all customers; or empty collection if no customer found")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "if the retrieval is successful")
})
//@NewSpan
public CollectionModel<User> getAllCustomers() {
final Collection<User> users = this.userService.getAllUsers();
for (final User user : users) {
Expand All @@ -71,7 +70,6 @@ public CollectionModel<User> getAllCustomers() {
@ApiResponses({
@ApiResponse(responseCode = "200", description = "if the retrieval is successful")
})
//@NewSpan
public User getCustomer(
@Parameter(description = "Customer identifier") @PathVariable("id") String id) {
return userService.getOrCreate(id);
Expand All @@ -82,20 +80,17 @@ public User getCustomer(
@ApiResponses({
@ApiResponse(responseCode = "200", description = "if the delete is successful")
})
//@NewSpan
public BooleanStatusResponse deleteCustomer(
@Parameter(description = "Customer identifier") @PathVariable("id") String id) {
User prev = this.userService.removeUser(id);
return new BooleanStatusResponse(prev != null);
}

//
@GetMapping("/{id}/cards")
@Operation(summary = "Return all cards for the specified customer identifier")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "if the retrieval is successful")
})
//@NewSpan
public CollectionModel<Object> getCustomerCards(
@Parameter(description = "Customer identifier") @PathVariable("id") String id) {
final User user = this.userService.getUser(id);
Expand All @@ -115,7 +110,6 @@ public CollectionModel<Object> getCustomerCards(
@ApiResponses({
@ApiResponse(responseCode = "200", description = "if the retrieval is successful")
})
//@NewSpan
public Object getCustomerAddresses(
@Parameter(description = "Customer identifier") @PathVariable("id") String id) {
final User user = this.userService.getUser(id);
Expand Down
1 change: 1 addition & 0 deletions users/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ logging:
org.springframework.security: info
org.springframework.web: info
com.oracle.coherence.spring: info
org.apache.http: info # set to debug to see logging of http payloads

0 comments on commit 7aa9932

Please sign in to comment.