1111import io .github .easy4j .opencli .parser .OpenCliParsedFields ;
1212import io .github .easy4j .opencli .util .OpenCliStrings ;
1313import java .util .Objects ;
14+ import java .util .UUID ;
1415import kong .unirest .HttpResponse ;
1516import kong .unirest .Unirest ;
1617import 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 )
0 commit comments