Skip to content

Commit

Permalink
adding duration filed for successful and unsuccessful service calls i…
Browse files Browse the repository at this point in the history
…n rest template implementation (#5)

[feature] adding duration filed for successful and unsuccessful service calls in rest template implementation
  • Loading branch information
ebrahimiar authored Jul 1, 2024
1 parent 84fb98a commit 4928f8a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
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

0 comments on commit 4928f8a

Please sign in to comment.