From a3363e957f6b23daa98114923580eac69cdddc1e Mon Sep 17 00:00:00 2001 From: Lauri Tulmin Date: Mon, 27 May 2024 15:16:35 +0300 Subject: [PATCH] Fix request header capture corrupting tomcat request --- .../tomcat/common/TomcatHttpAttributesGetter.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/instrumentation/tomcat/tomcat-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/tomcat/common/TomcatHttpAttributesGetter.java b/instrumentation/tomcat/tomcat-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/tomcat/common/TomcatHttpAttributesGetter.java index 96c69ec2e5f0..b8031b31b671 100644 --- a/instrumentation/tomcat/tomcat-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/tomcat/common/TomcatHttpAttributesGetter.java +++ b/instrumentation/tomcat/tomcat-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/tomcat/common/TomcatHttpAttributesGetter.java @@ -8,6 +8,7 @@ import static io.opentelemetry.javaagent.instrumentation.tomcat.common.TomcatHelper.messageBytesToString; import io.opentelemetry.instrumentation.api.semconv.http.HttpServerAttributesGetter; +import java.util.ArrayList; import java.util.Collections; import java.util.List; import javax.annotation.Nullable; @@ -15,6 +16,7 @@ import org.apache.coyote.Request; import org.apache.coyote.Response; import org.apache.tomcat.util.buf.MessageBytes; +import org.apache.tomcat.util.http.MimeHeaders; public class TomcatHttpAttributesGetter implements HttpServerAttributesGetter { @@ -44,7 +46,17 @@ public String getUrlQuery(Request request) { @Override public List getHttpRequestHeader(Request request, String name) { - return Collections.list(request.getMimeHeaders().values(name)); + List result = null; + MimeHeaders headers = request.getMimeHeaders(); + int i = headers.findHeader(name, 0); + while (i != -1) { + if (result == null) { + result = new ArrayList<>(); + } + result.add(messageBytesToString(headers.getValue(i))); + i = headers.findHeader(name, i + 1); + } + return result != null ? result : Collections.emptyList(); } @Override