|
| 1 | +/* |
| 2 | + * Copyright 2022-2024 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.cloud.stream.binder.reactorkafka; |
| 18 | + |
| 19 | +import java.lang.reflect.Type; |
| 20 | +import java.time.Duration; |
| 21 | +import java.util.function.Function; |
| 22 | +import java.util.stream.Collectors; |
| 23 | + |
| 24 | +import brave.handler.SpanHandler; |
| 25 | +import brave.test.TestSpanHandler; |
| 26 | +import io.micrometer.observation.Observation; |
| 27 | +import io.micrometer.observation.ObservationRegistry; |
| 28 | +import io.micrometer.observation.contextpropagation.ObservationThreadLocalAccessor; |
| 29 | +import io.micrometer.tracing.brave.bridge.BraveFinishedSpan; |
| 30 | +import io.micrometer.tracing.test.simple.SpansAssert; |
| 31 | +import org.apache.kafka.clients.consumer.ConsumerRecord; |
| 32 | +import org.apache.kafka.clients.producer.ProducerRecord; |
| 33 | +import org.junit.jupiter.api.Test; |
| 34 | +import reactor.core.publisher.Flux; |
| 35 | +import reactor.core.publisher.Mono; |
| 36 | +import reactor.kafka.receiver.ReceiverRecord; |
| 37 | +import reactor.kafka.receiver.observation.KafkaReceiverObservation; |
| 38 | +import reactor.kafka.receiver.observation.KafkaRecordReceiverContext; |
| 39 | + |
| 40 | +import org.springframework.beans.factory.annotation.Autowired; |
| 41 | +import org.springframework.boot.SpringBootConfiguration; |
| 42 | +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
| 43 | +import org.springframework.boot.test.autoconfigure.actuate.observability.AutoConfigureObservability; |
| 44 | +import org.springframework.boot.test.context.SpringBootTest; |
| 45 | +import org.springframework.cloud.stream.function.StreamBridge; |
| 46 | +import org.springframework.context.annotation.Bean; |
| 47 | +import org.springframework.integration.IntegrationMessageHeaderAccessor; |
| 48 | +import org.springframework.integration.support.MessageBuilder; |
| 49 | +import org.springframework.kafka.support.Acknowledgment; |
| 50 | +import org.springframework.kafka.support.converter.MessagingMessageConverter; |
| 51 | +import org.springframework.kafka.support.converter.RecordMessageConverter; |
| 52 | +import org.springframework.kafka.test.EmbeddedKafkaBroker; |
| 53 | +import org.springframework.kafka.test.context.EmbeddedKafka; |
| 54 | +import org.springframework.messaging.Message; |
| 55 | +import org.springframework.test.annotation.DirtiesContext; |
| 56 | + |
| 57 | +import static org.assertj.core.api.Assertions.assertThat; |
| 58 | +import static org.awaitility.Awaitility.await; |
| 59 | + |
| 60 | +/** |
| 61 | + * @author Artem Bilan |
| 62 | + * @author Soby Chacko |
| 63 | + * @since 4.2.0 |
| 64 | + */ |
| 65 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, properties = { |
| 66 | + "spring.kafka.consumer.metadata.max.age.ms=1000", |
| 67 | + "spring.cloud.function.definition=receive", |
| 68 | + "spring.cloud.stream.function.reactive.uppercase=true", |
| 69 | + "spring.cloud.stream.bindings.receive-in-0.group=rkbot-in-group", |
| 70 | + "spring.cloud.stream.bindings.receive-in-0.destination=rkbot-in-topic", |
| 71 | + "spring.cloud.stream.bindings.receive-out-0.destination=rkbot-out-topic", |
| 72 | + "spring.cloud.stream.kafka.binder.enable-observation=true", |
| 73 | + "spring.cloud.stream.kafka.binder.brokers=${spring.kafka.bootstrap-servers}", |
| 74 | + "management.tracing.sampling.probability=1", |
| 75 | + "spring.cloud.stream.kafka.bindings.receive-in-0.consumer.converterBeanName=fullRR" |
| 76 | + }) |
| 77 | +@DirtiesContext |
| 78 | +@AutoConfigureObservability |
| 79 | +@EmbeddedKafka(topics = { "rkbot-out-topic" }) |
| 80 | +public class ReactorKafkaBinderObservationTests { |
| 81 | + |
| 82 | + private static final TestSpanHandler SPANS = new TestSpanHandler(); |
| 83 | + |
| 84 | + @Autowired |
| 85 | + StreamBridge streamBridge; |
| 86 | + |
| 87 | + @Autowired |
| 88 | + ObservationRegistry observationRegistry; |
| 89 | + |
| 90 | + @Autowired |
| 91 | + TestConfiguration testConfiguration; |
| 92 | + |
| 93 | + @Autowired |
| 94 | + private EmbeddedKafkaBroker embeddedKafka; |
| 95 | + |
| 96 | + @Test |
| 97 | + void endToEndReactorKafkaBinder1() { |
| 98 | + |
| 99 | + streamBridge.send("rkbot-in-topic", MessageBuilder.withPayload("data") |
| 100 | + .build()); |
| 101 | + |
| 102 | + await().timeout(Duration.ofSeconds(10)).untilAsserted(() -> assertThat(SPANS.spans()).hasSize(3)); |
| 103 | + SpansAssert.assertThat(SPANS.spans().stream().map(BraveFinishedSpan::fromBrave).collect(Collectors.toList())) |
| 104 | + .haveSameTraceId(); |
| 105 | + } |
| 106 | + |
| 107 | + @SpringBootConfiguration |
| 108 | + @EnableAutoConfiguration(exclude = org.springframework.cloud.function.observability.ObservationAutoConfiguration.class) |
| 109 | + public static class TestConfiguration { |
| 110 | + |
| 111 | + @Bean |
| 112 | + SpanHandler testSpanHandler() { |
| 113 | + return SPANS; |
| 114 | + } |
| 115 | + |
| 116 | + @Bean |
| 117 | + RecordMessageConverter fullRR() { |
| 118 | + return new RecordMessageConverter() { |
| 119 | + |
| 120 | + private final RecordMessageConverter converter = new MessagingMessageConverter(); |
| 121 | + |
| 122 | + @Override |
| 123 | + public Message<?> toMessage(ConsumerRecord<?, ?> record, Acknowledgment acknowledgment, |
| 124 | + org.apache.kafka.clients.consumer.Consumer<?, ?> consumer, Type payloadType) { |
| 125 | + |
| 126 | + return MessageBuilder.withPayload(record).build(); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public ProducerRecord<?, ?> fromMessage(Message<?> message, String defaultTopic) { |
| 131 | + return this.converter.fromMessage(message, defaultTopic); |
| 132 | + } |
| 133 | + |
| 134 | + }; |
| 135 | + } |
| 136 | + |
| 137 | + @Bean |
| 138 | + Function<Flux<ReceiverRecord<byte[], byte[]>>, Flux<Message<String>>> receive(ObservationRegistry observationRegistry) { |
| 139 | + return s -> s |
| 140 | + .flatMap(record -> { |
| 141 | + Observation receiverObservation = |
| 142 | + KafkaReceiverObservation.RECEIVER_OBSERVATION.start(null, |
| 143 | + KafkaReceiverObservation.DefaultKafkaReceiverObservationConvention.INSTANCE, |
| 144 | + () -> |
| 145 | + new KafkaRecordReceiverContext( |
| 146 | + record, "user.receiver", "localhost:9092"), |
| 147 | + observationRegistry); |
| 148 | + |
| 149 | + return Mono.deferContextual(contextView -> Mono.just(record) |
| 150 | + .map(rec -> new String(rec.value()).toLowerCase()) |
| 151 | + .map(rec -> MessageBuilder.withPayload(rec).setHeader(IntegrationMessageHeaderAccessor.REACTOR_CONTEXT, contextView).build())) |
| 152 | + .doOnTerminate(receiverObservation::stop) |
| 153 | + .doOnError(receiverObservation::error) |
| 154 | + .contextWrite(context -> context.put(ObservationThreadLocalAccessor.KEY, receiverObservation)); |
| 155 | + }); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | +} |
| 160 | + |
0 commit comments