Skip to content

Commit 2a5ba09

Browse files
committed
fix(logging) : Prevent endPointResponse showing 200 for failed request (502)
(cherry picked from commit df8c37a)
1 parent aecdede commit 2a5ba09

File tree

2 files changed

+59
-1
lines changed
  • gravitee-apim-gateway/gravitee-apim-gateway-handlers/gravitee-apim-gateway-handlers-api/src
    • main/java/io/gravitee/gateway/reactive/handlers/api/v4/analytics/logging
    • test/java/io/gravitee/gateway/reactive/handlers/api/v4/analytics/logging

2 files changed

+59
-1
lines changed

gravitee-apim-gateway/gravitee-apim-gateway-handlers/gravitee-apim-gateway-handlers-api/src/main/java/io/gravitee/gateway/reactive/handlers/api/v4/analytics/logging/LoggingHook.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,12 @@ public Completable post(String id, HttpExecutionContext ctx, @Nullable Execution
7777
}
7878

7979
if (log != null && loggingContext != null && loggingContext.endpointResponse()) {
80-
((LogEndpointResponse) log.getEndpointResponse()).capture();
80+
final ExecutionFailure executionFailure = ctx.getInternalAttribute(
81+
InternalContextAttributes.ATTR_INTERNAL_EXECUTION_FAILURE
82+
);
83+
if (executionFailure == null) {
84+
((LogEndpointResponse) log.getEndpointResponse()).capture();
85+
}
8186

8287
final HttpResponseInternal response = ((HttpExecutionContextInternal) ctx).response();
8388
response.setHeaders(((LogHeadersCaptor) response.headers()).getDelegate());

gravitee-apim-gateway/gravitee-apim-gateway-handlers/gravitee-apim-gateway-handlers-api/src/test/java/io/gravitee/gateway/reactive/handlers/api/v4/analytics/logging/LoggingHookTest.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,4 +363,57 @@ void shouldNotWrapHeadersInPreWhenNeitherEndpointRequestHeadersNorResponseEnable
363363
obs.assertComplete();
364364
verify(response, never()).setHeaders(any());
365365
}
366+
367+
@Test
368+
void shouldCaptureEndpointResponseWhenExecutionFailureIsNull() {
369+
Log log = Log.builder().timestamp(System.currentTimeMillis()).build();
370+
LogEndpointResponse logEndpointResponse = mock(LogEndpointResponse.class);
371+
log.setEndpointResponse(logEndpointResponse);
372+
373+
when(metrics.getLog()).thenReturn(log);
374+
when(loggingContext.endpointResponse()).thenReturn(true);
375+
when(response.headers()).thenReturn(new LogHeadersCaptor(HttpHeaders.create()));
376+
when(ctx.getInternalAttribute(InternalContextAttributes.ATTR_INTERNAL_EXECUTION_FAILURE)).thenReturn(null);
377+
378+
TestObserver<Void> obs = cut.post("test", ctx, ExecutionPhase.RESPONSE).test();
379+
380+
obs.assertComplete();
381+
verify(logEndpointResponse).capture();
382+
}
383+
384+
@Test
385+
void shouldNotCaptureEndpointResponseWhenExecutionFailureIsNotNull() {
386+
Log log = Log.builder().timestamp(System.currentTimeMillis()).build();
387+
LogEndpointResponse logEndpointResponse = mock(LogEndpointResponse.class);
388+
log.setEndpointResponse(logEndpointResponse);
389+
ExecutionFailure executionFailure = new ExecutionFailure(500);
390+
391+
when(metrics.getLog()).thenReturn(log);
392+
when(loggingContext.endpointResponse()).thenReturn(true);
393+
when(response.headers()).thenReturn(new LogHeadersCaptor(HttpHeaders.create()));
394+
when(ctx.getInternalAttribute(InternalContextAttributes.ATTR_INTERNAL_EXECUTION_FAILURE)).thenReturn(executionFailure);
395+
396+
TestObserver<Void> obs = cut.post("test", ctx, ExecutionPhase.RESPONSE).test();
397+
398+
obs.assertComplete();
399+
verify(logEndpointResponse, never()).capture();
400+
}
401+
402+
@Test
403+
void shouldNotCaptureEndpointResponseWhenExecutionFailureIsPresent() {
404+
Log log = Log.builder().timestamp(System.currentTimeMillis()).build();
405+
LogEndpointResponse logEndpointResponse = mock(LogEndpointResponse.class);
406+
log.setEndpointResponse(logEndpointResponse);
407+
ExecutionFailure executionFailure = new ExecutionFailure(502);
408+
409+
when(metrics.getLog()).thenReturn(log);
410+
when(loggingContext.endpointResponse()).thenReturn(true);
411+
when(response.headers()).thenReturn(new LogHeadersCaptor(HttpHeaders.create()));
412+
when(ctx.getInternalAttribute(InternalContextAttributes.ATTR_INTERNAL_EXECUTION_FAILURE)).thenReturn(executionFailure);
413+
414+
TestObserver<Void> obs = cut.post("test", ctx, ExecutionPhase.RESPONSE).test();
415+
416+
obs.assertComplete();
417+
verify(logEndpointResponse, never()).capture();
418+
}
366419
}

0 commit comments

Comments
 (0)