Skip to content

Commit

Permalink
Debug logging
Browse files Browse the repository at this point in the history
  • Loading branch information
IvoMajic committed Mar 30, 2024
1 parent cb390b0 commit 82f9b1e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.treblle</groupId>
<artifactId>treblle-spring-boot-starter</artifactId>
<version>2.0.4</version>
<version>2.0.5</version>
<name>treblle-spring-boot-starter</name>
<description>Official Treblle Starter for Spring Boot</description>
<url>https://github.com/Treblle/treblle-spring</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class TreblleProperties {
private String apiKey;
private String projectId;
private Integer filterOrder = Ordered.LOWEST_PRECEDENCE - 10; // Similar to HttpTraceFilter
private boolean debug = false;
private List<String> urlPatterns = Collections.emptyList();
private List<String> maskingKeywords = Collections.emptyList();

Expand Down Expand Up @@ -47,6 +48,14 @@ public void setFilterOrder(Integer filterOrder) {
this.filterOrder = filterOrder;
}

public boolean isDebug() {
return debug;
}

public void setDebug(boolean debug) {
this.debug = debug;
}

public List<String> getUrlPatterns() {
return urlPatterns;
}
Expand Down
47 changes: 44 additions & 3 deletions src/main/java/com/treblle/spring/service/TreblleServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@
import org.springframework.core.env.Environment;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.http.MediaType;
import org.springframework.http.client.*;
import org.springframework.scheduling.annotation.Async;
import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.time.Duration;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
Expand All @@ -33,7 +38,7 @@

public class TreblleServiceImpl implements TreblleService {

private static final Logger log = LoggerFactory.getLogger(TreblleServiceImpl.class);
private static final Logger LOGGER = LoggerFactory.getLogger(TreblleServiceImpl.class);

private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

Expand Down Expand Up @@ -71,6 +76,12 @@ public TreblleServiceImpl(Environment environment, TreblleProperties treblleProp
throw new IllegalStateException("Treblle Project ID is required.");
}

if (treblleProperties.isDebug()) {
restTemplateBuilder = restTemplateBuilder
.requestFactory(() -> new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()))
.additionalInterceptors(new TreblleLoggingInterceptor());
}

this.restTemplate =
restTemplateBuilder
.setConnectTimeout(Duration.ofSeconds(1))
Expand Down Expand Up @@ -182,10 +193,12 @@ public void maskAndSendPayload(TrebllePayload payload, byte[] requestBody, byte[
try {
restTemplate.postForEntity(Optional.ofNullable(treblleProperties.getEndpoint()).orElse(getRandomAPIEndpoint()), requestEntity, Void.class);
} catch (RestClientException exception) {
log.error("An error occurred while sending network request to Treblle.", exception);
if (treblleProperties.isDebug()) {
LOGGER.error("An error occurred while sending network request to Treblle.", exception);
}
}
} catch (Exception exception) {
log.error("An error occurred while sending data to Treblle.", exception);
LOGGER.error("An error occurred while preparing data for Treblle.", exception);
}
}

Expand Down Expand Up @@ -214,4 +227,32 @@ private JsonNode readBody(byte[] body, Consumer<RuntimeError> errorConsumer) {
return null;
}
}

public static class TreblleLoggingInterceptor implements ClientHttpRequestInterceptor {

private static final Logger LOGGER = LoggerFactory.getLogger(TreblleLoggingInterceptor.class);

@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
LOGGER.debug("Request Method: " + request.getMethod());
LOGGER.debug("Request URI: " + request.getURI());
LOGGER.debug("Request Headers: " + request.getHeaders());
if (body.length > 0) {
LOGGER.debug("Request Body: " + new String(body, StandardCharsets.UTF_8));
}

ClientHttpResponse response = execution.execute(request, body);

LOGGER.debug("Response Status Code: " + response.getStatusCode());
LOGGER.debug("Response Headers: " + response.getHeaders());
if (response.getBody() != null) {
String responseBody = StreamUtils.copyToString(response.getBody(), StandardCharsets.UTF_8);
LOGGER.debug("Response Body: " + responseBody);
}

return response;
}

}

}

0 comments on commit 82f9b1e

Please sign in to comment.