|
| 1 | +/* |
| 2 | + * Copyright 2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.integration.webflux.observation; |
| 18 | + |
| 19 | +import java.util.stream.Collectors; |
| 20 | + |
| 21 | +import brave.Tracing; |
| 22 | +import brave.propagation.ThreadLocalCurrentTraceContext; |
| 23 | +import brave.test.TestSpanHandler; |
| 24 | +import io.micrometer.observation.ObservationHandler; |
| 25 | +import io.micrometer.observation.ObservationRegistry; |
| 26 | +import io.micrometer.observation.tck.TestObservationRegistryAssert; |
| 27 | +import io.micrometer.tracing.Tracer; |
| 28 | +import io.micrometer.tracing.brave.bridge.BraveBaggageManager; |
| 29 | +import io.micrometer.tracing.brave.bridge.BraveCurrentTraceContext; |
| 30 | +import io.micrometer.tracing.brave.bridge.BraveFinishedSpan; |
| 31 | +import io.micrometer.tracing.brave.bridge.BravePropagator; |
| 32 | +import io.micrometer.tracing.brave.bridge.BraveTracer; |
| 33 | +import io.micrometer.tracing.handler.DefaultTracingObservationHandler; |
| 34 | +import io.micrometer.tracing.handler.PropagatingReceiverTracingObservationHandler; |
| 35 | +import io.micrometer.tracing.handler.PropagatingSenderTracingObservationHandler; |
| 36 | +import io.micrometer.tracing.propagation.Propagator; |
| 37 | +import io.micrometer.tracing.test.simple.SpansAssert; |
| 38 | +import org.junit.jupiter.api.BeforeEach; |
| 39 | +import org.junit.jupiter.api.Test; |
| 40 | + |
| 41 | +import org.springframework.beans.factory.annotation.Autowired; |
| 42 | +import org.springframework.context.ApplicationContext; |
| 43 | +import org.springframework.context.annotation.Bean; |
| 44 | +import org.springframework.context.annotation.Configuration; |
| 45 | +import org.springframework.http.HttpMethod; |
| 46 | +import org.springframework.integration.channel.FluxMessageChannel; |
| 47 | +import org.springframework.integration.channel.interceptor.ObservationPropagationChannelInterceptor; |
| 48 | +import org.springframework.integration.config.EnableIntegration; |
| 49 | +import org.springframework.integration.config.EnableIntegrationManagement; |
| 50 | +import org.springframework.integration.config.GlobalChannelInterceptor; |
| 51 | +import org.springframework.integration.dsl.IntegrationFlow; |
| 52 | +import org.springframework.integration.webflux.dsl.WebFlux; |
| 53 | +import org.springframework.messaging.Message; |
| 54 | +import org.springframework.messaging.PollableChannel; |
| 55 | +import org.springframework.messaging.support.ChannelInterceptor; |
| 56 | +import org.springframework.test.annotation.DirtiesContext; |
| 57 | +import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig; |
| 58 | +import org.springframework.test.web.reactive.server.WebTestClient; |
| 59 | +import org.springframework.web.filter.reactive.ServerHttpObservationFilter; |
| 60 | +import org.springframework.web.reactive.config.EnableWebFlux; |
| 61 | + |
| 62 | +import static org.assertj.core.api.Assertions.assertThat; |
| 63 | + |
| 64 | +/** |
| 65 | + * @author Artem Bilan |
| 66 | + * |
| 67 | + * @since 6.0.3 |
| 68 | + */ |
| 69 | +@SpringJUnitWebConfig |
| 70 | +@DirtiesContext |
| 71 | +public class WebFluxObservationPropagationTests { |
| 72 | + |
| 73 | + private static final TestSpanHandler SPANS = new TestSpanHandler(); |
| 74 | + |
| 75 | + @Autowired |
| 76 | + ObservationRegistry observationRegistry; |
| 77 | + |
| 78 | + @Autowired |
| 79 | + private WebTestClient webTestClient; |
| 80 | + |
| 81 | + @Autowired |
| 82 | + private PollableChannel testChannel; |
| 83 | + |
| 84 | + @BeforeEach |
| 85 | + void setup() { |
| 86 | + SPANS.clear(); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + void observationIsPropagatedFromWebFluxToServiceActivator() { |
| 91 | + String testData = "testData"; |
| 92 | + |
| 93 | + this.webTestClient.post().uri("/test") |
| 94 | + .bodyValue(testData) |
| 95 | + .exchange() |
| 96 | + .expectStatus().isOk(); |
| 97 | + |
| 98 | + Message<?> receive = this.testChannel.receive(10_000); |
| 99 | + assertThat(receive).isNotNull() |
| 100 | + .extracting(Message::getPayload) |
| 101 | + .isEqualTo("Received data: " + testData); |
| 102 | + |
| 103 | + TestObservationRegistryAssert.assertThat(this.observationRegistry) |
| 104 | + .hasRemainingCurrentObservation(); |
| 105 | + |
| 106 | + this.observationRegistry.getCurrentObservation().stop(); |
| 107 | + |
| 108 | + assertThat(SPANS.spans()).hasSize(6); |
| 109 | + SpansAssert.assertThat(SPANS.spans().stream().map(BraveFinishedSpan::fromBrave).collect(Collectors.toList())) |
| 110 | + .haveSameTraceId(); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + void observationIsPropagatedWebFluxRequestReply() { |
| 115 | + String testData = "TESTDATA"; |
| 116 | + |
| 117 | + this.webTestClient.get().uri("/testRequestReply?name=" + testData) |
| 118 | + .headers(headers -> headers.setBasicAuth("guest", "guest")) |
| 119 | + .exchange() |
| 120 | + .expectBody(String.class) |
| 121 | + .isEqualTo(testData.toLowerCase()); |
| 122 | + |
| 123 | + assertThat(SPANS.spans()).hasSize(3); |
| 124 | + SpansAssert.assertThat(SPANS.spans().stream().map(BraveFinishedSpan::fromBrave).collect(Collectors.toList())) |
| 125 | + .haveSameTraceId(); |
| 126 | + } |
| 127 | + |
| 128 | + @Configuration |
| 129 | + @EnableWebFlux |
| 130 | + @EnableIntegration |
| 131 | + @EnableIntegrationManagement(observationPatterns = "*") |
| 132 | + public static class ContextConfiguration { |
| 133 | + |
| 134 | + @Bean |
| 135 | + public Tracing braveTracing() { |
| 136 | + return Tracing.newBuilder().addSpanHandler(SPANS).build(); |
| 137 | + } |
| 138 | + |
| 139 | + @Bean |
| 140 | + Tracer simpleTracer(Tracing tracing) { |
| 141 | + return new BraveTracer(tracing.tracer(), |
| 142 | + new BraveCurrentTraceContext(ThreadLocalCurrentTraceContext.create()), |
| 143 | + new BraveBaggageManager()); |
| 144 | + } |
| 145 | + |
| 146 | + @Bean |
| 147 | + BravePropagator bravePropagator(Tracing tracing) { |
| 148 | + return new BravePropagator(tracing); |
| 149 | + } |
| 150 | + |
| 151 | + |
| 152 | + @Bean |
| 153 | + ObservationRegistry observationRegistry(Tracer tracer, Propagator propagator) { |
| 154 | + ObservationRegistry observationRegistry = ObservationRegistry.create(); |
| 155 | + observationRegistry.observationConfig() |
| 156 | + .observationHandler( |
| 157 | + // Composite will pick the first matching handler |
| 158 | + new ObservationHandler.FirstMatchingCompositeObservationHandler( |
| 159 | + // This is responsible for creating a child span on the sender side |
| 160 | + new PropagatingSenderTracingObservationHandler<>(tracer, propagator), |
| 161 | + // This is responsible for creating a span on the receiver side |
| 162 | + new PropagatingReceiverTracingObservationHandler<>(tracer, propagator), |
| 163 | + // This is responsible for creating a default span |
| 164 | + new DefaultTracingObservationHandler(tracer))); |
| 165 | + return observationRegistry; |
| 166 | + } |
| 167 | + |
| 168 | + @Bean |
| 169 | + WebTestClient webTestClient(ApplicationContext applicationContext) { |
| 170 | + return WebTestClient.bindToApplicationContext(applicationContext).build(); |
| 171 | + } |
| 172 | + |
| 173 | + @Bean |
| 174 | + ServerHttpObservationFilter webfluxObservationFilter(ObservationRegistry registry) { |
| 175 | + return new ServerHttpObservationFilter(registry); |
| 176 | + } |
| 177 | + |
| 178 | + @Bean |
| 179 | + @GlobalChannelInterceptor |
| 180 | + public ChannelInterceptor observationPropagationInterceptor(ObservationRegistry observationRegistry) { |
| 181 | + return new ObservationPropagationChannelInterceptor(observationRegistry); |
| 182 | + } |
| 183 | + |
| 184 | + @Bean |
| 185 | + IntegrationFlow webFluxFlow() { |
| 186 | + return IntegrationFlow |
| 187 | + .from(WebFlux.inboundChannelAdapter("/test") |
| 188 | + .requestMapping((mapping) -> mapping.methods(HttpMethod.POST)) |
| 189 | + .requestPayloadType(String.class) |
| 190 | + .id("webFluxInbound")) |
| 191 | + .channel(c -> c.flux("requestChannel")) |
| 192 | + .<String>handle((p, h) -> "Received data: " + p, e -> e.id("testHandler")) |
| 193 | + .channel(c -> c.queue("testChannel")) |
| 194 | + .get(); |
| 195 | + } |
| 196 | + |
| 197 | + @Bean |
| 198 | + FluxMessageChannel webFluxRequestChannel() { |
| 199 | + return new FluxMessageChannel(); |
| 200 | + } |
| 201 | + |
| 202 | + @Bean |
| 203 | + IntegrationFlow webFluxRequestReplyFlow(FluxMessageChannel webFluxRequestChannel) { |
| 204 | + return IntegrationFlow.from(WebFlux.inboundGateway("/testRequestReply") |
| 205 | + .requestMapping(r -> r.params("name")) |
| 206 | + .payloadExpression("#requestParams.name[0]") |
| 207 | + .requestChannel(webFluxRequestChannel) |
| 208 | + .id("webFluxGateway")) |
| 209 | + .<String, String>transform(String::toLowerCase, e -> e.id("testTransformer")) |
| 210 | + .get(); |
| 211 | + } |
| 212 | + |
| 213 | + } |
| 214 | + |
| 215 | +} |
0 commit comments