diff --git a/instrumentation/servlet/servlet-3.0/javaagent-unit-tests/src/test/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/snippet/InjectionTest.java b/instrumentation/servlet/servlet-3.0/javaagent-unit-tests/src/test/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/snippet/InjectionTest.java index 1a7682f934ab..2472deee295d 100644 --- a/instrumentation/servlet/servlet-3.0/javaagent-unit-tests/src/test/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/snippet/InjectionTest.java +++ b/instrumentation/servlet/servlet-3.0/javaagent-unit-tests/src/test/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/snippet/InjectionTest.java @@ -7,8 +7,10 @@ import static io.opentelemetry.javaagent.instrumentation.servlet.v3_0.snippet.TestUtil.readFile; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; -import io.opentelemetry.javaagent.bootstrap.servlet.SnippetHolder; +import io.opentelemetry.javaagent.bootstrap.servlet.ExperimentalSnippetHolder; import java.io.IOException; import java.io.StringWriter; import java.nio.charset.StandardCharsets; @@ -21,13 +23,16 @@ class InjectionTest { @Test void testInjectionForStringContainHeadTag() throws IOException { String testSnippet = "\n "; - SnippetHolder.setSnippet(testSnippet); + ExperimentalSnippetHolder.setSnippet(testSnippet); // read the originalFile String original = readFile("staticHtmlOrigin.html"); // read the correct answer String correct = readFile("staticHtmlAfter.html"); byte[] originalBytes = original.getBytes(StandardCharsets.UTF_8); - InjectionState obj = new InjectionState(StandardCharsets.UTF_8.name()); + SnippetInjectingResponseWrapper response = mock(SnippetInjectingResponseWrapper.class); + when(response.isCommitted()).thenReturn(false); + when(response.getCharacterEncoding()).thenReturn(StandardCharsets.UTF_8.name()); + InjectionState obj = new InjectionState(response); StringWriter writer = new StringWriter(); @@ -54,13 +59,16 @@ public void write(int b) throws IOException { @Disabled void testInjectionForChinese() throws IOException { String testSnippet = "\n "; - SnippetHolder.setSnippet(testSnippet); + ExperimentalSnippetHolder.setSnippet(testSnippet); // read the originalFile String original = readFile("staticHtmlChineseOrigin.html"); // read the correct answer String correct = readFile("staticHtmlChineseAfter.html"); byte[] originalBytes = original.getBytes(StandardCharsets.UTF_8); - InjectionState obj = new InjectionState(StandardCharsets.UTF_8.name()); + SnippetInjectingResponseWrapper response = mock(SnippetInjectingResponseWrapper.class); + when(response.isCommitted()).thenReturn(false); + when(response.getCharacterEncoding()).thenReturn(StandardCharsets.UTF_8.name()); + InjectionState obj = new InjectionState(response); StringWriter writer = new StringWriter(); @@ -86,12 +94,15 @@ public void write(int b) throws IOException { @Test void testInjectionForStringWithoutHeadTag() throws IOException { String testSnippet = "\n "; - SnippetHolder.setSnippet(testSnippet); + ExperimentalSnippetHolder.setSnippet(testSnippet); // read the originalFile String original = readFile("htmlWithoutHeadTag.html"); byte[] originalBytes = original.getBytes(StandardCharsets.UTF_8); - InjectionState obj = new InjectionState(StandardCharsets.UTF_8.name()); + SnippetInjectingResponseWrapper response = mock(SnippetInjectingResponseWrapper.class); + when(response.isCommitted()).thenReturn(false); + when(response.getCharacterEncoding()).thenReturn(StandardCharsets.UTF_8.name()); + InjectionState obj = new InjectionState(response); StringWriter writer = new StringWriter(); ServletOutputStream sp = @@ -115,11 +126,14 @@ public void write(int b) throws IOException { @Test void testHalfHeadTag() throws IOException { String testSnippet = "\n "; - SnippetHolder.setSnippet(testSnippet); + ExperimentalSnippetHolder.setSnippet(testSnippet); // read the original string String originalFirstPart = "\n" + "\n" + " Test "); + ExperimentalSnippetHolder.setSnippet("\n "); SnippetInjectingResponseWrapper responseWrapper = new SnippetInjectingResponseWrapper(response); responseWrapper.getWriter().write(original); responseWrapper.getWriter().flush(); @@ -58,7 +58,7 @@ void testInjectToChineseTextHtml() throws IOException { StringWriter writer = new StringWriter(); when(response.getWriter()).thenReturn(new PrintWriter(writer)); - SnippetHolder.setSnippet("\n "); + ExperimentalSnippetHolder.setSnippet("\n "); SnippetInjectingResponseWrapper responseWrapper = new SnippetInjectingResponseWrapper(response); responseWrapper.getWriter().write(original); responseWrapper.getWriter().flush(); @@ -84,7 +84,7 @@ void shouldNotInjectToTextHtml() throws IOException { when(response.containsHeader("content-type")).thenReturn(true); when(response.getWriter()).thenReturn(new PrintWriter(writer, true)); - SnippetHolder.setSnippet("\n "); + ExperimentalSnippetHolder.setSnippet("\n "); SnippetInjectingResponseWrapper responseWrapper = new SnippetInjectingResponseWrapper(response); responseWrapper.getWriter().write(original); @@ -110,7 +110,7 @@ void testWriteInt() throws IOException { StringWriter writer = new StringWriter(); // StringWriter correctWriter = new StringWriter(); when(response.getWriter()).thenReturn(new PrintWriter(writer)); - SnippetHolder.setSnippet("\n "); + ExperimentalSnippetHolder.setSnippet("\n "); SnippetInjectingResponseWrapper responseWrapper = new SnippetInjectingResponseWrapper(response); byte[] originalBytes = original.getBytes(Charset.defaultCharset().name()); // byte[] correctBytes = correct.getBytes(UTF_8); @@ -142,7 +142,7 @@ void testWriteCharArray() throws IOException { StringWriter writer = new StringWriter(); when(response.getWriter()).thenReturn(new PrintWriter(writer)); - SnippetHolder.setSnippet("\n "); + ExperimentalSnippetHolder.setSnippet("\n "); SnippetInjectingResponseWrapper responseWrapper = new SnippetInjectingResponseWrapper(response); char[] originalChars = original.toCharArray(); responseWrapper.getWriter().write(originalChars, 0, originalChars.length); @@ -171,7 +171,7 @@ void testWriteWithOffset() throws IOException { StringWriter writer = new StringWriter(); when(response.getWriter()).thenReturn(new PrintWriter(writer)); - SnippetHolder.setSnippet("\n "); + ExperimentalSnippetHolder.setSnippet("\n "); SnippetInjectingResponseWrapper responseWrapper = new SnippetInjectingResponseWrapper(response); responseWrapper diff --git a/instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/Servlet3Advice.java b/instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/Servlet3Advice.java index b3f7dc4156bc..d625789f5595 100644 --- a/instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/Servlet3Advice.java +++ b/instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/Servlet3Advice.java @@ -13,8 +13,8 @@ import io.opentelemetry.javaagent.bootstrap.CallDepth; import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge; import io.opentelemetry.javaagent.bootstrap.servlet.AppServerBridge; +import io.opentelemetry.javaagent.bootstrap.servlet.ExperimentalSnippetHolder; import io.opentelemetry.javaagent.bootstrap.servlet.MappingResolver; -import io.opentelemetry.javaagent.bootstrap.servlet.SnippetHolder; import io.opentelemetry.javaagent.instrumentation.servlet.ServletRequestContext; import io.opentelemetry.javaagent.instrumentation.servlet.v3_0.snippet.SnippetInjectingResponseWrapper; import javax.servlet.Servlet; @@ -43,7 +43,7 @@ public static void onEnter( } HttpServletRequest httpServletRequest = (HttpServletRequest) request; - if (!SnippetHolder.getSnippet().isEmpty() + if (!ExperimentalSnippetHolder.getSnippet().isEmpty() && !((HttpServletResponse) response).containsHeader(FAKE_SNIPPET_HEADER)) { response = new SnippetInjectingResponseWrapper((HttpServletResponse) response); } diff --git a/instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/snippet/InjectionState.java b/instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/snippet/InjectionState.java index 94e2dbcf7d73..d5dbe07830ab 100644 --- a/instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/snippet/InjectionState.java +++ b/instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/snippet/InjectionState.java @@ -5,24 +5,14 @@ package io.opentelemetry.javaagent.instrumentation.servlet.v3_0.snippet; -import javax.annotation.Nullable; - public class InjectionState { - private static final int ALREADY_INJECTED_FAKE_VALUE = -1; + private static final int HEAD_TAG_WRITTEN_FAKE_VALUE = -1; private static final int HEAD_TAG_LENGTH = "".length(); - - private final String characterEncoding; - @Nullable private final SnippetInjectingResponseWrapper wrapper; + private final SnippetInjectingResponseWrapper wrapper; private int headTagBytesSeen = 0; public InjectionState(SnippetInjectingResponseWrapper wrapper) { this.wrapper = wrapper; - this.characterEncoding = wrapper.getCharacterEncoding(); - } - - public InjectionState(String characterEncoding) { - this.characterEncoding = characterEncoding; - this.wrapper = null; } public int getHeadTagBytesSeen() { @@ -30,15 +20,15 @@ public int getHeadTagBytesSeen() { } public String getCharacterEncoding() { - return characterEncoding; + return wrapper.getCharacterEncoding(); } - public void setAlreadyInjected() { - headTagBytesSeen = ALREADY_INJECTED_FAKE_VALUE; + public void setHeadTagWritten() { + headTagBytesSeen = HEAD_TAG_WRITTEN_FAKE_VALUE; } - public boolean isAlreadyInjected() { - return headTagBytesSeen == ALREADY_INJECTED_FAKE_VALUE; + public boolean isHeadTagWritten() { + return headTagBytesSeen == HEAD_TAG_WRITTEN_FAKE_VALUE; } /** @@ -46,7 +36,7 @@ public boolean isAlreadyInjected() { * inject. Otherwise, returns false. */ public boolean processByte(int b) { - if (isAlreadyInjected()) { + if (isHeadTagWritten()) { return false; } if (inHeadTag(b)) { diff --git a/instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/snippet/ServletOutputStreamInjectionHelper.java b/instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/snippet/ServletOutputStreamInjectionHelper.java index 970873bfe996..3ec799b74e06 100644 --- a/instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/snippet/ServletOutputStreamInjectionHelper.java +++ b/instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/snippet/ServletOutputStreamInjectionHelper.java @@ -7,7 +7,7 @@ import static java.util.logging.Level.FINE; -import io.opentelemetry.javaagent.bootstrap.servlet.SnippetHolder; +import io.opentelemetry.javaagent.bootstrap.servlet.ExperimentalSnippetHolder; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.logging.Logger; @@ -25,48 +25,63 @@ public class ServletOutputStreamInjectionHelper { public static boolean handleWrite( byte[] original, int off, int length, InjectionState state, ServletOutputStream out) throws IOException { - if (state.isAlreadyInjected()) { + if (state.isHeadTagWritten()) { return false; } int i; - boolean shouldInject = false; + boolean endOfHeadTagFound = false; for (i = off; i < length && i - off < length; i++) { if (state.processByte(original[i])) { - shouldInject = true; + endOfHeadTagFound = true; break; } } - if (!shouldInject) { + if (!endOfHeadTagFound) { return false; } - state.setAlreadyInjected(); // set before write to avoid recursive loop - out.write(original, off, i + 1); + state.setHeadTagWritten(); // set before write to avoid recursive loop + if (state.getWrapper().isNotSafeToInject()) { + return false; + } + byte[] snippetBytes; try { - byte[] snippetBytes = SnippetHolder.getSnippetBytes(state.getCharacterEncoding()); - out.write(snippetBytes); + snippetBytes = ExperimentalSnippetHolder.getSnippetBytes(state.getCharacterEncoding()); } catch (UnsupportedEncodingException e) { logger.log(FINE, "UnsupportedEncodingException", e); + return false; } + // updating Content-Length before any further writing in case that writing triggers a flush + state.getWrapper().updateContentLengthIfPreviouslySet(); + out.write(original, off, i + 1); + out.write(snippetBytes); out.write(original, i + 1, length - i - 1); return true; } public static boolean handleWrite(InjectionState state, ServletOutputStream out, int b) throws IOException { - if (state.isAlreadyInjected()) { + if (state.isHeadTagWritten()) { return false; } if (!state.processByte(b)) { return false; } - state.setAlreadyInjected(); // set before write to avoid recursive loop - out.write(b); + state.setHeadTagWritten(); // set before write to avoid recursive loop + + if (state.getWrapper().isNotSafeToInject()) { + return false; + } + byte[] snippetBytes; try { - byte[] snippetBytes = SnippetHolder.getSnippetBytes(state.getCharacterEncoding()); - out.write(snippetBytes); + snippetBytes = ExperimentalSnippetHolder.getSnippetBytes(state.getCharacterEncoding()); } catch (UnsupportedEncodingException e) { logger.log(FINE, "UnsupportedEncodingException", e); + return false; } + state.getWrapper().updateContentLengthIfPreviouslySet(); + out.write(b); + + out.write(snippetBytes); return true; } } diff --git a/instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/snippet/SnippetInjectingPrintWriter.java b/instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/snippet/SnippetInjectingPrintWriter.java index ba3c7177f309..88e3a02a21b0 100644 --- a/instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/snippet/SnippetInjectingPrintWriter.java +++ b/instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/snippet/SnippetInjectingPrintWriter.java @@ -11,9 +11,10 @@ public class SnippetInjectingPrintWriter extends PrintWriter { private final String snippet; private final InjectionState state; - public SnippetInjectingPrintWriter(PrintWriter writer, String snippet, String characterEncoding) { + public SnippetInjectingPrintWriter( + PrintWriter writer, String snippet, SnippetInjectingResponseWrapper wrapper) { super(writer); - state = new InjectionState(characterEncoding); + state = new InjectionState(wrapper); this.snippet = snippet; } @@ -26,14 +27,20 @@ public void write(String s, int off, int len) { @Override public void write(int b) { - boolean shouldInject = state.processByte(b); super.write(b); - if (shouldInject) { - // set before write to avoid recursive loop since super.write(String) may delegate back to - // write(int) - state.setAlreadyInjected(); - super.write(snippet); + if (state.isHeadTagWritten()) { + return; } + boolean endOfHeadTagFound = state.processByte(b); + if (!endOfHeadTagFound) { + return; + } + state.setHeadTagWritten(); // set before write to avoid recursive loop + if (state.getWrapper().isNotSafeToInject()) { + return; + } + state.getWrapper().updateContentLengthIfPreviouslySet(); + super.write(snippet); } @Override diff --git a/instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/snippet/SnippetInjectingResponseWrapper.java b/instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/snippet/SnippetInjectingResponseWrapper.java index 5e5e59e9526f..85e5a40c80a1 100644 --- a/instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/snippet/SnippetInjectingResponseWrapper.java +++ b/instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/snippet/SnippetInjectingResponseWrapper.java @@ -8,7 +8,7 @@ import static io.opentelemetry.javaagent.instrumentation.servlet.v3_0.snippet.Injection.initializeInjectionStateIfNeeded; import static java.util.logging.Level.FINE; -import io.opentelemetry.javaagent.bootstrap.servlet.SnippetHolder; +import io.opentelemetry.javaagent.bootstrap.servlet.ExperimentalSnippetHolder; import java.io.IOException; import java.io.PrintWriter; import java.lang.invoke.MethodHandle; @@ -20,13 +20,27 @@ import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponseWrapper; +/** + * Notes on Content-Length: the snippet length is only added to the content length when injection + * occurs and the content length was set previously. + * + *

If the Content-Length is set after snippet injection occurs (either for the first time or is + * set again for some reason),we intentionally do not add the snippet length, because the + * application server may be making that call at the end of a request when it sees the request has + * not been submitted, in which case it is likely using the real length of content that has been + * written, including the snippet length. + */ public class SnippetInjectingResponseWrapper extends HttpServletResponseWrapper { private static final Logger logger = Logger.getLogger(HttpServletResponseWrapper.class.getName()); public static final String FAKE_SNIPPET_HEADER = "FAKE_SNIPPET_HEADER"; - private static final String SNIPPET = SnippetHolder.getSnippet(); + private static final String SNIPPET = ExperimentalSnippetHolder.getSnippet(); private static final int SNIPPET_LENGTH = SNIPPET.length(); + + private static final int UNSET = -1; @Nullable private static final MethodHandle setContentLengthLongHandler = getMethodHandle(); + private long contentLength = UNSET; + private SnippetInjectingPrintWriter snippetInjectingPrintWriter = null; public SnippetInjectingResponseWrapper(HttpServletResponse response) { @@ -50,9 +64,10 @@ public boolean containsHeader(String name) { @Override public void setHeader(String name, String value) { - if (isContentTypeTextHtml() && "Content-Length".equalsIgnoreCase(name)) { + // checking content-type is just an optimization to avoid unnecessary parsing + if ("Content-Length".equalsIgnoreCase(name) && isContentTypeTextHtml()) { try { - value = Integer.toString(SNIPPET_LENGTH + Integer.valueOf(value)); + contentLength = Long.valueOf(value); } catch (NumberFormatException ex) { logger.log(FINE, "NumberFormatException", ex); } @@ -62,11 +77,12 @@ public void setHeader(String name, String value) { @Override public void addHeader(String name, String value) { - if (isContentTypeTextHtml() && "Content-Length".equalsIgnoreCase(name)) { + // checking content-type is just an optimization to avoid unnecessary parsing + if ("Content-Length".equalsIgnoreCase(name) && isContentTypeTextHtml()) { try { - value = Integer.toString(SNIPPET_LENGTH + Integer.valueOf(value)); + contentLength = Long.valueOf(value); } catch (NumberFormatException ex) { - logger.log(FINE, "Invalid string format", ex); + logger.log(FINE, "NumberFormatException", ex); } } super.addHeader(name, value); @@ -74,25 +90,25 @@ public void addHeader(String name, String value) { @Override public void setIntHeader(String name, int value) { - if (isContentTypeTextHtml() && "Content-Length".equalsIgnoreCase(name)) { - value += SNIPPET_LENGTH; + // checking content-type is just an optimization to avoid unnecessary parsing + if ("Content-Length".equalsIgnoreCase(name) && isContentTypeTextHtml()) { + contentLength = value; } super.setIntHeader(name, value); } @Override public void addIntHeader(String name, int value) { - if (isContentTypeTextHtml() && "Content-Length".equalsIgnoreCase(name)) { - value += SNIPPET_LENGTH; + // checking content-type is just an optimization to avoid unnecessary parsing + if ("Content-Length".equalsIgnoreCase(name) && isContentTypeTextHtml()) { + contentLength = value; } super.addIntHeader(name, value); } @Override public void setContentLength(int len) { - if (isContentTypeTextHtml()) { - len += SNIPPET_LENGTH; - } + contentLength = len; super.setContentLength(len); } @@ -111,18 +127,8 @@ private static MethodHandle getMethodHandle() { } } - public boolean isContentTypeTextHtml() { - String contentType = super.getContentType(); - if (contentType == null) { - contentType = super.getHeader("content-type"); - } - return contentType != null && contentType.startsWith("text/html"); - } - public void setContentLengthLong(long length) throws Throwable { - if (isContentTypeTextHtml()) { - length += SNIPPET_LENGTH; - } + contentLength = length; if (setContentLengthLongHandler == null) { super.setContentLength((int) length); } else { @@ -130,6 +136,14 @@ public void setContentLengthLong(long length) throws Throwable { } } + public boolean isContentTypeTextHtml() { + String contentType = super.getContentType(); + if (contentType == null) { + contentType = super.getHeader("content-type"); + } + return contentType != null && contentType.startsWith("text/html"); + } + @Override public ServletOutputStream getOutputStream() throws IOException { ServletOutputStream output = super.getOutputStream(); @@ -144,8 +158,21 @@ public PrintWriter getWriter() throws IOException { } if (snippetInjectingPrintWriter == null) { snippetInjectingPrintWriter = - new SnippetInjectingPrintWriter(super.getWriter(), SNIPPET, super.getCharacterEncoding()); + new SnippetInjectingPrintWriter(super.getWriter(), SNIPPET, this); } return snippetInjectingPrintWriter; } + + public void updateContentLengthIfPreviouslySet() { + if (contentLength != UNSET) { + setContentLength((int) contentLength + SNIPPET_LENGTH); + } + } + + public boolean isNotSafeToInject() { + // if content-length was set and response was already committed (headers sent to the client), + // then not safe to inject because the content-length header cannot be updated to account for + // the snippet length + return isCommitted() && (contentLength != UNSET); + } } diff --git a/instrumentation/servlet/servlet-3.0/javaagent/src/test/groovy/AbstractServlet3Test.groovy b/instrumentation/servlet/servlet-3.0/javaagent/src/test/groovy/AbstractServlet3Test.groovy index 68af31056742..0d48faf71c59 100644 --- a/instrumentation/servlet/servlet-3.0/javaagent/src/test/groovy/AbstractServlet3Test.groovy +++ b/instrumentation/servlet/servlet-3.0/javaagent/src/test/groovy/AbstractServlet3Test.groovy @@ -7,7 +7,7 @@ import io.opentelemetry.instrumentation.test.AgentTestTrait import io.opentelemetry.instrumentation.test.asserts.TraceAssert import io.opentelemetry.instrumentation.test.base.HttpServerTest import io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint -import io.opentelemetry.javaagent.bootstrap.servlet.SnippetHolder +import io.opentelemetry.javaagent.bootstrap.servlet.ExperimentalSnippetHolder import io.opentelemetry.testing.internal.armeria.common.AggregatedHttpRequest import javax.servlet.Servlet @@ -103,7 +103,7 @@ abstract class AbstractServlet3Test extends HttpServerTest Test ") + ExperimentalSnippetHolder.setSnippet("\n ") def request = request(HTML2, "GET") def response = client.execute(request).aggregate().join() @@ -127,7 +127,7 @@ abstract class AbstractServlet3Test extends HttpServerTest Test ") + ExperimentalSnippetHolder.setSnippet("\n ") def request = request(HTML, "GET") def response = client.execute(request).aggregate().join() diff --git a/instrumentation/servlet/servlet-3.0/javaagent/src/test/groovy/TestServlet3.groovy b/instrumentation/servlet/servlet-3.0/javaagent/src/test/groovy/TestServlet3.groovy index 13187cfd6467..83e1873f3c20 100644 --- a/instrumentation/servlet/servlet-3.0/javaagent/src/test/groovy/TestServlet3.groovy +++ b/instrumentation/servlet/servlet-3.0/javaagent/src/test/groovy/TestServlet3.groovy @@ -76,18 +76,13 @@ class TestServlet3 { case HTML: resp.contentType = "text/html" resp.status = endpoint.status - resp.setContentLength(endpoint.body.length()) + resp.setContentLengthLong(endpoint.body.length()) resp.writer.print(endpoint.body) break case HTML2: resp.contentType = "text/html" resp.status = endpoint.status - try { - resp.setContentLengthLong(endpoint.body.length()) - } catch (Exception e) { - // servlet 3.0 - resp.setContentLength(endpoint.body.length()) - } + resp.setContentLength(endpoint.body.length()) byte[] body = endpoint.body.getBytes() resp.getOutputStream().write(body, 0, body.length) break @@ -166,14 +161,13 @@ class TestServlet3 { case HTML: resp.contentType = "text/html" resp.status = endpoint.status - resp.writer.print(endpoint.body) resp.setContentLength(endpoint.body.length()) + resp.writer.print(endpoint.body) context.complete() break case HTML2: resp.contentType = "text/html" resp.status = endpoint.status - resp.setContentLengthLong(endpoint.body.length()) resp.getOutputStream().print(endpoint.body) context.complete() break diff --git a/instrumentation/servlet/servlet-common/bootstrap/src/main/java/io/opentelemetry/javaagent/bootstrap/servlet/SnippetHolder.java b/instrumentation/servlet/servlet-common/bootstrap/src/main/java/io/opentelemetry/javaagent/bootstrap/servlet/ExperimentalSnippetHolder.java similarity index 84% rename from instrumentation/servlet/servlet-common/bootstrap/src/main/java/io/opentelemetry/javaagent/bootstrap/servlet/SnippetHolder.java rename to instrumentation/servlet/servlet-common/bootstrap/src/main/java/io/opentelemetry/javaagent/bootstrap/servlet/ExperimentalSnippetHolder.java index 904a5aaba988..3f6bdbcbab6d 100644 --- a/instrumentation/servlet/servlet-common/bootstrap/src/main/java/io/opentelemetry/javaagent/bootstrap/servlet/SnippetHolder.java +++ b/instrumentation/servlet/servlet-common/bootstrap/src/main/java/io/opentelemetry/javaagent/bootstrap/servlet/ExperimentalSnippetHolder.java @@ -7,12 +7,12 @@ import java.io.UnsupportedEncodingException; -public class SnippetHolder { +public class ExperimentalSnippetHolder { private static String snippet = ""; public static void setSnippet(String snippet) { - SnippetHolder.snippet = snippet; + ExperimentalSnippetHolder.snippet = snippet; } public static String getSnippet() {