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

Some corrections and performance improvements in CorrelationIdFilter #183

Merged
Merged
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 @@ -2,10 +2,8 @@

import static com.sap.hcp.cf.logging.common.customfields.CustomField.customField;
import static com.sap.hcp.cf.logging.common.request.HttpHeaders.W3C_TRACEPARENT;
import static java.util.Optional.ofNullable;

import java.util.UUID;
import java.util.function.Predicate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Expand All @@ -27,8 +25,8 @@
public class CorrelationIdFilter extends AbstractLoggingFilter {

private static final Logger LOG = LoggerFactory.getLogger(CorrelationIdFilter.class);
private HttpHeader correlationHeader;
private HttpHeader traceparentHeader;
private final HttpHeader correlationHeader;
private final HttpHeader traceparentHeader;

public CorrelationIdFilter() {
this(HttpHeaders.CORRELATION_ID);
Expand Down Expand Up @@ -65,23 +63,26 @@ private String determineCorrelationId(HttpServletRequest request) {
return correlationId;
}

private boolean isBlankOrDefault(String value) {
private static boolean isBlankOrDefault(String value) {
return value == null || value.isEmpty() || value.equals(Defaults.UNKNOWN);
}

private String getCorrelationIdFromTraceparent(HttpServletRequest request) {
String traceparent = HttpHeaderUtilities.getHeaderValue(request, traceparentHeader);
return ofNullable(traceparent).filter(not(this::isBlankOrDefault)).map(this::parseTraceparent).orElse(
null);
return isBlankOrDefault(traceparent) ? null : parseTraceparent(traceparent);
}

private <T> Predicate<T> not(Predicate<T> p) {
return p.negate();
}

private String parseTraceparent(String value) {
String[] tokens = value.split("-");
return tokens.length >= 2 ? tokens[1] : null;
private static String parseTraceparent(String value) {
int idx1 = value.indexOf('-');
if (idx1 != -1) {
++idx1;
int idx2 = value.indexOf('-', idx1);
if (idx2 != -1) {
// return string between first and second '-'.
return value.substring(idx1, idx2);
}
}
return null;
KarstenSchnitter marked this conversation as resolved.
Show resolved Hide resolved
}

private void addCorrelationIdHeader(HttpServletResponse response, String correlationId) {
Expand Down