-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from companieshouse/lp-170-add-logging-intereptor
LP-170 Add Logging Interceptor to log all requests to the api
- Loading branch information
Showing
7 changed files
with
154 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/main/java/uk/gov/companieshouse/limitedpartnershipsapi/config/InterceptorConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package uk.gov.companieshouse.limitedpartnershipsapi.config; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.lang.NonNull; | ||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
import uk.gov.companieshouse.limitedpartnershipsapi.interceptor.LoggingInterceptor; | ||
|
||
|
||
@Configuration | ||
public class InterceptorConfig implements WebMvcConfigurer { | ||
|
||
private final LoggingInterceptor loggingInterceptor; | ||
|
||
public InterceptorConfig(LoggingInterceptor loggingInterceptor) { | ||
this.loggingInterceptor = loggingInterceptor; | ||
} | ||
|
||
/** | ||
* Set up the interceptors to run against endpoints when the endpoints are called | ||
* Interceptors are executed in the order they are added to the registry | ||
* @param registry The spring interceptor registry | ||
*/ | ||
@Override | ||
public void addInterceptors(@NonNull InterceptorRegistry registry) { | ||
registry.addInterceptor(loggingInterceptor); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...ain/java/uk/gov/companieshouse/limitedpartnershipsapi/interceptor/LoggingInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package uk.gov.companieshouse.limitedpartnershipsapi.interceptor; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.lang.NonNull; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
import org.springframework.web.servlet.HandlerMapping; | ||
import uk.gov.companieshouse.limitedpartnershipsapi.utils.ApiLogger; | ||
|
||
import static uk.gov.companieshouse.limitedpartnershipsapi.utils.Constants.ERIC_REQUEST_ID_KEY; | ||
|
||
|
||
@Component | ||
public class LoggingInterceptor implements HandlerInterceptor { | ||
|
||
private static final String START_TIME_KEY = "start-time"; | ||
|
||
@Override | ||
public boolean preHandle(HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull Object handler) { | ||
Long startTime = System.currentTimeMillis(); | ||
request.setAttribute(START_TIME_KEY, startTime); | ||
|
||
ApiLogger.infoContext(getRequestId(request), String.format("Start of request. Method: %s Path: %s", | ||
getRequestMethod(request), getRequestPath(request)), null); | ||
return true; | ||
} | ||
|
||
@Override | ||
public void afterCompletion(HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull Object handler, Exception ex) { | ||
Long startTime = (Long) request.getAttribute(START_TIME_KEY); | ||
|
||
if (startTime == null) { | ||
ApiLogger.infoContext(getRequestId(request), "Start time not found in request attributes.", null); | ||
} else { | ||
long responseTime = System.currentTimeMillis() - startTime; | ||
ApiLogger.infoContext(getRequestId(request), String.format("End of request. Method: %s Path: %s Duration: %sms Status: %s", | ||
getRequestMethod(request), getRequestPath(request), responseTime, response.getStatus()), null); | ||
} | ||
} | ||
|
||
private String getRequestPath(HttpServletRequest request) { | ||
return (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE); | ||
} | ||
|
||
private String getRequestMethod(HttpServletRequest request) { | ||
return request.getMethod(); | ||
} | ||
|
||
private String getRequestId(HttpServletRequest request) { | ||
return request.getHeader(ERIC_REQUEST_ID_KEY); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/main/java/uk/gov/companieshouse/limitedpartnershipsapi/utils/Constants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package uk.gov.companieshouse.limitedpartnershipsapi.utils; | ||
|
||
public class Constants { | ||
|
||
private Constants() { } | ||
|
||
// Request header names | ||
public static final String ERIC_REQUEST_ID_KEY = "X-Request-Id"; | ||
} |
47 changes: 47 additions & 0 deletions
47
src/test/java/uk/gov/companieshouse/limitedpartnershipsapi/config/InterceptorConfigTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package uk.gov.companieshouse.limitedpartnershipsapi.config; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InOrder; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.web.servlet.config.annotation.InterceptorRegistration; | ||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | ||
import uk.gov.companieshouse.limitedpartnershipsapi.interceptor.LoggingInterceptor; | ||
|
||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.inOrder; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class InterceptorConfigTest { | ||
|
||
@Mock | ||
private InterceptorRegistry interceptorRegistry; | ||
|
||
@Mock | ||
private InterceptorRegistration interceptorRegistration; | ||
|
||
@Mock | ||
private LoggingInterceptor loggingInterceptor; | ||
|
||
|
||
@InjectMocks | ||
private InterceptorConfig interceptorConfig; | ||
|
||
@Test | ||
void addInterceptorsTest() { | ||
when(interceptorRegistry.addInterceptor(any())).thenReturn(interceptorRegistration); | ||
|
||
interceptorConfig.addInterceptors(interceptorRegistry); | ||
|
||
InOrder inOrder = inOrder(interceptorRegistry, interceptorRegistration); | ||
inOrder.verify(interceptorRegistry).addInterceptor(loggingInterceptor); | ||
|
||
verify(interceptorRegistry, times(1)).addInterceptor(any()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters