Commit a97db194 authored by neelesh thakur's avatar neelesh thakur
Browse files

Merge branch 'ignores-for-logfilter' into 'master'

add ignored servlet paths for Transaction Log Filter;

See merge request !96
parents 5d6ab124 5132dd22
Pipeline #41044 passed with stages
in 6 minutes and 41 seconds
......@@ -17,6 +17,7 @@ package org.opengroup.osdu.azure.filters;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
......@@ -28,6 +29,7 @@ import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
......@@ -49,6 +51,9 @@ public final class TransactionLogFilter implements Filter {
@Autowired
private JaxRsDpsLog jaxRsDpsLog;
@Value("${logging.ignore.servlet.paths:}#{T(java.util.Collections).emptyList()}")
private List<String> ignoredServletPaths = new ArrayList<>();
/**
* Filter logic.
* @param servletRequest Request object.
......@@ -62,11 +67,16 @@ public final class TransactionLogFilter implements Filter {
final FilterChain filterChain) throws IOException, ServletException {
final HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
final HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;
logTransactionStart(httpRequest);
final long start = System.currentTimeMillis();
filterChain.doFilter(servletRequest, servletResponse);
final long timeTaken = System.currentTimeMillis() - start;
logTransactionEnd(httpRequest, httpResponse, timeTaken);
String servletPath = httpRequest.getServletPath();
if (ignoredServletPaths.stream().noneMatch(path -> servletPath.equals(path))) {
logTransactionStart(httpRequest);
final long start = System.currentTimeMillis();
filterChain.doFilter(servletRequest, servletResponse);
final long timeTaken = System.currentTimeMillis() - start;
logTransactionEnd(httpRequest, httpResponse, timeTaken);
} else {
filterChain.doFilter(servletRequest, servletResponse);
}
}
/**
......
......@@ -14,6 +14,7 @@
package org.opengroup.osdu.azure.filters;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
......@@ -28,12 +29,20 @@ import javax.servlet.FilterChain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import static org.apache.commons.lang3.reflect.FieldUtils.getDeclaredField;
import static org.apache.commons.lang3.reflect.FieldUtils.writeField;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.only;
/**
* Tests for {@link TransactionLogFilter}
......@@ -53,13 +62,9 @@ public class TransactionLogFilterTest {
@InjectMocks
private TransactionLogFilter logFilter;
@BeforeEach
public void setup() {
when(servletResponse.getStatus()).thenReturn(STATUS_CODE);
}
@Test
public void testStartAndEndMessagesAreLoggedProperly() throws Exception {
when(servletResponse.getStatus()).thenReturn(STATUS_CODE);
final String startLogMessage = "Start Web-API PUT records Headers: {correlation-id:abc}";
final String endMessage = "End Web-API PUT records Headers: {correlation-id:abc} status=200 time=";
when(servletRequest.getMethod()).thenReturn("PUT");
......@@ -70,7 +75,7 @@ public class TransactionLogFilterTest {
doNothing().when(jaxRsDpsLog).info(eq("TxnLogger"), logMessageCaptor.capture());
this.logFilter.doFilter(servletRequest, servletResponse, filterChain);
verify(servletRequest, times(2)).getMethod();
verify(servletRequest, times(2)).getServletPath();
verify(servletRequest, times(3)).getServletPath();
verify(servletRequest, times(2)).getHeader(eq(DpsHeaders.CORRELATION_ID));
verify(servletResponse, times(2)).getHeader(eq(DpsHeaders.CORRELATION_ID));
verify(servletResponse, times(1)).getStatus();
......@@ -81,6 +86,7 @@ public class TransactionLogFilterTest {
@Test
public void testStartAndEndMessagesAreLoggedProperlyWithNoHeaders() throws Exception {
when(servletResponse.getStatus()).thenReturn(STATUS_CODE);
final String startLogMessage = "Start Web-API PUT records Headers: {}";
final String endMessage = "End Web-API PUT records Headers: {} status=200 time=";
when(servletRequest.getMethod()).thenReturn("PUT");
......@@ -89,10 +95,36 @@ public class TransactionLogFilterTest {
doNothing().when(jaxRsDpsLog).info(eq("TxnLogger"), logMessageCaptor.capture());
this.logFilter.doFilter(servletRequest, servletResponse, filterChain);
verify(servletRequest, times(2)).getMethod();
verify(servletRequest, times(2)).getServletPath();
verify(servletRequest, times(3)).getServletPath();
verify(servletResponse, times(1)).getStatus();
assertEquals(2, logMessageCaptor.getAllValues().size());
assertEquals(startLogMessage, logMessageCaptor.getAllValues().get(0));
assertEquals(true, logMessageCaptor.getAllValues().get(1).startsWith(endMessage));
}
@Test
public void testNoLoggingWhenIgnoredServletPathProvided() throws Exception {
final String ignoredServletPath = "/actuator/health";
initIgnoredServletPaths(ignoredServletPath);
when(servletRequest.getServletPath()).thenReturn(ignoredServletPath);
final ArgumentCaptor<String> logMessageCaptor = ArgumentCaptor.forClass(String.class);
this.logFilter.doFilter(servletRequest, servletResponse, filterChain);
verify(servletRequest, never()).getMethod();
verify(servletRequest, only()).getServletPath();
verify(servletRequest, never()).getHeader(eq(DpsHeaders.CORRELATION_ID));
verify(servletResponse, never()).getHeader(eq(DpsHeaders.CORRELATION_ID));
verify(servletResponse, never()).getStatus();
assertEquals(0, logMessageCaptor.getAllValues().size());
}
private void initIgnoredServletPaths(String ignoredServletPath) {
Field field = getDeclaredField(TransactionLogFilter.class, "ignoredServletPaths", true);
field.setAccessible(true);
try {
List<String> ignoredServletPaths = (List<String>) field.get(logFilter);
ignoredServletPaths.add(ignoredServletPath);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment