Skip to content

Commit

Permalink
response status
Browse files Browse the repository at this point in the history
  • Loading branch information
michalsomora committed Nov 14, 2023
1 parent ce4d17a commit f77bcdd
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 14 deletions.
10 changes: 8 additions & 2 deletions extensions/log/rs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,18 @@ tkit.log.rs.start.template=%1$s %2$s started.
tkit.log.rs.end.enabled=true
# End log message template (%1 - HTTP method, %2 - Path, %3 - Duration, %4 - HTTP response code, %5 - HTTP response name, %6 - URI)
tkit.log.rs.end.template=%1$s %2$s [%4$s] [%3$ss]
# Enable duration time as MDC parameter
tkit.log.rs.end.mdc.duration.enabled=true
# Enable MDC parameters in rest calls
tkit.log.rs.end.mdc.enabled=true
# MDC duration name
tkit.log.rs.end.mdc.duration.name=rs-time
# Response status MDC key name
tkit.log.rs.end.mdc.response-status.name=rs-status
# Enable MDC parameters in rest client calls
tkit.log.rs.client.end.mdc.enabled=true
# MDC duration name
tkit.log.rs.client.end.mdc.duration.name=rs-client-time
# Response client status MDC key name
tkit.log.rs.client.end.mdc.response-status.name=rs-client-status
# Enable or disable error log message
tkit.log.rs.error.enabled=true
# Enable or disable error log message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ public void mdcTest() {

assertLogs().assertLines(1)
.assertMatches(0,
".+INFO \\{X-Correlation-ID=.+, rs-time=.+\\} \\[org\\.tki\\.qua\\.log\\.rs\\.tes\\.app\\.NoAnnotationRestController\\] \\(executor-thread-0\\) GET \\/no-anno\\/test1 \\[200\\] \\[.+s\\]");
".+INFO \\{X-Correlation-ID=.+, rs-status=200, rs-time=.+\\} \\[org\\.tki\\.qua\\.log\\.rs\\.tes\\.app\\.NoAnnotationRestController\\] \\(executor-thread-0\\) GET \\/no-anno\\/test1 \\[200\\] \\[.+s\\]");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,12 @@ public void filter(ClientRequestContext requestContext, ClientResponseContext re
Response.StatusType status = responseContext.getStatusInfo();
context.close();

if (config.client.end.mdc.durationEnabled) {
if (config.client.end.mdc.enabled) {
MDC.put(config.client.end.mdc.durationName, context.time);
context.mdcKeys.add(config.client.end.mdc.durationName);

MDC.put(config.client.end.mdc.responseStatusName, status.getStatusCode());
context.mdcKeys.add(config.end.mdc.responseStatusName);
}

if (config.client.end.enabled) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ public class RestInterceptorContext {

String time;

Long duration;

boolean exclude = false;

Set<String> mdcKeys = new HashSet<>();
Expand All @@ -28,7 +26,6 @@ public class RestInterceptorContext {
}

public void close() {
duration = (System.currentTimeMillis() - startTime);
time = String.format("%.3f", duration / 1000f);
time = String.format("%.3f", (System.currentTimeMillis() - startTime) / 1000f);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,16 @@ public void filter(ContainerRequestContext requestContext, ContainerResponseCont
}
if (config.end.enabled && log) {

if (config.end.mdc.durationEnabled) {
Response.StatusType status = responseContext.getStatusInfo();

if (config.end.mdc.enabled) {
MDC.put(config.end.mdc.durationName, restContext.time);
restContext.mdcKeys.add(config.end.mdc.durationName);

MDC.put(config.end.mdc.responseStatusName, status.getStatusCode());
restContext.mdcKeys.add(config.end.mdc.responseStatusName);
}
Response.StatusType status = responseContext.getStatusInfo();

LoggerFactory.getLogger(resourceInfo.getResourceClass())
.info(String.format(config.end.template, restContext.method, restContext.path,
restContext.time, status.getStatusCode(), status.getReasonPhrase(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,20 @@ public static class RestClientMdcLogConfig {
/**
* Enable duration time as MDC parameter
*/
@ConfigItem(name = "duration.enabled", defaultValue = "true")
public boolean durationEnabled;
@ConfigItem(name = "enabled", defaultValue = "true")
public boolean enabled;

/**
* Duration MDC key name
*/
@ConfigItem(name = "duration.name", defaultValue = "rs-client-time")
public String durationName;

/**
* Response client status MDC key name
*/
@ConfigItem(name = "response-status.name", defaultValue = "rs-client-status")
public String responseStatusName;
}

@ConfigGroup
Expand All @@ -236,14 +242,20 @@ public static class RestMdcLogConfig {
/**
* Enable duration time as MDC parameter
*/
@ConfigItem(name = "duration.enabled", defaultValue = "true")
public boolean durationEnabled;
@ConfigItem(name = "enabled", defaultValue = "true")
public boolean enabled;

/**
* Duration MDC key name
*/
@ConfigItem(name = "duration.name", defaultValue = "rs-time")
public String durationName;

/**
* Response status MDC key name
*/
@ConfigItem(name = "response-status.name", defaultValue = "rs-status")
public String responseStatusName;
}

/**
Expand Down

0 comments on commit f77bcdd

Please sign in to comment.