Skip to content

Commit 473fa95

Browse files
committed
udpate
1 parent 0f03948 commit 473fa95

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/snippet/ServletOutputStreamInjectionHelper.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,10 @@ public static boolean handleWrite(
4141
}
4242
state.setAlreadyInjected(); // set before write to avoid recursive loop
4343
out.write(original, off, i + 1);
44+
if (state.getWrapper().isNotSafeToInject()) {
45+
return false;
46+
}
4447
try {
45-
if (state.getWrapper().isNotSafeToInject()) {
46-
// content length already set and sent, don't inject
47-
return false;
48-
}
4948
byte[] snippetBytes = ExperimentalSnippetHolder.getSnippetBytes(state.getCharacterEncoding());
5049
state.getWrapper().updateContentLengthIfPreviouslySet();
5150
out.write(snippetBytes);
@@ -66,11 +65,10 @@ public static boolean handleWrite(InjectionState state, ServletOutputStream out,
6665
}
6766
state.setAlreadyInjected(); // set before write to avoid recursive loop
6867
out.write(b);
68+
if (state.getWrapper().isNotSafeToInject()) {
69+
return false;
70+
}
6971
try {
70-
if (state.getWrapper().isNotSafeToInject()) {
71-
// content length already set and sent, don't inject
72-
return false;
73-
}
7472
byte[] snippetBytes = ExperimentalSnippetHolder.getSnippetBytes(state.getCharacterEncoding());
7573
state.getWrapper().updateContentLengthIfPreviouslySet();
7674
out.write(snippetBytes);

instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/snippet/SnippetInjectingPrintWriter.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public void write(int b) {
3434
// write(int)
3535
state.setAlreadyInjected();
3636
if (state.getWrapper().isNotSafeToInject()) {
37-
// content length already set and sent, don't inject
3837
return;
3938
}
4039
state.getWrapper().updateContentLengthIfPreviouslySet();

instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/snippet/SnippetInjectingResponseWrapper.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,25 @@
2121
import javax.servlet.http.HttpServletResponseWrapper;
2222

2323
/**
24-
* Notes with contentLength: 1. Q: what is the timing that we would update the content length? 1.
25-
* the injection happened 2. the content length was already set outside 2. Q: Why we would not add
26-
* the content length after the injection if the content length was not set yet? A: The server may
27-
* monitor the bytes that have written, and use the new length itself
24+
* Notes on Content-Length: the snippet length is only added to the content length when injection
25+
* occurs and the content length was set previously.
26+
*
27+
* <p>If the Content-Length is set after snippet injection occurs (either for the first time or is
28+
* set again for some reason),we intentionally do not add the snippet length, because the
29+
* application server may be making that call at the end of a request when it sees the request has
30+
* not been submitted, in which case it is likely using the real length of content that has been
31+
* written, including the snippet length.
2832
*/
2933
public class SnippetInjectingResponseWrapper extends HttpServletResponseWrapper {
3034
private static final Logger logger = Logger.getLogger(HttpServletResponseWrapper.class.getName());
3135
public static final String FAKE_SNIPPET_HEADER = "FAKE_SNIPPET_HEADER";
3236
private static final String SNIPPET = ExperimentalSnippetHolder.getSnippet();
3337
private static final int SNIPPET_LENGTH = SNIPPET.length();
3438

35-
private static final int NOT_SET = -1;
39+
private static final int UNSET = -1;
3640
@Nullable private static final MethodHandle setContentLengthLongHandler = getMethodHandle();
3741

38-
private long contentLength = NOT_SET;
42+
private long contentLength = UNSET;
3943

4044
private SnippetInjectingPrintWriter snippetInjectingPrintWriter = null;
4145

@@ -60,7 +64,6 @@ public boolean containsHeader(String name) {
6064

6165
@Override
6266
public void setHeader(String name, String value) {
63-
// checking content-type is just an optimization to avoid unnecessary parsing
6467
if ("Content-Length".equalsIgnoreCase(name) && isContentTypeTextHtml()) {
6568
try {
6669
contentLength = Long.valueOf(value);
@@ -157,12 +160,12 @@ public PrintWriter getWriter() throws IOException {
157160
}
158161

159162
public void updateContentLengthIfPreviouslySet() {
160-
if (contentLength != NOT_SET) {
163+
if (contentLength != UNSET) {
161164
setContentLength((int) contentLength + SNIPPET_LENGTH);
162165
}
163166
}
164167

165168
public boolean isNotSafeToInject() {
166-
return isCommitted() && (contentLength != NOT_SET);
169+
return isCommitted() && (contentLength != UNSET);
167170
}
168171
}

0 commit comments

Comments
 (0)