Skip to content

Commit b86f4f7

Browse files
authored
Improve InjectingPipe outputstream and writer (#9176)
1 parent 785ce96 commit b86f4f7

File tree

7 files changed

+37
-26
lines changed

7 files changed

+37
-26
lines changed

dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/instrumentation/buffer/InjectingPipeOutputStream.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ private void drain() throws IOException {
145145
bufferFilled = false;
146146
}
147147

148+
@Override
149+
public void flush() throws IOException {
150+
downstream.flush();
151+
}
152+
148153
@Override
149154
public void close() throws IOException {
150155
if (!found) {

dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/instrumentation/buffer/InjectingPipeWriter.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import java.io.Writer;
55

66
/**
7-
* An Writer containing a circular buffer with a lookbehind buffer of n bytes. The first time that
7+
* A Writer containing a circular buffer with a lookbehind buffer of n bytes. The first time that
88
* the latest n bytes matches the marker, a content is injected before.
99
*/
1010
public class InjectingPipeWriter extends Writer {
@@ -40,24 +40,24 @@ public InjectingPipeWriter(
4040
}
4141

4242
@Override
43-
public void write(int b) throws IOException {
43+
public void write(int c) throws IOException {
4444
if (found) {
45-
downstream.write(b);
45+
downstream.write(c);
4646
return;
4747
}
4848

4949
if (bufferFilled) {
5050
downstream.write(lookbehind[pos]);
5151
}
5252

53-
lookbehind[pos] = (char) b;
53+
lookbehind[pos] = (char) c;
5454
pos = (pos + 1) % lookbehind.length;
5555

5656
if (!bufferFilled) {
5757
bufferFilled = pos == 0;
5858
}
5959

60-
if (marker[matchingPos++] == b) {
60+
if (marker[matchingPos++] == c) {
6161
if (matchingPos == marker.length) {
6262
found = true;
6363
downstream.write(contentToInject);

dd-java-agent/instrumentation/servlet/request-3/src/main/java/datadog/trace/instrumentation/servlet3/RumHttpServletResponseWrapper.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ public ServletOutputStream getOutputStream() throws IOException {
3333
outputStream =
3434
new WrappedServletOutputStream(
3535
super.getOutputStream(),
36-
rumInjector.getMarker(encoding),
37-
rumInjector.getSnippet(encoding),
36+
rumInjector.getMarkerBytes(encoding),
37+
rumInjector.getSnippetBytes(encoding),
3838
this::onInjected);
3939
}
4040
return outputStream;
@@ -50,7 +50,10 @@ public PrintWriter getWriter() throws IOException {
5050
printWriter =
5151
new PrintWriter(
5252
new InjectingPipeWriter(
53-
delegate, rumInjector.getMarker(), rumInjector.getSnippet(), this::onInjected));
53+
delegate,
54+
rumInjector.getMarkerChars(),
55+
rumInjector.getSnippetChars(),
56+
this::onInjected));
5457
}
5558
return printWriter;
5659
}

dd-java-agent/instrumentation/servlet/request-5/src/main/java/datadog/trace/instrumentation/servlet5/RumHttpServletResponseWrapper.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ public ServletOutputStream getOutputStream() throws IOException {
3333
outputStream =
3434
new WrappedServletOutputStream(
3535
super.getOutputStream(),
36-
rumInjector.getMarker(encoding),
37-
rumInjector.getSnippet(encoding),
36+
rumInjector.getMarkerBytes(encoding),
37+
rumInjector.getSnippetBytes(encoding),
3838
this::onInjected);
3939
}
4040
return outputStream;
@@ -50,7 +50,10 @@ public PrintWriter getWriter() throws IOException {
5050
printWriter =
5151
new PrintWriter(
5252
new InjectingPipeWriter(
53-
delegate, rumInjector.getMarker(), rumInjector.getSnippet(), this::onInjected));
53+
delegate,
54+
rumInjector.getMarkerChars(),
55+
rumInjector.getSnippetChars(),
56+
this::onInjected));
5457
}
5558
return printWriter;
5659
}

dd-java-agent/testing/src/main/groovy/datadog/trace/agent/test/base/HttpServerTest.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2216,7 +2216,7 @@ abstract class HttpServerTest<SERVER> extends WithHttpServer<SERVER> {
22162216
def response = client.newCall(request).execute()
22172217
then:
22182218
assert response.code() == 200
2219-
assert response.body().string().contains(new String(RumInjector.get().getSnippet("UTF-8"), "UTF-8")) == expected
2219+
assert response.body().string().contains(new String(RumInjector.get().getSnippetBytes("UTF-8"), "UTF-8")) == expected
22202220
assert response.header("x-datadog-rum-injected") == (expected ? "1" : null)
22212221
where:
22222222
mime | expected

internal-api/src/main/java/datadog/trace/api/rum/RumInjector.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public boolean isEnabled() {
7474
* @return The HTML snippet chars to inject, {@code null} if RUM injection is disabled.
7575
*/
7676
@Nullable
77-
public char[] getSnippet() {
77+
public char[] getSnippetChars() {
7878
if (!this.enabled) {
7979
return null;
8080
}
@@ -87,7 +87,7 @@ public char[] getSnippet() {
8787
* @return The HTML snippet to inject, {@code null} if RUM injection is disabled.
8888
*/
8989
@Nullable
90-
public byte[] getSnippet(String encoding) {
90+
public byte[] getSnippetBytes(String encoding) {
9191
if (!this.enabled) {
9292
return null;
9393
}
@@ -100,7 +100,7 @@ public byte[] getSnippet(String encoding) {
100100
* @return The marker chars, {@code null} if RUM injection is disabled.
101101
*/
102102
@Nullable
103-
public char[] getMarker() {
103+
public char[] getMarkerChars() {
104104
if (!this.enabled) {
105105
return null;
106106
}
@@ -114,7 +114,7 @@ public char[] getMarker() {
114114
* @return The marker bytes, {@code null} if RUM injection is disabled.
115115
*/
116116
@Nullable
117-
public byte[] getMarker(String encoding) {
117+
public byte[] getMarkerBytes(String encoding) {
118118
if (!this.enabled) {
119119
return null;
120120
}

internal-api/src/test/groovy/datadog/trace/api/rum/RumInjectorTest.groovy

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ class RumInjectorTest extends DDSpecification {
2020

2121
then:
2222
!injector.isEnabled()
23-
injector.getMarker(UTF8) == null
24-
injector.getSnippet(UTF8) == null
23+
injector.getMarkerBytes(UTF8) == null
24+
injector.getSnippetBytes(UTF8) == null
2525
}
2626

2727
void 'invalid config injector'() {
@@ -36,10 +36,10 @@ class RumInjectorTest extends DDSpecification {
3636

3737
then:
3838
!injector.isEnabled()
39-
injector.getMarker(UTF8) == null
40-
injector.getSnippet(UTF8) == null
41-
injector.getSnippet() == null
42-
injector.getMarker() == null
39+
injector.getMarkerBytes(UTF8) == null
40+
injector.getSnippetBytes(UTF8) == null
41+
injector.getSnippetChars() == null
42+
injector.getMarkerChars() == null
4343
}
4444

4545
void 'enabled injector'() {
@@ -56,9 +56,9 @@ class RumInjectorTest extends DDSpecification {
5656

5757
then:
5858
injector.isEnabled()
59-
injector.getMarker(UTF8) != null
60-
injector.getSnippet(UTF8) != null
61-
injector.getSnippet() != null
62-
injector.getMarker() != null
59+
injector.getMarkerBytes(UTF8) != null
60+
injector.getSnippetBytes(UTF8) != null
61+
injector.getSnippetChars() != null
62+
injector.getMarkerChars() != null
6363
}
6464
}

0 commit comments

Comments
 (0)