Skip to content

Commit 1485788

Browse files
committed
fix: correlate payload-free remote HTTP diagnostics
1 parent 6e23120 commit 1485788

2 files changed

Lines changed: 30 additions & 13 deletions

File tree

‎src/main/java/io/github/easy4j/opencli/remote/OpenCliRemoteAgentHttpClient.java‎

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import io.github.easy4j.opencli.parser.OpenCliParsedFields;
1212
import io.github.easy4j.opencli.util.OpenCliStrings;
1313
import java.util.Objects;
14+
import java.util.UUID;
1415
import kong.unirest.HttpResponse;
1516
import kong.unirest.Unirest;
1617
import kong.unirest.UnirestException;
@@ -68,11 +69,13 @@ public OpenCliResult collect(OpenCliCollectRequest request) {
6869
}
6970
String url = base.trim().replaceAll("/+$", "") + "/collect";
7071
int timeout = resolveTimeoutMillis();
72+
String correlationId = UUID.randomUUID().toString();
7173
String bodyJson;
7274
try {
7375
bodyJson = MAPPER.writeValueAsString(request);
7476
} catch (com.fasterxml.jackson.core.JsonProcessingException e) {
75-
throw new OpenCliExecutableFailureException("Failed to serialize collect request", null);
77+
log.warn("Agent request serialization failed correlationId={}", correlationId);
78+
throw failure("Failed to serialize collect request", correlationId);
7679
}
7780
try {
7881
HttpResponse<String> response =
@@ -86,18 +89,19 @@ public OpenCliResult collect(OpenCliCollectRequest request) {
8689
int status = response.getStatus();
8790
String respBody = Objects.isNull(response.getBody()) ? "" : response.getBody();
8891
if (status < 200 || status >= 300) {
89-
log.warn("Agent HTTP non-2xx status={} responseChars={}", status, respBody.length());
90-
throw new OpenCliExecutableFailureException("Agent HTTP " + status, null);
92+
log.warn("Agent HTTP non-2xx correlationId={} status={} responseChars={}",
93+
correlationId, status, respBody.length());
94+
throw failure("Agent HTTP " + status, correlationId);
9195
}
92-
return mapResponse(respBody);
96+
return mapResponse(respBody, correlationId);
9397
} catch (OpenCliNonZeroExitException e) {
9498
throw e;
9599
} catch (com.fasterxml.jackson.core.JsonProcessingException e) {
96-
log.warn("Agent response parse failed");
97-
throw new OpenCliExecutableFailureException("Failed to parse agent response", null);
100+
log.warn("Agent response parse failed correlationId={}", correlationId);
101+
throw failure("Failed to parse agent response", correlationId);
98102
} catch (UnirestException e) {
99-
log.warn("Agent HTTP I/O failed");
100-
throw new OpenCliExecutableFailureException("Agent HTTP I/O error", null);
103+
log.warn("Agent HTTP I/O failed correlationId={}", correlationId);
104+
throw failure("Agent HTTP I/O error", correlationId);
101105
}
102106
}
103107

@@ -147,15 +151,20 @@ private int resolveTimeoutMillis() {
147151
return (int) Math.min(timeoutMs, Integer.MAX_VALUE);
148152
}
149153

150-
private OpenCliResult mapResponse(String respBody)
154+
private static OpenCliExecutableFailureException failure(String message, String correlationId) {
155+
return new OpenCliExecutableFailureException(message + " correlationId=" + correlationId, null);
156+
}
157+
158+
private OpenCliResult mapResponse(String respBody, String correlationId)
151159
throws com.fasterxml.jackson.core.JsonProcessingException {
152160
String rawCapture = captureRawIfEnabled(respBody);
153161
AgentCollectEnvelope env;
154162
try {
155163
env = MAPPER.readValue(respBody, AgentCollectEnvelope.class);
156164
} catch (com.fasterxml.jackson.core.JsonProcessingException e) {
157-
log.warn("Agent response envelope parse failed responseChars={}", respBody.length());
158-
throw new OpenCliExecutableFailureException("Failed to parse agent response envelope", null);
165+
log.warn("Agent response envelope parse failed correlationId={} responseChars={}",
166+
correlationId, respBody.length());
167+
throw failure("Failed to parse agent response envelope", correlationId);
159168
}
160169
boolean success = Objects.nonNull(env.success) && env.success;
161170
String err = Objects.isNull(env.error) ? "" : env.error;
@@ -170,7 +179,8 @@ private OpenCliResult mapResponse(String respBody)
170179
OpenCliParsedFields parsed = OpenCliOutputParser.parseBestEffort(stdout, err);
171180
if (!success) {
172181
log.warn(
173-
"Agent collect reported failure errorChars={} stdoutLength={}",
182+
"Agent collect reported failure correlationId={} errorChars={} stdoutLength={}",
183+
correlationId,
174184
err.length(),
175185
stdout.length());
176186
OpenCliResult failed =
@@ -182,7 +192,7 @@ private OpenCliResult mapResponse(String respBody)
182192
.parsed(parsed)
183193
.remoteRawHttpBody(rawCapture)
184194
.build();
185-
throw new OpenCliNonZeroExitException("OpenCLI agent reported failure", failed);
195+
throw new OpenCliNonZeroExitException("OpenCLI agent reported failure correlationId=" + correlationId, failed);
186196
}
187197
return OpenCliResult.builder()
188198
.stdout(stdout)

‎src/test/java/io/github/easy4j/opencli/remote/OpenCliRemoteAgentHttpClientTest.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import java.net.InetSocketAddress;
1717
import java.nio.charset.StandardCharsets;
1818
import java.util.UUID;
19+
import java.util.regex.Matcher;
20+
import java.util.regex.Pattern;
1921
import org.junit.jupiter.api.Assertions;
2022
import org.junit.jupiter.api.Test;
2123

@@ -110,6 +112,11 @@ void nonSuccessHttpResponseDoesNotExposeBodyCanaryInException() throws IOExcepti
110112
Assertions.assertFalse(ex.toString().contains(urlCanary));
111113
String logText = diagnostics.toString(StandardCharsets.UTF_8.name());
112114
Assertions.assertTrue(logText.contains("Agent HTTP non-2xx"));
115+
Matcher correlation = Pattern.compile("correlationId=([0-9a-f-]{36})")
116+
.matcher(ex.getMessage());
117+
Assertions.assertTrue(correlation.find());
118+
Assertions.assertDoesNotThrow(() -> UUID.fromString(correlation.group(1)));
119+
Assertions.assertTrue(logText.contains(correlation.group(0)));
113120
Assertions.assertFalse(logText.contains(canary));
114121
Assertions.assertFalse(logText.contains(urlCanary));
115122
for (Throwable cause = ex.getCause(); cause != null; cause = cause.getCause()) {

0 commit comments

Comments
 (0)