diff --git a/cf-java-logging-support-servlet/src/main/java/com/sap/hcp/cf/logging/servlet/filter/CorrelationIdFilter.java b/cf-java-logging-support-servlet/src/main/java/com/sap/hcp/cf/logging/servlet/filter/CorrelationIdFilter.java index a7772514..4853be18 100644 --- a/cf-java-logging-support-servlet/src/main/java/com/sap/hcp/cf/logging/servlet/filter/CorrelationIdFilter.java +++ b/cf-java-logging-support-servlet/src/main/java/com/sap/hcp/cf/logging/servlet/filter/CorrelationIdFilter.java @@ -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; @@ -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); @@ -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 Predicate not(Predicate 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; } private void addCorrelationIdHeader(HttpServletResponse response, String correlationId) {