Skip to content

Commit

Permalink
Fix and update telemetry properties and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
armando-rodriguez-cko committed Dec 16, 2024
1 parent d86557b commit 9ff02f7
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 194 deletions.
47 changes: 24 additions & 23 deletions src/main/java/com/checkout/ApacheHttpClientTransport.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
Expand Down Expand Up @@ -64,7 +63,7 @@ class ApacheHttpClientTransport implements Transport {
private final TransportConfiguration transportConfiguration;
private final CheckoutConfiguration configuration;

private static final ThreadLocal<Map<String, Object>> telemetryData = ThreadLocal.withInitial(HashMap::new);
private static final ThreadLocal<RequestMetrics> telemetryData = ThreadLocal.withInitial(RequestMetrics::new);

ApacheHttpClientTransport(
final URI baseUri,
Expand Down Expand Up @@ -167,10 +166,7 @@ private Response performCall(final SdkAuthorization authorization,

String currentRequestId = UUID.randomUUID().toString();

if (configuration.isTelemetryEnabled()) {
String telemetryHeader = generateTelemetryHeader(currentRequestId);
request.setHeader("cko-sdk-telemetry", telemetryHeader);
}
addTelemetryHeader(request, currentRequestId);

long startTime = System.currentTimeMillis();

Expand All @@ -197,35 +193,40 @@ private Response performCall(final SdkAuthorization authorization,
}
return Response.builder().statusCode(statusCode).headers(headers).build();
} catch (final NoHttpResponseException e) {
log.error("Target server failed to respond with a valid HTTP response.");
return Response.builder().statusCode(HttpStatus.SC_GATEWAY_TIMEOUT).build();
return handleException(e, "Target server failed to respond with a valid HTTP response.");
} catch (final Exception e) {
log.error("Exception occurred during the execution of the client...", e);
return handleException(e, "Exception occurred during the execution of the client...");
}
return Response.builder().statusCode(transportConfiguration.getDefaultHttpStatusCode()).build();
}

private String generateTelemetryHeader(String currentRequestId) {
Map<String, Object> data = getTelemetryData();
String prevRequestId = (String) data.get("prevRequestId");
Long prevRequestDuration = (Long) data.get("prevRequestDuration");
private void addTelemetryHeader(HttpUriRequest request, String currentRequestId) {
if (configuration.isTelemetryEnabled()) {
String telemetryHeader = generateTelemetryHeader(currentRequestId);
request.setHeader("cko-sdk-telemetry", telemetryHeader);
}
}

return String.format("{\"requestId\":\"%s\",\"prevRequestId\":\"%s\",\"prevRequestDuration\":%d}",
currentRequestId,
prevRequestId != null ? prevRequestId : "N/A",
prevRequestDuration != null ? prevRequestDuration : 0);
private String generateTelemetryHeader(String currentRequestId) {
RequestMetrics metrics = getTelemetryData();
metrics.setRequestId(currentRequestId);
return metrics.toTelemetryHeader();
}

private static void updateTelemetryData(String requestId, long duration) {
Map<String, Object> data = telemetryData.get();
data.put("prevRequestId", requestId);
data.put("prevRequestDuration", duration);
RequestMetrics metrics = telemetryData.get();
metrics.setPrevRequestId(requestId);
metrics.setPrevRequestDuration(duration);
}

private static Map<String, Object> getTelemetryData() {
private static RequestMetrics getTelemetryData() {
return telemetryData.get();
}

private Response handleException(Exception e, String errorMessage) {
log.error(errorMessage, e);
return Response.builder().statusCode(transportConfiguration.getDefaultHttpStatusCode()).build();
}

private Header[] sanitiseHeaders(final Header[] headers) {
return Arrays.stream(headers)
.filter(it -> !it.getName().equals(AUTHORIZATION))
Expand Down Expand Up @@ -255,4 +256,4 @@ private String getRequestUrl(final String path) {
throw new CheckoutException(e);
}
}
}
}
19 changes: 14 additions & 5 deletions src/main/java/com/checkout/RequestMetrics.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
package com.checkout;

import com.google.gson.annotations.SerializedName;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
public class RequestMetrics {

@SerializedName("prev_request_id")
private String prevRequestId;

@SerializedName("request_id")
private String requestId;

@SerializedName("prev_request_duration")
private Long prevRequestDuration;
private String prevRequestId;

public RequestMetrics(Long prevRequestDuration, String prevRequestId) {
this.prevRequestDuration = prevRequestDuration;
this.prevRequestId = prevRequestId;
public String toTelemetryHeader() {
return String.format("{\"prev_request_id\":\"%s\",\"request_id\":\"%s\",\"prev_request_duration\":%d}",
prevRequestId != null ? prevRequestId : "N/A",
requestId != null ? requestId : "N/A",
prevRequestDuration != null ? prevRequestDuration : 0);
}
}
}
Loading

0 comments on commit 9ff02f7

Please sign in to comment.