Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding duration filed for successful and unsuccessful service calls in rest template implementation #5

Merged
merged 3 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,21 @@ public ClientHttpResponse intercept(HttpRequest request, byte[] requestBody, Cli
log.info(httpLoggingInterceptorUtil.getRequestDetailContent(request, requestBody, webServiceName));
}
ClientHttpResponse response;
long startTime = System.currentTimeMillis();
try {
response = ex.execute(request, requestBody);
if (log.isInfoEnabled()) {
HttpResponseWrapper responseWrapper = new HttpResponseWrapper(response);
log.info(httpLoggingInterceptorUtil.getResponseDetailContent(responseWrapper, webServiceName));
log.info(httpLoggingInterceptorUtil.getResponseDetailContent(responseWrapper, webServiceName,
System.currentTimeMillis() - startTime));
return responseWrapper;
} else {
return response;
}
} catch (IOException e) {
if (log.isInfoEnabled()) {
log.info(httpLoggingInterceptorUtil.getExceptionDetailContent(e, webServiceName));
log.info(httpLoggingInterceptorUtil.getExceptionDetailContent(e, webServiceName,
System.currentTimeMillis() - startTime));
}
throw new HttpClientRequestExecuteException(e.getMessage(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ public String getRequestDetailContent(HttpRequest request, byte[] body, String w
return toJson(requestData);
}

public String getResponseDetailContent(ClientHttpResponse response, String webServiceName) throws IOException {
public String getResponseDetailContent(ClientHttpResponse response, String webServiceName, long elapsedTime) throws IOException {
final Map<String, Object> responseData = new LinkedHashMap<>();
responseData.put("invoked", webServiceName);
responseData.put("duration", elapsedTime / 1000.0 + "s");
responseData.put("status", response.getStatusCode());
if (!response.getHeaders().isEmpty()) {
responseData.put("headers", getMaskedHeaders(response.getHeaders()));
Expand All @@ -80,9 +81,10 @@ public String getResponseDetailContent(ClientHttpResponse response, String webSe
return toJson(responseData);
}

public String getExceptionDetailContent(Exception exception, String webServiceName) {
public String getExceptionDetailContent(Exception exception, String webServiceName, long elapsedTime) {
final Map<String, Object> exceptionData = new LinkedHashMap<>();
exceptionData.put("invoked", webServiceName);
exceptionData.put("duration", elapsedTime / 1000.0 + "s");
exceptionData.put("exception", exception.getClass().getSimpleName());
exceptionData.put("message", exception.getMessage());
return toJson(exceptionData);
Expand Down
Loading