-
Notifications
You must be signed in to change notification settings - Fork 327
Capture text-based request bodies #498
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
felixbarny
merged 20 commits into
elastic:master
from
felixbarny:improvement/capture-request-body
Mar 22, 2019
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
efa2ac9
Initial commit for POC that request body can be send to APM server
672d561
Started implementing comments
a3cdc19
Implemented change requests as stated in github comments
59e0ed1
Fixed wrong implementation of onReadExit. Parameters of read method h…
16a7e95
Extended InputStreamInstrumentation to be able to instrument ReadLine…
9b1e5e1
Merge branch 'master' of https://github.com/elastic/apm-agent-java in…
010bb4c
Continued to implement comments of felixbarny
1da9c25
Use prepared body request capture infrastructure
ba74473
Resolved merge conflicts
3762a0a
Merge remote-tracking branch 'origin/master' into improvement/capture…
felixbarny 643dc3c
Wrap ServletInputStream
felixbarny bca2b1d
Merge remote-tracking branch 'origin/master' into improvement/capture…
felixbarny 00b3544
Make capture_body content types configurable
felixbarny d366064
End reading if reset is called
felixbarny 7d0e453
Apply review suggestions
felixbarny 309ebe3
Add javadoc for setRawBody
felixbarny c65c9fd
Add integration tests for different read methods
felixbarny 368a8ef
Remove leftovers
felixbarny 84ff615
Merge branch 'master' into improvement/capture-request-body
felixbarny 77e1f4c
Rename capture_content_types to capture_body_content_types
felixbarny 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
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
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
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
125 changes: 125 additions & 0 deletions
125
...gin/src/main/java/co/elastic/apm/agent/servlet/RequestStreamRecordingInstrumentation.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,125 @@ | ||
| /*- | ||
| * #%L | ||
| * Elastic APM Java agent | ||
| * %% | ||
| * Copyright (C) 2018 - 2019 Elastic and contributors | ||
| * %% | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * #L% | ||
| */ | ||
| package co.elastic.apm.agent.servlet; | ||
|
|
||
| import co.elastic.apm.agent.bci.ElasticApmInstrumentation; | ||
| import co.elastic.apm.agent.bci.HelperClassManager; | ||
| import co.elastic.apm.agent.bci.VisibleForAdvice; | ||
| import co.elastic.apm.agent.impl.ElasticApmTracer; | ||
| import co.elastic.apm.agent.impl.context.Request; | ||
| import co.elastic.apm.agent.impl.transaction.Transaction; | ||
| import net.bytebuddy.asm.Advice; | ||
| import net.bytebuddy.description.NamedElement; | ||
| import net.bytebuddy.description.method.MethodDescription; | ||
| import net.bytebuddy.description.type.TypeDescription; | ||
| import net.bytebuddy.matcher.ElementMatcher; | ||
|
|
||
| import javax.annotation.Nullable; | ||
| import javax.servlet.ServletInputStream; | ||
| import java.util.Arrays; | ||
| import java.util.Collection; | ||
|
|
||
| import static co.elastic.apm.agent.servlet.ServletInstrumentation.SERVLET_API; | ||
| import static net.bytebuddy.matcher.ElementMatchers.hasSuperType; | ||
| import static net.bytebuddy.matcher.ElementMatchers.isInterface; | ||
| import static net.bytebuddy.matcher.ElementMatchers.nameContains; | ||
| import static net.bytebuddy.matcher.ElementMatchers.named; | ||
| import static net.bytebuddy.matcher.ElementMatchers.not; | ||
| import static net.bytebuddy.matcher.ElementMatchers.returns; | ||
|
|
||
| public class RequestStreamRecordingInstrumentation extends ElasticApmInstrumentation { | ||
|
|
||
| @Nullable | ||
| @VisibleForAdvice | ||
| // referring to InputStreamWrapperFactory is legal because of type erasure | ||
| public static HelperClassManager<InputStreamWrapperFactory> wrapperHelperClassManager; | ||
|
|
||
| @Override | ||
| public void init(ElasticApmTracer tracer) { | ||
| wrapperHelperClassManager = HelperClassManager.ForSingleClassLoader.of(tracer, | ||
| "co.elastic.apm.agent.servlet.helper.InputStreamFactoryHelperImpl", | ||
| "co.elastic.apm.agent.servlet.helper.RecordingServletInputStreamWrapper"); | ||
| } | ||
|
|
||
| @Override | ||
| public ElementMatcher<? super NamedElement> getTypeMatcherPreFilter() { | ||
| return nameContains("Request"); | ||
| } | ||
|
|
||
| @Override | ||
| public ElementMatcher<? super TypeDescription> getTypeMatcher() { | ||
| return hasSuperType(named("javax.servlet.ServletRequest")).and(not(isInterface())); | ||
| } | ||
|
|
||
| @Override | ||
| public ElementMatcher<? super MethodDescription> getMethodMatcher() { | ||
| return named("getInputStream").and(returns(named("javax.servlet.ServletInputStream"))); | ||
| } | ||
|
|
||
| @Override | ||
| public Collection<String> getInstrumentationGroupNames() { | ||
| return Arrays.asList(SERVLET_API, "servlet-input-stream"); | ||
| } | ||
|
|
||
| @Override | ||
| public Class<?> getAdviceClass() { | ||
| return GetInputStreamAdvice.class; | ||
| } | ||
|
|
||
| public interface InputStreamWrapperFactory { | ||
| ServletInputStream wrap(Request request, ServletInputStream servletInputStream); | ||
| } | ||
|
|
||
| public static class GetInputStreamAdvice { | ||
|
|
||
| @VisibleForAdvice | ||
| public static final ThreadLocal<Boolean> nestedThreadLocal = new ThreadLocal<Boolean>() { | ||
| @Override | ||
| protected Boolean initialValue() { | ||
| return Boolean.FALSE; | ||
| } | ||
| }; | ||
|
|
||
| @Advice.OnMethodEnter(suppress = Throwable.class) | ||
| public static void onReadEnter(@Advice.This Object thiz, | ||
| @Advice.Local("transaction") Transaction transaction, | ||
| @Advice.Local("nested") boolean nested) { | ||
| nested = nestedThreadLocal.get(); | ||
| nestedThreadLocal.set(Boolean.TRUE); | ||
| } | ||
|
|
||
| @Advice.OnMethodExit(suppress = Throwable.class) | ||
| public static void afterGetInputStream(@Advice.Return(readOnly = false) ServletInputStream inputStream, | ||
| @Advice.Local("nested") boolean nested) { | ||
| if (nested || tracer == null || wrapperHelperClassManager == null) { | ||
| return; | ||
| } | ||
| try { | ||
| final Transaction transaction = tracer.currentTransaction(); | ||
| // only wrap if the body buffer has been initialized via ServletTransactionHelper.startCaptureBody | ||
| if (transaction != null && transaction.getContext().getRequest().getBodyBuffer() != null) { | ||
| inputStream = wrapperHelperClassManager.getForClassLoaderOfClass(inputStream.getClass()).wrap(transaction.getContext().getRequest(), inputStream); | ||
| } | ||
| } finally { | ||
| nestedThreadLocal.set(Boolean.FALSE); | ||
eyalkoren marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
| } | ||
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
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.