-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Capture request body for HttpUrlConnection (#3724)
- Loading branch information
Showing
17 changed files
with
533 additions
and
153 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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
55 changes: 55 additions & 0 deletions
55
...client-core/src/main/java/co/elastic/apm/agent/httpclient/RequestBodyRecordingHelper.java
This file contains 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,55 @@ | ||
package co.elastic.apm.agent.httpclient; | ||
|
||
import co.elastic.apm.agent.tracer.Span; | ||
import co.elastic.apm.agent.tracer.SpanEndListener; | ||
import co.elastic.apm.agent.tracer.metadata.BodyCapture; | ||
|
||
class RequestBodyRecordingHelper implements SpanEndListener<Span<?>> { | ||
|
||
/** | ||
* We do not need to participate in span reference counting here. | ||
* Instead, we only hold a reference to the span for the time it is not ended. | ||
* This is ensured via the {@link #onEnd(Span)} callback. | ||
*/ | ||
// Visible for testing | ||
Span<?> clientSpan; | ||
|
||
public RequestBodyRecordingHelper(Span<?> clientSpan) { | ||
if (!clientSpan.isFinished()) { | ||
this.clientSpan = clientSpan; | ||
clientSpan.addEndListener(this); | ||
} | ||
} | ||
|
||
void appendToBody(byte b) { | ||
if (clientSpan != null) { | ||
BodyCapture requestBody = clientSpan.getContext().getHttp().getRequestBody(); | ||
requestBody.append(b); | ||
if (requestBody.isFull()) { | ||
releaseSpan(); | ||
} | ||
} | ||
} | ||
|
||
void appendToBody(byte[] b, int off, int len) { | ||
if (clientSpan != null) { | ||
BodyCapture requestBody = clientSpan.getContext().getHttp().getRequestBody(); | ||
requestBody.append(b, off, len); | ||
if (requestBody.isFull()) { | ||
releaseSpan(); | ||
} | ||
} | ||
} | ||
|
||
void releaseSpan() { | ||
if (clientSpan != null) { | ||
clientSpan.removeEndListener(this); | ||
} | ||
clientSpan = null; | ||
} | ||
|
||
@Override | ||
public void onEnd(Span<?> span) { | ||
releaseSpan(); | ||
} | ||
} |
This file contains 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
Oops, something went wrong.