-
Notifications
You must be signed in to change notification settings - Fork 325
Add body capturing for reactive spring webclient #3754
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
3 commits
Select commit
Hold shift + click to select a range
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
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
30 changes: 30 additions & 0 deletions
30
...client-plugin/src/main/java/co/elastic/apm/agent/springwebclient/BodyCaptureRegistry.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,30 @@ | ||
| package co.elastic.apm.agent.springwebclient; | ||
|
|
||
| import co.elastic.apm.agent.httpclient.RequestBodyRecordingHelper; | ||
| import co.elastic.apm.agent.sdk.weakconcurrent.WeakConcurrent; | ||
| import co.elastic.apm.agent.sdk.weakconcurrent.WeakMap; | ||
| import co.elastic.apm.agent.tracer.AbstractSpan; | ||
| import co.elastic.apm.agent.tracer.Span; | ||
| import org.springframework.http.client.reactive.ClientHttpRequest; | ||
|
|
||
| import javax.annotation.Nullable; | ||
|
|
||
| public class BodyCaptureRegistry { | ||
|
|
||
| private static final WeakMap<ClientHttpRequest, RequestBodyRecordingHelper> PENDING_RECORDINGS = WeakConcurrent.buildMap(); | ||
|
|
||
| public static void maybeCaptureBodyFor(AbstractSpan<?> abstractSpan, ClientHttpRequest request) { | ||
| if (!(abstractSpan instanceof Span<?>)) { | ||
| return; | ||
| } | ||
| Span<?> span = (Span<?>) abstractSpan; | ||
| if (span.getContext().getHttp().getRequestBody().startCapture()) { | ||
| PENDING_RECORDINGS.put(request, new RequestBodyRecordingHelper(span)); | ||
| } | ||
| } | ||
|
|
||
| @Nullable | ||
| public static RequestBodyRecordingHelper activateRecording(ClientHttpRequest request) { | ||
| return PENDING_RECORDINGS.remove(request); | ||
| } | ||
| } |
102 changes: 102 additions & 0 deletions
102
...rc/main/java/co/elastic/apm/agent/springwebclient/ClientHttpConnectorInstrumentation.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,102 @@ | ||
| /* | ||
| * Licensed to Elasticsearch B.V. under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. licenses this file to you 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. | ||
| */ | ||
| package co.elastic.apm.agent.springwebclient; | ||
|
|
||
| import co.elastic.apm.agent.sdk.ElasticApmInstrumentation; | ||
| import co.elastic.apm.agent.tracer.GlobalTracer; | ||
| import co.elastic.apm.agent.tracer.TraceState; | ||
| import co.elastic.apm.agent.tracer.Tracer; | ||
| 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 org.springframework.http.HttpMethod; | ||
| import org.springframework.http.client.reactive.ClientHttpRequest; | ||
| import reactor.core.publisher.Mono; | ||
|
|
||
| import javax.annotation.Nullable; | ||
| import java.net.URI; | ||
| import java.util.Arrays; | ||
| import java.util.Collection; | ||
| import java.util.function.Function; | ||
|
|
||
| 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.nameStartsWith; | ||
| import static net.bytebuddy.matcher.ElementMatchers.named; | ||
| import static net.bytebuddy.matcher.ElementMatchers.not; | ||
| import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
|
|
||
| /** | ||
| * Instruments all {@link org.springframework.http.client.reactive.ClientHttpConnector} types, to preserve the span context | ||
| * within the callback passed to {@link org.springframework.http.client.reactive.ClientHttpConnector#connect(HttpMethod, URI, Function)}. | ||
| * If the span is ready for it, this will cause the request body to be captured. | ||
| */ | ||
| public class ClientHttpConnectorInstrumentation extends ElasticApmInstrumentation { | ||
|
|
||
| private static final Tracer tracer = GlobalTracer.get(); | ||
|
|
||
| @Override | ||
| public ElementMatcher<? super NamedElement> getTypeMatcherPreFilter() { | ||
| return nameStartsWith("org.springframework.http.") | ||
| .and(nameContains("Connector")); | ||
| } | ||
|
|
||
| @Override | ||
| public ElementMatcher<? super TypeDescription> getTypeMatcher() { | ||
| return hasSuperType(named("org.springframework.http.client.reactive.ClientHttpConnector")) | ||
| .and(not(isInterface())); | ||
| } | ||
|
|
||
| @Override | ||
| public ElementMatcher<? super MethodDescription> getMethodMatcher() { | ||
| return named("connect") | ||
| .and(takesArgument(2, named("java.util.function.Function"))); | ||
| } | ||
|
|
||
| @Override | ||
| public Collection<String> getInstrumentationGroupNames() { | ||
| return Arrays.asList("http-client", "spring-webclient"); | ||
| } | ||
|
|
||
| public static class AdviceClass { | ||
|
|
||
| @Nullable | ||
| @Advice.OnMethodEnter(suppress = Throwable.class, inline = false) | ||
| @Advice.AssignReturned.ToArguments(@Advice.AssignReturned.ToArguments.ToArgument(2)) | ||
| public static Function<? super ClientHttpRequest, Mono<Void>> onBefore(@Advice.Argument(2) Function<? super ClientHttpRequest, Mono<Void>> requestCallback) { | ||
| TraceState<?> context = tracer.currentContext(); | ||
| if (context.isEmpty()) { | ||
| return requestCallback; | ||
| } | ||
| return new Function<ClientHttpRequest, Mono<Void>>() { | ||
| @Override | ||
| public Mono<Void> apply(ClientHttpRequest clientHttpRequest) { | ||
| // Note that even though ClientHttpRequest exposes headers via the interface, those are empty | ||
| // therefore we check the span capturing pre-conditions in the WebClientExchangeFunctionInstrumentation instead | ||
| BodyCaptureRegistry.maybeCaptureBodyFor(context.getSpan(), clientHttpRequest); | ||
| return requestCallback.apply(clientHttpRequest); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| } | ||
| } |
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.