-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Propagate and Observation from Reactive context #3999
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
garyrussell
merged 1 commit into
spring-projects:main
from
artembilan:reactive_observation
Feb 1, 2023
Merged
Changes from all commits
Commits
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
215 changes: 215 additions & 0 deletions
215
...g/springframework/integration/webflux/observation/WebFluxObservationPropagationTests.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,215 @@ | ||
/* | ||
* Copyright 2023 the original author or authors. | ||
* | ||
* 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 | ||
* | ||
* https://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 org.springframework.integration.webflux.observation; | ||
|
||
import java.util.stream.Collectors; | ||
|
||
import brave.Tracing; | ||
import brave.propagation.ThreadLocalCurrentTraceContext; | ||
import brave.test.TestSpanHandler; | ||
import io.micrometer.observation.ObservationHandler; | ||
import io.micrometer.observation.ObservationRegistry; | ||
import io.micrometer.observation.tck.TestObservationRegistryAssert; | ||
import io.micrometer.tracing.Tracer; | ||
import io.micrometer.tracing.brave.bridge.BraveBaggageManager; | ||
import io.micrometer.tracing.brave.bridge.BraveCurrentTraceContext; | ||
import io.micrometer.tracing.brave.bridge.BraveFinishedSpan; | ||
import io.micrometer.tracing.brave.bridge.BravePropagator; | ||
import io.micrometer.tracing.brave.bridge.BraveTracer; | ||
import io.micrometer.tracing.handler.DefaultTracingObservationHandler; | ||
import io.micrometer.tracing.handler.PropagatingReceiverTracingObservationHandler; | ||
import io.micrometer.tracing.handler.PropagatingSenderTracingObservationHandler; | ||
import io.micrometer.tracing.propagation.Propagator; | ||
import io.micrometer.tracing.test.simple.SpansAssert; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.integration.channel.FluxMessageChannel; | ||
import org.springframework.integration.channel.interceptor.ObservationPropagationChannelInterceptor; | ||
import org.springframework.integration.config.EnableIntegration; | ||
import org.springframework.integration.config.EnableIntegrationManagement; | ||
import org.springframework.integration.config.GlobalChannelInterceptor; | ||
import org.springframework.integration.dsl.IntegrationFlow; | ||
import org.springframework.integration.webflux.dsl.WebFlux; | ||
import org.springframework.messaging.Message; | ||
import org.springframework.messaging.PollableChannel; | ||
import org.springframework.messaging.support.ChannelInterceptor; | ||
import org.springframework.test.annotation.DirtiesContext; | ||
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig; | ||
import org.springframework.test.web.reactive.server.WebTestClient; | ||
import org.springframework.web.filter.reactive.ServerHttpObservationFilter; | ||
import org.springframework.web.reactive.config.EnableWebFlux; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* @author Artem Bilan | ||
* | ||
* @since 6.0.3 | ||
*/ | ||
@SpringJUnitWebConfig | ||
@DirtiesContext | ||
public class WebFluxObservationPropagationTests { | ||
|
||
private static final TestSpanHandler SPANS = new TestSpanHandler(); | ||
|
||
@Autowired | ||
ObservationRegistry observationRegistry; | ||
|
||
@Autowired | ||
private WebTestClient webTestClient; | ||
|
||
@Autowired | ||
private PollableChannel testChannel; | ||
|
||
@BeforeEach | ||
void setup() { | ||
SPANS.clear(); | ||
} | ||
|
||
@Test | ||
void observationIsPropagatedFromWebFluxToServiceActivator() { | ||
String testData = "testData"; | ||
|
||
this.webTestClient.post().uri("/test") | ||
.bodyValue(testData) | ||
.exchange() | ||
.expectStatus().isOk(); | ||
|
||
Message<?> receive = this.testChannel.receive(10_000); | ||
assertThat(receive).isNotNull() | ||
.extracting(Message::getPayload) | ||
.isEqualTo("Received data: " + testData); | ||
|
||
TestObservationRegistryAssert.assertThat(this.observationRegistry) | ||
.hasRemainingCurrentObservation(); | ||
|
||
this.observationRegistry.getCurrentObservation().stop(); | ||
|
||
assertThat(SPANS.spans()).hasSize(6); | ||
SpansAssert.assertThat(SPANS.spans().stream().map(BraveFinishedSpan::fromBrave).collect(Collectors.toList())) | ||
.haveSameTraceId(); | ||
} | ||
|
||
@Test | ||
void observationIsPropagatedWebFluxRequestReply() { | ||
String testData = "TESTDATA"; | ||
|
||
this.webTestClient.get().uri("/testRequestReply?name=" + testData) | ||
.headers(headers -> headers.setBasicAuth("guest", "guest")) | ||
.exchange() | ||
.expectBody(String.class) | ||
.isEqualTo(testData.toLowerCase()); | ||
|
||
assertThat(SPANS.spans()).hasSize(3); | ||
SpansAssert.assertThat(SPANS.spans().stream().map(BraveFinishedSpan::fromBrave).collect(Collectors.toList())) | ||
.haveSameTraceId(); | ||
} | ||
|
||
@Configuration | ||
@EnableWebFlux | ||
@EnableIntegration | ||
@EnableIntegrationManagement(observationPatterns = "*") | ||
public static class ContextConfiguration { | ||
|
||
@Bean | ||
public Tracing braveTracing() { | ||
return Tracing.newBuilder().addSpanHandler(SPANS).build(); | ||
} | ||
|
||
@Bean | ||
Tracer simpleTracer(Tracing tracing) { | ||
return new BraveTracer(tracing.tracer(), | ||
new BraveCurrentTraceContext(ThreadLocalCurrentTraceContext.create()), | ||
new BraveBaggageManager()); | ||
} | ||
|
||
@Bean | ||
BravePropagator bravePropagator(Tracing tracing) { | ||
return new BravePropagator(tracing); | ||
} | ||
|
||
|
||
@Bean | ||
ObservationRegistry observationRegistry(Tracer tracer, Propagator propagator) { | ||
ObservationRegistry observationRegistry = ObservationRegistry.create(); | ||
observationRegistry.observationConfig() | ||
.observationHandler( | ||
// Composite will pick the first matching handler | ||
new ObservationHandler.FirstMatchingCompositeObservationHandler( | ||
// This is responsible for creating a child span on the sender side | ||
new PropagatingSenderTracingObservationHandler<>(tracer, propagator), | ||
// This is responsible for creating a span on the receiver side | ||
new PropagatingReceiverTracingObservationHandler<>(tracer, propagator), | ||
// This is responsible for creating a default span | ||
new DefaultTracingObservationHandler(tracer))); | ||
return observationRegistry; | ||
} | ||
|
||
@Bean | ||
WebTestClient webTestClient(ApplicationContext applicationContext) { | ||
return WebTestClient.bindToApplicationContext(applicationContext).build(); | ||
} | ||
|
||
@Bean | ||
ServerHttpObservationFilter webfluxObservationFilter(ObservationRegistry registry) { | ||
return new ServerHttpObservationFilter(registry); | ||
} | ||
|
||
@Bean | ||
@GlobalChannelInterceptor | ||
public ChannelInterceptor observationPropagationInterceptor(ObservationRegistry observationRegistry) { | ||
return new ObservationPropagationChannelInterceptor(observationRegistry); | ||
} | ||
|
||
@Bean | ||
IntegrationFlow webFluxFlow() { | ||
return IntegrationFlow | ||
.from(WebFlux.inboundChannelAdapter("/test") | ||
.requestMapping((mapping) -> mapping.methods(HttpMethod.POST)) | ||
.requestPayloadType(String.class) | ||
.id("webFluxInbound")) | ||
.channel(c -> c.flux("requestChannel")) | ||
.<String>handle((p, h) -> "Received data: " + p, e -> e.id("testHandler")) | ||
.channel(c -> c.queue("testChannel")) | ||
.get(); | ||
} | ||
|
||
@Bean | ||
FluxMessageChannel webFluxRequestChannel() { | ||
return new FluxMessageChannel(); | ||
} | ||
|
||
@Bean | ||
IntegrationFlow webFluxRequestReplyFlow(FluxMessageChannel webFluxRequestChannel) { | ||
return IntegrationFlow.from(WebFlux.inboundGateway("/testRequestReply") | ||
.requestMapping(r -> r.params("name")) | ||
.payloadExpression("#requestParams.name[0]") | ||
.requestChannel(webFluxRequestChannel) | ||
.id("webFluxGateway")) | ||
.<String, String>transform(String::toLowerCase, e -> e.id("testTransformer")) | ||
.get(); | ||
} | ||
|
||
} | ||
|
||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this not required in the send/receive case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is covered by the
MessagingGatewaySupport
change in this PR.We need such an action only when we lose a reactive scope.
This
send()
returns nothing, butsendAndReceiveMessageReactive()
returns aMono
where respective context is still present. Therefore we have to repeat a set toThreadLocal
over there where we lose the reactive stream context again.That's why I have those separate tests in this PR.
NOTE: The RSocket support will work as well just because we have a fix in the
MessagingGatewaySupport
and it does not use a plainsend()
.