-
Notifications
You must be signed in to change notification settings - Fork 325
Capture request body for Apache HttpClient 4.x #3692
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ed26a8a
Implemented request body capturing for apache httpclient 4.x
JonasKunz 6314a66
added hacked serialization of http request body as otel attribute
JonasKunz 9494c0b
License headers, fix compilation
JonasKunz 418baf0
fix compilation
JonasKunz 1181ced
Fix apache http async client test
JonasKunz b0f1e09
Added API for enabling request body capture.
JonasKunz 417a735
Centralized logic and tests to be reusable across clients
JonasKunz 07dd59f
Added tests for BodyCapture context
JonasKunz 2b7d07c
fixes, renamed output otel attribute
JonasKunz faa5f1d
Merge remote-tracking branch 'elastic/main' into apache4-request-body
JonasKunz 250ee18
Added changelog
JonasKunz c8dcb9f
Added synchronization for body capture state
JonasKunz 19f3949
Update apm-agent-core/src/main/java/co/elastic/apm/agent/impl/context…
JonasKunz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
apm-agent-core/src/main/java/co/elastic/apm/agent/impl/context/BodyCaptureImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package co.elastic.apm.agent.impl.context; | ||
|
||
import co.elastic.apm.agent.objectpool.Resetter; | ||
import co.elastic.apm.agent.objectpool.impl.QueueBasedObjectPool; | ||
import co.elastic.apm.agent.tracer.configuration.WebConfiguration; | ||
import co.elastic.apm.agent.tracer.metadata.BodyCapture; | ||
import co.elastic.apm.agent.tracer.pooling.Allocator; | ||
import co.elastic.apm.agent.tracer.pooling.ObjectPool; | ||
import co.elastic.apm.agent.tracer.pooling.Recyclable; | ||
import org.jctools.queues.atomic.MpmcAtomicArrayQueue; | ||
|
||
import javax.annotation.Nullable; | ||
import java.nio.ByteBuffer; | ||
|
||
public class BodyCaptureImpl implements BodyCapture, Recyclable { | ||
private static final ObjectPool<ByteBuffer> BYTE_BUFFER_POOL = QueueBasedObjectPool.of(new MpmcAtomicArrayQueue<ByteBuffer>(128), false, | ||
new Allocator<ByteBuffer>() { | ||
@Override | ||
public ByteBuffer createInstance() { | ||
return ByteBuffer.allocate(WebConfiguration.MAX_BODY_CAPTURE_BYTES); | ||
} | ||
}, | ||
new Resetter<ByteBuffer>() { | ||
@Override | ||
public void recycle(ByteBuffer object) { | ||
object.clear(); | ||
} | ||
}); | ||
|
||
private enum CaptureState { | ||
NOT_ELIGIBLE, | ||
ELIGIBLE, | ||
STARTED | ||
} | ||
|
||
private volatile CaptureState state; | ||
|
||
private final StringBuilder charset; | ||
|
||
/** | ||
* The maximum number of bytes to capture, if the body is longer remaining bytes will be dropped. | ||
*/ | ||
private int numBytesToCapture; | ||
|
||
@Nullable | ||
private ByteBuffer bodyBuffer; | ||
|
||
BodyCaptureImpl() { | ||
charset = new StringBuilder(); | ||
resetState(); | ||
} | ||
|
||
@Override | ||
public void resetState() { | ||
state = CaptureState.NOT_ELIGIBLE; | ||
charset.setLength(0); | ||
if (bodyBuffer != null) { | ||
BYTE_BUFFER_POOL.recycle(bodyBuffer); | ||
} | ||
} | ||
|
||
@Override | ||
public void markEligibleForCapturing() { | ||
if (state == CaptureState.NOT_ELIGIBLE) { | ||
synchronized (this) { | ||
if (state == CaptureState.NOT_ELIGIBLE) { | ||
state = CaptureState.ELIGIBLE; | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isEligibleForCapturing() { | ||
return state != CaptureState.NOT_ELIGIBLE; | ||
} | ||
|
||
@Override | ||
public boolean startCapture(@Nullable String requestCharset, int numBytesToCapture) { | ||
if (numBytesToCapture > WebConfiguration.MAX_BODY_CAPTURE_BYTES) { | ||
throw new IllegalArgumentException("Capturing " + numBytesToCapture + " bytes is not supported, maximum is " + WebConfiguration.MAX_BODY_CAPTURE_BYTES + " bytes"); | ||
} | ||
if (state == CaptureState.ELIGIBLE) { | ||
synchronized (this) { | ||
if (state == CaptureState.ELIGIBLE) { | ||
if (requestCharset != null) { | ||
this.charset.append(requestCharset); | ||
} | ||
this.numBytesToCapture = numBytesToCapture; | ||
state = CaptureState.STARTED; | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private void acquireBodyBufferIfRequired() { | ||
if (state != CaptureState.STARTED) { | ||
throw new IllegalStateException("Capturing has not been started!"); | ||
} | ||
if (bodyBuffer == null) { | ||
bodyBuffer = BYTE_BUFFER_POOL.createInstance(); | ||
} | ||
} | ||
|
||
@Override | ||
public void append(byte b) { | ||
acquireBodyBufferIfRequired(); | ||
if (!isFull()) { | ||
bodyBuffer.put(b); | ||
} | ||
} | ||
|
||
@Override | ||
public void append(byte[] b, int offset, int len) { | ||
acquireBodyBufferIfRequired(); | ||
int remaining = numBytesToCapture - bodyBuffer.position(); | ||
if (remaining > 0) { | ||
bodyBuffer.put(b, offset, Math.min(len, remaining)); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isFull() { | ||
if (bodyBuffer == null) { | ||
return false; | ||
} | ||
return bodyBuffer.position() >= numBytesToCapture; | ||
} | ||
|
||
@Nullable | ||
public CharSequence getCharset() { | ||
if (charset.length() == 0) { | ||
return null; | ||
} | ||
return charset; | ||
} | ||
|
||
@Nullable | ||
public ByteBuffer getBody() { | ||
return bodyBuffer; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
apm-agent-core/src/test/java/co/elastic/apm/agent/impl/context/BodyCaptureTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package co.elastic.apm.agent.impl.context; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
import static co.elastic.apm.agent.testutils.assertions.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
public class BodyCaptureTest { | ||
|
||
@Test | ||
public void testAppendTruncation() { | ||
BodyCaptureImpl capture = new BodyCaptureImpl(); | ||
capture.markEligibleForCapturing(); | ||
capture.startCapture("foobar", 10); | ||
assertThat(capture.isFull()).isFalse(); | ||
|
||
capture.append("123Hello World!".getBytes(StandardCharsets.UTF_8), 3, 5); | ||
assertThat(capture.isFull()).isFalse(); | ||
|
||
capture.append(" from the other side".getBytes(StandardCharsets.UTF_8), 0, 20); | ||
assertThat(capture.isFull()).isTrue(); | ||
|
||
ByteBuffer content = capture.getBody(); | ||
int size = content.position(); | ||
byte[] contentBytes = new byte[size]; | ||
content.position(0); | ||
content.get(contentBytes); | ||
|
||
assertThat(contentBytes).isEqualTo("Hello from".getBytes(StandardCharsets.UTF_8)); | ||
} | ||
|
||
@Test | ||
public void testLifecycle() { | ||
BodyCaptureImpl capture = new BodyCaptureImpl(); | ||
|
||
assertThat(capture.isEligibleForCapturing()).isFalse(); | ||
assertThat(capture.startCapture("foobar", 42)) | ||
.isFalse(); | ||
assertThatThrownBy(() -> capture.append((byte) 42)).isInstanceOf(IllegalStateException.class); | ||
|
||
capture.markEligibleForCapturing(); | ||
assertThat(capture.isEligibleForCapturing()).isTrue(); | ||
assertThatThrownBy(() -> capture.append((byte) 42)).isInstanceOf(IllegalStateException.class); | ||
|
||
assertThat(capture.startCapture("foobar", 42)) | ||
.isTrue(); | ||
capture.append((byte) 42); //ensure no exception thrown | ||
|
||
// startCapture should return true only once | ||
assertThat(capture.startCapture("foobar", 42)) | ||
.isFalse(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.